SDL2 multimedia library for 2D graphics, input handling, audio playback, and window management. Core types: Window, Renderer, Texture, Surface, Event, and Mixer. Compile with -lib sdl2.
AudioDevice
An audio playback device fed by queueing samples, for streaming audio generated by the program (synthesis, sonification) rather than loaded from a file. Initialize the audio subsystem first. The device opens paused.
Operations
GetId #
Get the SDL audio device id
method : public : GetId() ~ IntReturn
| Type | Description |
|---|---|
| Int | device id, 0 if not open |
GetQueuedSize #
Get the number of bytes still queued for playback
method : public : GetQueuedSize() ~ IntReturn
| Type | Description |
|---|---|
| Int | queued byte count, 0 if not open |
IsNull #
Determines if the device failed to open or has been closed
method : public : IsNull() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if there is no open device, false otherwise |
New # constructor
Open the default playback device for queued audio
New(frequency:Int, format:Int, channels:Int, samples:Int)Parameters
| Name | Type | Description |
|---|---|---|
| frequency | Int | samples per second, such as 44100 |
| format | Int | sample format, such as AudioFormat->SDL_AUDIO_S16 or AudioFormat->SDL_AUDIO_F32 |
| channels | Int | 1 for mono, 2 for stereo |
| samples | Int | the device buffer size in sample frames, a power of 2 such as 4096 |
Example
Core->Init(SubsystemFlags->SDL_INIT_AUDIO);
device := AudioDevice->New(44100, AudioFormat->SDL_AUDIO_F32, 1, 4096);
if(<>device->IsNull()) {
tone := Float->New[44100];
each(i : tone) {
tone[i] := Float->Sin(2.0 * Float->Pi() * 440.0 * i->As(Float) / 44100.0) * 0.25;
};
device->QueueFloats(tone);
device->Pause(false);
Timer->Delay(1000);
device->Close();
};Pause #
Pause or resume playback
method : public : Pause(pause:Bool) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| pause | Bool | true to pause, false to play |
Queue #
Queue raw sample bytes, laid out in the device's format, for playback
method : public : Queue(bytes:Byte[]) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| bytes | Byte | the sample bytes |
Return
| Type | Description |
|---|---|
| Int | 0 on success or a negative error code on failure |
QueueFloats #
Queue samples given as Float values, normally from -1.0 to 1.0, interleaved by channel. The device must have been opened with AudioFormat->SDL_AUDIO_F32.
method : public : QueueFloats(samples:Float[]) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| samples | Float | the samples |
Return
| Type | Description |
|---|---|
| Int | 0 on success, or a negative value on failure or for a device of another format |