SDL bindings for Bare.
npm i bare-sdl
The Window API provides functionality to create SDL windows.
const window = new sdl.Window(title, width, height[, flags])Parameters:
title(string): The window titlewidth(number): The window width in pixelsheight(number): The window height in pixelsflags(number, optional): Window creation flags. Defaults to 0
Available flags are exposed through the constants object. Common flags include:
SDL_WINDOW_FULLSCREEN: Fullscreen windowSDL_WINDOW_BORDERLESS: Borderless windowSDL_WINDOW_RESIZABLE: Resizable windowSDL_WINDOW_VULKAN: Window usable with VulkanSDL_WINDOW_METAL: Window usable with Metal
Returns: A new sdl.Window instance
Destroy Window and associated resources.
Returns: void
Example:
const window = new sdl.Window('My Window', 800, 600)
window.destroy()The Renderer API provides functionality to render graphics using SDL.
const renderer = new sdl.Renderer(window)Parameters:
window(sdl.Window): The window instance to render to
Returns: A new Renderer instance
Clears the renderer with the current draw color.
Returns: boolean indicating success
Renders a texture to the renderer.
Parameters:
texture(sdl.Texture): The texture to render
Returns: boolean indicating success
Updates the screen with the rendered content.
Returns: boolean indicating success
Destroy Renderer and associated resources.
Returns: void
The Texture API provides functionality to create and manage SDL textures.
const texture = new sdl.Texture(renderer, width, height[, pixelFormat[, textureAccess]])Parameters:
renderer(sdl.Renderer): The renderer instancewidth(number): The texture width in pixelsheight(number): The texture height in pixelspixelFormat(number, optional): The pixel format. Defaults toSDL_PIXELFORMAT_RGB24textureAccess(number, optional): The texture access pattern. Defaults toSDL_TEXTUREACCESS_STREAMING
Available pixel formats and texture access flags are exposed through the constants object.
Returns: A new Texture instance
Updates the texture with new pixel data.
Parameters:
buffer(Buffer): The pixel data bufferpitch(number): The number of bytes per row
Returns: boolean indicating success
Destroy Texture and associated resources.
Returns: void
The Event API provides functionality to handle SDL events.
const event = new sdl.Event()Returns: A new Event instance
Gets the event type.
Returns: number
Gets the keyboard event data.
Returns: Event.Keyboard instance
The Event.Keyboard API provides functionality to handle SDL keyboard events.
const keyboardEvent = new sdl.Event.Keyboard([event])Parameters:
event(sdl.Event, optional): The parent event instance. If not provided, a new event will be created.
Returns: A new Event.Keyboard instance
Gets the keyboard scancode.
Returns: number
The Poller API provides functionality to poll for SDL events.
const poller = new sdl.Poller()Returns: A new sdl.Poller instance
Polls for events.
Parameters:
event(sdl.Event): The event instance to store the polled event data
Returns: boolean indicating if an event was polled
The AudioDevice API provides functionality to manage SDL audio devices for playback and recording.
const device = new sdl.AudioDevice(deviceId[, spec])Parameters:
deviceId(number): The audio device ID. Useconstants.SDL_AUDIO_DEVICE_DEFAULT_PLAYBACKorconstants.SDL_AUDIO_DEVICE_DEFAULT_RECORDINGfor defaults.spec(object, optional): Audio specification with the following properties:format(number): Audio format (e.g.,constants.SDL_AUDIO_F32)channels(number): Number of audio channels (e.g., 2 for stereo)freq(number): Sample rate in Hz (e.g., 48000)
Returns: A new AudioDevice instance
Gets the device name.
Returns: string
Gets the audio device format.
Returns: AudioDeviceFormat instance
Indicates if this is a playback device.
Returns: boolean
Indicates if this is a physical device.
Returns: boolean
Indicates if the device is paused.
Returns: boolean
Gets or sets the device gain (volume).
Returns: number (0.0 to 1.0)
Binds an audio stream to this device for playback or recording.
Parameters:
stream(AudioStream): The audio stream to bind
Returns: boolean indicating success
Unbinds an audio stream from this device.
Parameters:
stream(AudioStream): The audio stream to unbind
Returns: void
Pauses audio playback/recording.
Returns: boolean indicating success
Resumes audio playback/recording.
Returns: boolean indicating success
Closes the audio device.
Returns: void
Gets the audio formats supported by all playback devices.
Returns: AudioDeviceFormat[] - Array of audio device formats
Gets the audio formats supported by all recording devices.
Returns: AudioDeviceFormat[] - Array of audio device formats
Gets all available playback devices.
Returns: AudioDevice[] - An array of audio devices
Gets all available recording devices.
Returns: AudioDevice[] - An array of audio devices
Creates a new instance of AudioDevice for the default audio playback device. Equivalent to calling new AudioDevice(constants.SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, spec)
Parameters:
spec(object, optional): Audio specification (same asopen())
Returns: AudioDevice - The default playback device
Creates a new instance of AudioDevice for the default audio recording device. Equivalent to calling new AudioDevice(constants.SDL_AUDIO_DEVICE_DEFAULT_RECORDING, spec)
Parameters:
spec(object, optional): Audio specification (same asopen())
Returns: AudioDevice - The default recording device
Represents the format of an audio device.
const format = new sdl.AudioDevice.AudioDeviceFormat(deviceId)Parameters:
deviceId(number): The audio device ID
Returns: A new AudioDeviceFormat instance
Indicates if the format is valid.
Returns: boolean
Gets the number of sample frames.
Returns: number
Gets the audio specification.
Returns: AudioSpec instance
Represents an audio specification.
const spec = new sdl.AudioDevice.AudioSpec(format)Parameters:
format(AudioDeviceFormat): The audio device format
Returns: A new AudioSpec instance
Gets the audio format.
Returns: number (e.g., constants.SDL_AUDIO_F32)
Gets the number of channels.
Returns: number
Gets the sample rate in Hz.
Returns: number
The Camera API provides functionality to access and capture from system cameras.
const camera = new sdl.Camera(deviceId[, spec])The AudioStream API provides functionality to manage SDL audio streams for format conversion, resampling, and device binding.
const stream = new sdl.AudioStream(sourceSpec, targetSpec)Parameters:
deviceId(number | object): The camera device ID, or an object{ id }returned byCamera.getCameras()spec(object, optional): Requested camera specification with the following properties:format(number): Pixel format (e.g.,constants.SDL_PIXELFORMAT_UYVY)colorspace(number): Colorspace constantwidth(number): Frame width in pixelsheight(number): Frame height in pixelsframerateNumerator(number): Framerate numeratorframerateDenominator(number): Framerate denominator
Returns: A new Camera instance
Gets the camera instance ID.
Returns: number
Gets the camera device name.
Returns: string
Gets device properties.
Returns: object
Gets the permission state for camera access: -1 (denied), 0 (pending), 1 (approved).
Returns: number
Indicates if camera access is approved.
Returns: boolean
Indicates if camera access is denied.
Returns: boolean
Indicates if camera access is pending.
Returns: boolean
Gets the current camera specification.
Returns: Camera.CameraSpec instance
Acquires a single camera frame.
Returns: Camera.CameraFrame instance
Closes the camera and releases resources.
Returns: void
Gets available camera devices.
Returns: object[] - Array of { id, name, index }
Gets the name for a camera device ID.
Parameters:
deviceId(number): The camera device ID
Returns: string
Gets the position for a camera device ID.
Parameters:
deviceId(number): The camera device ID
Returns: number (constants.SDL_CAMERA_POSITION_FRONT_FACING or constants.SDL_CAMERA_POSITION_BACK_FACING)
Gets the supported formats for a camera device.
Parameters:
deviceId(number): The camera device ID
Returns: object[] - Array of format objects { format, colorspace, width, height, framerateNumerator, framerateDenominator, fps }
Creates a Camera for the default device, optionally with a requested spec.
Parameters:
spec(object, optional): Requested specification (same fields as constructor)
Returns: Camera - The default camera
Represents the current camera format selection. Typically accessed only via an existing Camera instance.
const spec = camera.specGets the pixel format.
Returns: number
Gets the colorspace.
Returns: number
Gets the frame width in pixels.
Returns: number
Gets the frame height in pixels.
Returns: number
Gets the framerate numerator.
Returns: number
Gets the framerate denominator.
Returns: number
Gets the frames per second.
Returns: number
Represents a single captured frame. Typically accessed only via return value of camera.acquireFrame.
const frame = camera.acquireFrame()Indicates if the frame is valid.
Returns: boolean
Gets the frame timestamp.
Returns: number
Gets the frame width in pixels.
Returns: number
Gets the frame height in pixels.
Returns: number
Gets the number of bytes per row.
Returns: number
Gets the pixel format.
Returns: number
Gets the raw pixel data.
Returns: Buffer
sourceSpec(object): Source audio specification with the following properties:format(number): Audio format (e.g.,constants.SDL_AUDIO_F32)channels(number): Number of audio channels (e.g., 2 for stereo)freq(number): Sample rate in Hz (e.g., 44100)
targetSpec(object): Target audio specification with the following properties:format(number): Audio format (e.g.,constants.SDL_AUDIO_F32)channels(number): Number of audio channels (e.g., 2 for stereo)freq(number): Sample rate in Hz (e.g., 44100)
Returns: A new AudioStream instance
Example:
const mic = sdl.AudioDevice.defaultRecordingDevice()
const targetSpec = {
format: sdl.constants.SDL_AUDIO_F32,
channels: mic.spec.channels,
freq: mic.spec.freq
}
const audioStream = new sdl.AudioStream(mic.spec, targetSpec)Puts audio data into the stream for processing.
Parameters:
buffer(ArrayBuffer): The audio data bufferoffset(number, optional): The offset in bytes. Defaults to 0length(number, optional): The number of bytes to put. Defaults tobuffer.byteLength - offset
Returns: boolean indicating success
Gets processed audio data from the stream.
Parameters:
buffer(ArrayBuffer): The buffer to store the audio dataoffset(number, optional): The offset in bytes. Defaults to 0length(number, optional): The number of bytes to get. Defaults tobuffer.byteLength - offset
Returns: number - The number of bytes read
Gets the number of bytes available for reading from the stream.
Returns: number
Flushes any remaining audio data in the stream.
Returns: boolean indicating success
Clears all audio data from the stream.
Returns: boolean indicating success
Gets the ID of the bound audio device.
Returns: number - The device ID (0 if not bound)
Creates a readable stream for getting audio data.
Returns: Readable stream
Creates a writable stream for putting audio data.
Returns: Writable stream
Destroys the audio stream and frees associated resources.
main
Returns: void
- Video playback with
bare-ffmpeg - List available audio playback and recording devices
- List available cameras
- Capture and playback camera stream
- Use sdl.Camera to get device list, use bare-ffmpeg to capture and process camera stream
Apache-2.0