static int S_CaptureDriverInit (int sampleRate) { SDL_AudioDeviceID inputdevid = 0; SDL_AudioSpec desired, obtained; int ret = 0; const char *requested_device = NULL; if (SDL_WasInit (SDL_INIT_AUDIO) == 0) ret = SDL_InitSubSystem (SDL_INIT_AUDIO); if (ret == -1) { Con_Printf ("Couldn't initialize SDL audio: %s\n", SDL_GetError ()); return false; } memset (&desired, 0, sizeof (desired)); desired.freq = sampleRate; desired.samples = 64; desired.format = AUDIO_S16LSB; desired.channels = 1; /* Make audiodevice list start from index 1 so that 0 can be system default */ if (s_inputdevice.integer > 0) { requested_device = SDL_GetAudioDeviceName (s_inputdevice.integer - 1, 0); } if ((inputdevid = SDL_OpenAudioDevice (requested_device, 1, &desired, &obtained, 0)) <= 0) { Com_Printf ("sound: couldn't open SDL audio: %s\n", SDL_GetError ()); if (requested_device != NULL) { Com_Printf ("sound: retrying with default audio device\n"); if ((inputdevid = SDL_OpenAudioDevice (NULL, 1, &desired, &obtained, 0)) <= 0) { Com_Printf ("sound: failure again, aborting...\n"); return 0; } Cvar_LatchedSet (&s_inputdevice, "0"); } } if (obtained.format != AUDIO_S16LSB) { Com_Printf ("SDL audio format %d unsupported.\n", obtained.format); SDL_CloseAudioDevice (inputdevid); inputdevid = 0; return 0; } if (obtained.channels != 1 && obtained.channels != 2) { Com_Printf ("SDL audio channels %d unsupported.\n", obtained.channels); SDL_CloseAudioDevice (inputdevid); inputdevid = 0; return 0; } Com_Printf ("Using SDL audio capture driver: %s @ %d Hz (samplerate %d)\n", SDL_GetCurrentAudioDriver (), obtained.freq, obtained.samples); SDL_PauseAudioDevice (inputdevid, 0); return inputdevid; }
SDL2Sink::SDL2Sink() : impl(std::make_unique<Impl>()) { if (SDL_Init(SDL_INIT_AUDIO) < 0) { LOG_CRITICAL(Audio_Sink, "SDL_Init(SDL_INIT_AUDIO) failed"); impl->audio_device_id = 0; return; } SDL_AudioSpec desired_audiospec; SDL_zero(desired_audiospec); desired_audiospec.format = AUDIO_S16; desired_audiospec.channels = 2; desired_audiospec.freq = native_sample_rate; desired_audiospec.samples = 1024; desired_audiospec.userdata = impl.get(); desired_audiospec.callback = &Impl::Callback; SDL_AudioSpec obtained_audiospec; SDL_zero(obtained_audiospec); impl->audio_device_id = SDL_OpenAudioDevice(nullptr, false, &desired_audiospec, &obtained_audiospec, 0); if (impl->audio_device_id <= 0) { LOG_CRITICAL(Audio_Sink, "SDL_OpenAudioDevice failed"); return; } impl->sample_rate = obtained_audiospec.freq; // SDL2 audio devices start out paused, unpause it: SDL_PauseAudioDevice(impl->audio_device_id, 0); }
static int sdlsnd_open(void *arg) { SDL_AudioSpec spec, spec1; int err; S_printf("Initializing SDL sound output\n"); err = SDL_InitSubSystem(SDL_INIT_AUDIO); if (err) { error("SDL audio init failed, %s\n", SDL_GetError()); return 0; } spec.freq = 44100; spec.format = AUDIO_S16LSB; spec.channels = 2; spec.samples = 1024; spec.callback = sdlsnd_callback; spec.userdata = NULL; dev = SDL_OpenAudioDevice(NULL, 0, &spec, &spec1, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); if (!dev) { SDL_QuitSubSystem(SDL_INIT_AUDIO); error("SDL sound init failed: %s\n", SDL_GetError()); return 0; } params.rate = spec1.freq; params.format = PCM_FORMAT_S16_LE; params.channels = spec1.channels; pcm_setup_hpf(¶ms); return 1; }
AUD_Sound* AUD_LoadWAV(const char* wav, int loop) { AUD_Sound* product = malloc(sizeof(AUD_Sound)); if (!product) { return NULL; } if (!SDL_LoadWAV(wav, &product->spec, &product->buf, &product->buflen)) { free(product); return NULL; } product->curlen = product->buflen; product->cur = product->buf; product->spec.callback = AUD_Callback; product->spec.userdata = product; product->loop = loop; product->volume = SDL_MIX_MAXVOLUME; product->dev = SDL_OpenAudioDevice(NULL, 0, &product->spec, NULL, 0); if (!product->dev) { SDL_FreeWAV(product->buf); free(product); return NULL; } return product; }
static void iteration() { SDL_Event e; SDL_AudioDeviceID dev; while (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) { done = 1; } else if (e.type == SDL_AUDIODEVICEADDED) { const char *name = SDL_GetAudioDeviceName(e.adevice.which, 0); SDL_Log("New %s audio device: %s\n", e.adevice.iscapture ? "capture" : "output", name); if (!e.adevice.iscapture) { positions[posindex] = 0; spec.userdata = &positions[posindex++]; spec.callback = fillerup; dev = SDL_OpenAudioDevice(name, 0, &spec, NULL, 0); if (!dev) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open '%s': %s\n", name, SDL_GetError()); } else { SDL_Log("Opened '%s' as %u\n", name, (unsigned int) dev); SDL_PauseAudioDevice(dev, 0); } } } else if (e.type == SDL_AUDIODEVICEREMOVED) { dev = (SDL_AudioDeviceID) e.adevice.which; SDL_Log("%s device %u removed.\n", e.adevice.iscapture ? "capture" : "output", (unsigned int) dev); SDL_CloseAudioDevice(dev); } } }
bool SDLSoundInit() { if (audioid) return true; if (SDL_InitSubSystem(SDL_INIT_AUDIO)) return false; int count = SDL_GetNumAudioDevices(0); for (int i = 0; i < count; ++i) { Output(OUTPUT_INFO, "Audio device %d: %s", i, SDL_GetAudioDeviceName(i, 0)); } SDL_zero(playbackspec); playbackspec.freq = 44100; playbackspec.format = AUDIO_S16SYS; playbackspec.channels = 1; playbackspec.samples = 1024; playbackspec.callback = SDLAudioCallback; playbackspec.userdata = nullptr; SDL_AudioSpec obtained; audioid = SDL_OpenAudioDevice(nullptr, 0, &playbackspec, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE); return !!audioid; }
static bool initAudio() { if (audioDev) { SDL_CloseAudioDevice(audioDev); audioDev = 0; } if (audioEnabled) { SDL_AudioSpec desired = {}, actual = {}; // We want 48 KHz stereo f32 to match opusfile output desired.freq = 48000; desired.channels = 2; desired.format = AUDIO_F32; desired.samples = 4096; // TODO: user-configurable buffer size? desired.callback = audioCallback; audioDev = SDL_OpenAudioDevice(NULL, 0, &desired, &actual, 0); if (!audioDev) { narf::console->println("Could not open default audio device: " + std::string(SDL_GetError())); audioEnabled = false; return false; } SDL_PauseAudioDevice(audioDev, 0); narf::console->println("Audio initialized: " + std::to_string(actual.freq) + " Hz, " + std::to_string(actual.channels) + " channels"); } return true; }
SdlAudioSink::SdlAudioSink(const AudioSource &source, int device_id) : bytes_per_sample(source.BytesPerSample()), ring_buf(RINGBUF_POWER, source.BytesPerSample()), position_sample_count(0), source_out(false), state(Audio::State::STOPPED) { const char *name = SDL_GetAudioDeviceName(device_id, 0); if (name == nullptr) { throw ConfigError(std::string("invalid device id: ") + std::to_string(device_id)); } SDL_AudioSpec want; SDL_zero(want); want.freq = source.SampleRate(); want.format = FORMATS[static_cast<int>(source.OutputSampleFormat())]; want.channels = source.ChannelCount(); want.callback = &SDLCallback; want.userdata = (void *)this; SDL_AudioSpec have; SDL_zero(have); this->device = SDL_OpenAudioDevice(name, 0, &want, &have, 0); if (this->device == 0) { throw ConfigError(std::string("couldn't open device: ") + SDL_GetError()); } }
bool SoundSDL::init(long sampleRate) { SDL_AudioSpec audio; audio.freq = sampleRate; audio.format = AUDIO_S16SYS; audio.channels = 2; audio.samples = 1024; audio.callback = soundCallback; audio.userdata = this; if (!SDL_WasInit(SDL_INIT_AUDIO)) SDL_Init(SDL_INIT_AUDIO); _dev = SDL_OpenAudioDevice(NULL, 0, &audio, NULL, SDL_AUDIO_ALLOW_ANY_CHANGE); if(_dev < 0) { fprintf(stderr,"Failed to open audio: %s\n", SDL_GetError()); return false; } _rbuf.reset(_delay * sampleRate * 2); if (!_initialized) { _mutex = SDL_CreateMutex(); _semBufferFull = SDL_CreateSemaphore (0); _semBufferEmpty = SDL_CreateSemaphore (1); _initialized = true; } return true; }
SDLAudioDevice::SDLAudioDevice(IBus *bus) : _state(src_new(SRC_LINEAR, 1, &_error)) , _bus(bus) { memset(_in.data(), 0, BUFFER_SIZE * sizeof(float)); memset(_out.data(), 0, BUFFER_SIZE * sizeof(float)); SDL_InitSubSystem(SDL_INIT_AUDIO); SDL_AudioSpec obtained, desired; SDL_zero(desired); desired.freq = 44800; desired.format = AUDIO_S16LSB; desired.channels = 1; desired.samples = AUDIO_BUFFER_SIZE * 2; desired.callback = audio_callback; desired.userdata = this; _device = SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0); // FIXME: handle different configurations if (_device == 0) { std::cerr << "Couldn't open audio device: " << SDL_GetError() << "\n"; throw 0; } else { std::cout << obtained.freq << '\n'; } }
void Mixer::Init(const char* device) { Close(); SDL_AudioSpec want, have; SDL_zero(want); want.freq = 44100; want.format = AUDIO_S16SYS; want.channels = 2; want.samples = 1024; want.callback = Callback; want.userdata = this; deviceid = SDL_OpenAudioDevice(device, 0, &want, &have, 0); format.format = have.format; format.channels = have.channels; format.freq = have.freq; const char* filename = get_file_path(PATH_ID_CSS1); for (size_t i = 0; i < Util::CountOf(css1sources); i++) { Source_Sample* source_sample = new Source_Sample; if (source_sample->LoadCSS1(filename, i)) { source_sample->Convert(format); // convert to audio output format, saves some cpu usage but requires a bit more memory, optional css1sources[i] = source_sample; } else { css1sources[i] = &source_null; delete source_sample; } } effectbuffer = new uint8[(have.samples * format.BytesPerSample() * format.channels)]; SDL_PauseAudioDevice(deviceid, 0); }
int main(int argc, char** argv) { if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0) { SDL_Log("Unable to initialize SDL: %s", SDL_GetError()); return 1; } SDL_Window* win = SDL_CreateWindow("Bytebeat Visualizer", 0, 0, 800, 600, 0); SDL_Renderer* ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); SDL_AudioSpec want, have; SDL_AudioDeviceID dev; SDL_zero(want); SDL_zero(have); want.freq = 8000; want.format = AUDIO_U8; want.channels = 1; want.samples = 4096; want.callback = audio_callback; // save the current time want.userdata = malloc(sizeof(unsigned int)); memset(want.userdata, 0, sizeof(unsigned int)); // and save the pointer for further use unsigned int* t = want.userdata; dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0); SDL_PauseAudioDevice(dev, 0); int running = 1; while (running) { SDL_Event e; while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_QUIT: running = 0; break; } } SDL_SetRenderDrawColor(ren, 0, 0, 0, 255); SDL_RenderClear(ren); SDL_SetRenderDrawColor(ren, 255, 255, 255, 255); unsigned int i = (*t); i -= 8000; for (; i < *t; i++) { int x = 0; int sample_width = 1; SDL_RenderDrawLine(ren, x, 0, x + sample_width, 200); x++; } SDL_RenderPresent(ren); } SDL_CloseAudioDevice(dev); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); SDL_Quit(); return 0; }
/** * \brief Locks and unlocks open audio device. * * \sa https://wiki.libsdl.org/SDL_LockAudioDevice * \sa https://wiki.libsdl.org/SDL_UnlockAudioDevice */ int audio_lockUnlockOpenAudioDevice() { int i; int count; char *device; SDL_AudioDeviceID id; SDL_AudioSpec desired, obtained; /* Get number of devices. */ count = SDL_GetNumAudioDevices(0); SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)"); if (count > 0) { for (i = 0; i < count; i++) { /* Get device name */ device = (char *)SDL_GetAudioDeviceName(i, 0); SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i); SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL"); if (device == NULL) return TEST_ABORTED; /* Set standard desired spec */ desired.freq=22050; desired.format=AUDIO_S16SYS; desired.channels=2; desired.samples=4096; desired.callback=_audio_testCallback; desired.userdata=NULL; /* Open device */ id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE); SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device); SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %i", id); if (id > 1) { /* Lock to protect callback */ SDL_LockAudioDevice(id); SDLTest_AssertPass("SDL_LockAudioDevice(%i)", id); /* Simulate callback processing */ SDL_Delay(10); SDLTest_Log("Simulate callback processing - delay"); /* Unlock again */ SDL_UnlockAudioDevice(id); SDLTest_AssertPass("SDL_UnlockAudioDevice(%i)", id); /* Close device again */ SDL_CloseAudioDevice(id); SDLTest_AssertPass("Call to SDL_CloseAudioDevice()"); } } } else { SDLTest_Log("No devices to test with"); } return TEST_COMPLETED; }
/** * \brief Opens, checks current connected status, and closes a device. * * \sa https://wiki.libsdl.org/SDL_AudioDeviceConnected */ int audio_openCloseAudioDeviceConnected() { int result = -1; int i; int count; char *device; SDL_AudioDeviceID id; SDL_AudioSpec desired, obtained; /* Get number of devices. */ count = SDL_GetNumAudioDevices(0); SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)"); if (count > 0) { for (i = 0; i < count; i++) { /* Get device name */ device = (char *)SDL_GetAudioDeviceName(i, 0); SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i); SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL"); if (device == NULL) return TEST_ABORTED; /* Set standard desired spec */ desired.freq=22050; desired.format=AUDIO_S16SYS; desired.channels=2; desired.samples=4096; desired.callback=_audio_testCallback; desired.userdata=NULL; /* Open device */ id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE); SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device); SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >1, got: %i", id); if (id > 1) { /* TODO: enable test code when function is available in SDL2 */ #ifdef AUDIODEVICECONNECTED_DEFINED /* Get connected status */ result = SDL_AudioDeviceConnected(id); SDLTest_AssertPass("Call to SDL_AudioDeviceConnected()"); #endif SDLTest_AssertCheck(result == 1, "Verify returned value; expected: 1; got: %i", result); /* Close device again */ SDL_CloseAudioDevice(id); SDLTest_AssertPass("Call to SDL_CloseAudioDevice()"); } } } else { SDLTest_Log("No devices to test with"); } return TEST_COMPLETED; }
static void test_multi_audio() { int keep_going = 1; int i; if (devcount > 64) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Too many devices (%d), clamping to 64...\n", devcount); devcount = 64; } spec.callback = play_through_once; SDL_memset(cbd, '\0', sizeof(cbd)); SDL_Log("playing on all devices...\n"); for (i = 0; i < devcount; i++) { const char *devname = SDL_GetAudioDeviceName(i, 0); spec.userdata = &cbd[i]; cbd[i].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0); if (cbd[i].dev == 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device %d failed: %s\n", i, SDL_GetError()); } } for (i = 0; i < devcount; i++) { if (cbd[i].dev) { SDL_PauseAudioDevice(cbd[i].dev, 0); } } #ifdef __EMSCRIPTEN__ emscripten_set_main_loop(loop, 0, 1); #else while (keep_going) { keep_going = 0; for (i = 0; i < devcount; i++) { if ((cbd[i].dev) && (!cbd[i].done)) { keep_going = 1; } } SDL_Delay(100); } #endif for (i = 0; i < devcount; i++) { if (cbd[i].dev) { SDL_PauseAudioDevice(cbd[i].dev, 1); SDL_CloseAudioDevice(cbd[i].dev); } } SDL_Log("All done!\n"); }
bool GBASDLInitAudio(struct GBASDLAudio* context, struct GBAThread* threadContext) { if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { GBALog(0, GBA_LOG_ERROR, "Could not initialize SDL sound system: %s", SDL_GetError()); return false; } context->desiredSpec.freq = 44100; context->desiredSpec.format = AUDIO_S16SYS; context->desiredSpec.channels = 2; context->desiredSpec.samples = context->samples; context->desiredSpec.callback = _GBASDLAudioCallback; context->desiredSpec.userdata = context; #if RESAMPLE_LIBRARY == RESAMPLE_NN context->drift = 0.f; #endif #if SDL_VERSION_ATLEAST(2, 0, 0) context->deviceId = SDL_OpenAudioDevice(0, 0, &context->desiredSpec, &context->obtainedSpec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); if (context->deviceId == 0) { #else if (SDL_OpenAudio(&context->desiredSpec, &context->obtainedSpec) < 0) { #endif GBALog(0, GBA_LOG_ERROR, "Could not open SDL sound system"); return false; } context->thread = threadContext; context->samples = context->obtainedSpec.samples; float ratio = GBAAudioCalculateRatio(0x8000, threadContext->fpsTarget, 44100); threadContext->audioBuffers = context->samples / ratio; if (context->samples > threadContext->audioBuffers) { threadContext->audioBuffers = context->samples * 2; } #if SDL_VERSION_ATLEAST(2, 0, 0) SDL_PauseAudioDevice(context->deviceId, 0); #else SDL_PauseAudio(0); #endif return true; } void GBASDLDeinitAudio(struct GBASDLAudio* context) { UNUSED(context); #if SDL_VERSION_ATLEAST(2, 0, 0) SDL_PauseAudioDevice(context->deviceId, 1); SDL_CloseAudioDevice(context->deviceId); #else SDL_PauseAudio(1); SDL_CloseAudio(); #endif SDL_QuitSubSystem(SDL_INIT_AUDIO); }
/** * \brief Opens, checks current audio status, and closes a device. * * \sa https://wiki.libsdl.org/SDL_GetAudioStatus */ int audio_openCloseAndGetAudioStatus() { SDL_AudioStatus result; int i; int count; char *device; SDL_AudioDeviceID id; SDL_AudioSpec desired, obtained; /* Get number of devices. */ count = SDL_GetNumAudioDevices(0); SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)"); if (count > 0) { for (i = 0; i < count; i++) { /* Get device name */ device = (char *)SDL_GetAudioDeviceName(i, 0); SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i); SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL"); if (device == NULL) return TEST_ABORTED; /* Set standard desired spec */ desired.freq=22050; desired.format=AUDIO_S16SYS; desired.channels=2; desired.samples=4096; desired.callback=_audio_testCallback; desired.userdata=NULL; /* Open device */ id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE); SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device); SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %i", id); if (id > 1) { /* Check device audio status */ result = SDL_GetAudioDeviceStatus(id); SDLTest_AssertPass("Call to SDL_GetAudioDeviceStatus()"); SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED, "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i", SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result); /* Close device again */ SDL_CloseAudioDevice(id); SDLTest_AssertPass("Call to SDL_CloseAudioDevice()"); } } } else { SDLTest_Log("No devices to test with"); } return TEST_COMPLETED; }
int main(int argc, char *argv[]) { // initialize SDL with only audio support if (SDL_Init(SDL_INIT_AUDIO)) { printf("SDL_Init: %s\n", SDL_GetError()); return -1; } // describe the kind of audio format we want SDL_AudioSpec desiredAudioFormat; SDL_zero(desiredAudioFormat); // initialize all fields to zero desiredAudioFormat.freq = 48000; // the frequency is 48,000 samples/second desiredAudioFormat.format = AUDIO_S16; // each sample is a 16 bit signed integer desiredAudioFormat.channels = 1; // mono audio desiredAudioFormat.samples = 4096; // 4096 samples will be submitted at a time desiredAudioFormat.callback = MyAudioCallback;// call MyAudioCallback whenever it needs more samples to play // open audio device SDL_AudioDeviceID audioDeviceID = SDL_OpenAudioDevice( NULL, // I don't care what device to use 0, // I'm not using this device for audio capture &desiredAudioFormat, // the format I want audio in NULL, // force format to be the desired one 0); // no special audio conversion flags if (!audioDeviceID) { printf("SDL_OpenAudioDevice: %s\n", SDL_GetError()); return -1; } // start playing audio by un-pausing it SDL_PauseAudioDevice(audioDeviceID, 0); while (1) { // wait for one event. SDL_Event e; SDL_WaitEvent(&e); // quit if it's a quit event. otherwise keep waiting. if (e.type == SDL_QUIT) { break; } } // throw everything away SDL_Quit(); return 0; }
qboolean SNDDMA_Init(void) { SDL_AudioSpec desired, obtained; SDL_zero(desired); desired.freq = 11025; desired.format = loadas8bit.value ? AUDIO_U8 : AUDIO_S16SYS; desired.channels = 2; desired.samples = 512; desired.callback = audioCallback; dev = SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, SDL_AUDIO_ALLOW_FORMAT_CHANGE); if (dev == 0) { Con_Printf("SDL_OpenAudioDevice error: %s\n", SDL_GetError()); return false; } shm = &sn; // SDL_AudioFormat layout // +----------------------sample is signed if set // | // | +----------sample is bigendian if set // | | // | | +--sample is float if set // | | | // | | | +--sample bit size---+ // | | | | | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 shm->samplebits = obtained.format & 0xFF; shm->speed = obtained.freq; shm->channels = obtained.channels; shm->samples = obtained.samples * obtained.channels * 8; shm->submission_chunk = 1; shm->samplepos = 0; bufferSize = shm->samples * (shm->samplebits / 8); shm->buffer = (unsigned char *) calloc(1, bufferSize); if (shm->buffer == NULL) { Con_Printf("Couldn't allocate sound buffer\n"); SDL_CloseAudioDevice(dev); shm = NULL; return false; } SDL_PauseAudioDevice(dev, 0); return true; }
void StringSynth::Play() { SDL_AudioSpec want; want.freq = 44100; want.format = AUDIO_F32SYS; want.channels = 1; want.samples = 4096; want.callback = stringsynth_callback; want.userdata = (void*)(this); device_ID = SDL_OpenAudioDevice(NULL, 0, &want, &specification, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE); SDL_PauseAudioDevice(device_ID, 0); paused = false; stopped = false; }
result sdl2static_init(SoLoud::Soloud *aSoloud, unsigned int aFlags, unsigned int aSamplerate, unsigned int aBuffer, unsigned int aChannels) { if (!SDL_WasInit(SDL_INIT_AUDIO)) { if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { return UNKNOWN_ERROR; } } SDL_AudioSpec as; as.freq = aSamplerate; as.format = AUDIO_F32; as.channels = aChannels; as.samples = aBuffer; as.callback = soloud_sdl2static_audiomixer; as.userdata = (void*)aSoloud; gAudioDeviceID = SDL_OpenAudioDevice(NULL, 0, &as, &gActiveAudioSpec, SDL_AUDIO_ALLOW_ANY_CHANGE & ~(SDL_AUDIO_ALLOW_FORMAT_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE)); if (gAudioDeviceID == 0) { as.format = AUDIO_S16; gAudioDeviceID = SDL_OpenAudioDevice(NULL, 0, &as, &gActiveAudioSpec, SDL_AUDIO_ALLOW_ANY_CHANGE & ~(SDL_AUDIO_ALLOW_FORMAT_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE)); if (gAudioDeviceID == 0) { return UNKNOWN_ERROR; } } aSoloud->postinit(gActiveAudioSpec.freq, gActiveAudioSpec.samples, aFlags, gActiveAudioSpec.channels); aSoloud->mBackendCleanupFunc = soloud_sdl2static_deinit; SDL_PauseAudioDevice(gAudioDeviceID, 0); aSoloud->mBackendString = "SDL2 (static)"; return 0; }
bool mSDLInitAudio(struct mSDLAudio* context, struct mCoreThread* threadContext) { if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { mLOG(SDL_AUDIO, ERROR, "Could not initialize SDL sound system: %s", SDL_GetError()); return false; } context->desiredSpec.freq = context->sampleRate; context->desiredSpec.format = AUDIO_S16SYS; context->desiredSpec.channels = 2; context->desiredSpec.samples = context->samples; context->desiredSpec.callback = _mSDLAudioCallback; context->desiredSpec.userdata = context; #if SDL_VERSION_ATLEAST(2, 0, 0) context->deviceId = SDL_OpenAudioDevice(0, 0, &context->desiredSpec, &context->obtainedSpec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); if (context->deviceId == 0) { #else if (SDL_OpenAudio(&context->desiredSpec, &context->obtainedSpec) < 0) { #endif mLOG(SDL_AUDIO, ERROR, "Could not open SDL sound system"); return false; } context->core = 0; if (threadContext) { context->core = threadContext->core; context->sync = &threadContext->impl->sync; #if SDL_VERSION_ATLEAST(2, 0, 0) SDL_PauseAudioDevice(context->deviceId, 0); #else SDL_PauseAudio(0); #endif } return true; } void mSDLDeinitAudio(struct mSDLAudio* context) { UNUSED(context); #if SDL_VERSION_ATLEAST(2, 0, 0) SDL_PauseAudioDevice(context->deviceId, 1); SDL_CloseAudioDevice(context->deviceId); #else SDL_PauseAudio(1); SDL_CloseAudio(); #endif SDL_QuitSubSystem(SDL_INIT_AUDIO); }
/* ================== SNDDMA_Init Try to find a sound device to mix for. Returns false if nothing is found. ================== */ int SNDDMA_Init(void) { SDL_AudioSpec desired = { 0 }; SDL_AudioSpec obtained = { 0 }; S_StopAllSounds(); Com_Printf("Initializing SDL audio\n"); if (s_khz->value == 44) desired.freq = 44100; else if (s_khz->value == 22) desired.freq = 22050; else desired.freq = 11025; desired.channels = 2; desired.samples = 2048; desired.format = AUDIO_S16; desired.callback = SNDDMA_Callback; desired.userdata = &dma; audio_device = SDL_OpenAudioDevice( NULL, /* default sound device */ false, /* playback */ &desired, &obtained, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); if (audio_device == 0) { Com_Printf("... unable to open.\n"); snd_init = false; return false; } memset((void *)&dma, 0, sizeof (dma)); dma.buffer = NULL; dma.channels = obtained.channels; dma.samplebits = 16; dma.samplepos = 0; dma.samples = obtained.samples * obtained.channels; dma.speed = obtained.freq; dma.submission_chunk = 512; snd_init = true; return true; }
const char* Sound_Queue::start( long sample_rate, int chan_count ) { assert( !bufs ); // can only be initialized once write_buf = 0; write_pos = 0; read_buf = 0; #ifndef DREAMCAST bufs = new sample_t [(long) buf_size * buf_count]; #else bufs = (sample_t *)std::malloc((long) buf_size * buf_count); #endif if ( !bufs ) { log_message(LOG_ERROR, "Run out of memory starting sound queue\n"); return "Out of memory"; } currently_playing_ = bufs; free_sem = SDL_CreateSemaphore( buf_count - 1 ); if ( !free_sem ) { log_message(LOG_ERROR, "Couldn't create semaphore starting sound queue. %s\n", SDL_GetError()); return sdl_error( "Couldn't create semaphore" ); } SDL_AudioSpec as; as.freq = sample_rate; as.format = AUDIO_S16SYS; as.channels = chan_count; as.silence = 0; as.samples = buf_size / chan_count; as.size = 0; as.callback = fill_buffer_; as.userdata = this; SDL_AudioSpec as2; // if ( SDL_OpenAudio( &as, NULL ) < 0 ) if (!(this->device = SDL_OpenAudioDevice(NULL, 0, &as, &as2, SDL_AUDIO_ALLOW_ANY_CHANGE))) { log_message(LOG_ERROR, "Couldn't open SDL audio\n"); return sdl_error( "Couldn't open SDL audio" ); } SDL_PauseAudioDevice(this->device, 0); sound_open = true; return NULL; }
internal SDL_AudioDeviceID SDLInitSound(int32 SamplesPerSecond) { SDL_AudioSpec spec = {}; spec.freq = SamplesPerSecond; spec.format = AUDIO_S16; spec.channels = 2; spec.samples = 4096; SDL_AudioDeviceID Result = SDL_OpenAudioDevice(NULL, 0, &spec, 0, 0); if (Result != 0) { SDL_PauseAudioDevice(Result, 0); } else { printf("Failed to open SDL audio device: %s\n", SDL_GetError()); } return Result; }
bool SDLSoundInit() { if (audioid) return true; if (SDL_InitSubSystem(SDL_INIT_AUDIO)) return false; playbackspec.freq = 44100; playbackspec.format = AUDIO_S16SYS; playbackspec.channels = 1; playbackspec.samples = 512; playbackspec.callback = SDLAudioCallback; playbackspec.userdata = nullptr; audioid = SDL_OpenAudioDevice(nullptr, 0, &playbackspec, nullptr, SDL_AUDIO_ALLOW_ANY_CHANGE); return !!audioid; }
Player::Player() { if (SDL_Init(SDL_INIT_AUDIO) < 0) { std::ostringstream os; os << "Could not initialize SDL audio: " << SDL_GetError(); throw std::runtime_error(os.str()); } SDL_AudioSpec want, have; SDL_zero(want); want.freq = 22050; want.format = AUDIO_S16LSB; want.channels = 1; want.samples = 4096; want.userdata = (void*) this; want.callback = Player::audioCallbackProxy; audioDevice = SDL_OpenAudioDevice(NULL, 0, &want, &have, SDL_AUDIO_ALLOW_FORMAT_CHANGE); }
AudioHandler::AudioHandler(AVStream* aStream) { isQuit = false; packetQueue = new PacketQueue(); if (SDL_Init(SDL_INIT_AUDIO) < 0) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(0); } audioStream = aStream; codecContext = audioStream->codec; codec = avcodec_find_decoder(codecContext->codec_id); if (codec == nullptr) { fprintf(stderr, "Audio codec not found or unsupported.\n"); exit(1); } if (codecContext->sample_fmt == AV_SAMPLE_FMT_S16P) codecContext->request_sample_fmt = AV_SAMPLE_FMT_S16; if (avcodec_open2(codecContext, codec, nullptr) < 0) { fprintf(stderr, "Could not open audiocodec.\n"); exit(1); } SDL_AudioSpec desiredSpecs; SDL_AudioSpec specs; SDL_zero(desiredSpecs); desiredSpecs.freq = codecContext->sample_rate; desiredSpecs.format = AUDIO_S16SYS; desiredSpecs.channels = codecContext->channels; desiredSpecs.silence = 0; desiredSpecs.samples = SDL_AUDIO_BUFFER_SIZE; desiredSpecs.callback = PlaybackCallback; desiredSpecs.userdata = this; //if (SDL_OpenAudio(&desiredSpecs, &specs) < 0) { device = SDL_OpenAudioDevice(nullptr, 0, &desiredSpecs, &specs, SDL_AUDIO_ALLOW_FORMAT_CHANGE); if (device == 0) { fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); exit(1); } if (desiredSpecs.format != specs.format) printf("We didn't get desired audio format.\n"); }
SoundSystemImpl::SoundSystemImpl() { // Setup audio mAudioSpecRequest.freq = 48000; mAudioSpecRequest.format = AUDIO_F32; mAudioSpecRequest.channels = 2; mAudioSpecRequest.samples = SoundFrame::sample_size; mAudioSpecRequest.callback = nullptr; mAudioSpecRequest.userdata = nullptr; SDL_memset(&mAudioSpecObtained, 0, sizeof(mAudioSpecObtained)); mAudioDeviceId = SDL_OpenAudioDevice(nullptr, false, &mAudioSpecRequest, &mAudioSpecObtained, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); if (mAudioDeviceId <= 0) { printf("Couldn't open audio: %s\n", SDL_GetError()); SDL_assert(false); exit(EXIT_FAILURE); } printf("Audio: freq %d, format %d, channels %d, samples %d \n", mAudioSpecObtained.freq, mAudioSpecObtained.format, mAudioSpecObtained.channels, mAudioSpecObtained.samples); mFrameMixer.reserve(mAudioSpecObtained.samples * mAudioSpecObtained.channels); SDL_PauseAudioDevice(mAudioDeviceId, 0); }
void Audio_init () { SDL_AudioSpec want; want.freq = 44100; want.format = AUDIO_S16LSB; want.channels = 2; want.samples = 2048; want.callback = Audio_mixer; want.userdata = NULL; if (App_get_option_IV("info")) { int i; printf("Audio devices:\n"); for (i = 0; i < SDL_GetNumAudioDevices(0); i++) { const char* name = SDL_GetAudioDeviceName(i, 0); printf("\t%s\n", name); } const char* cur = SDL_GetCurrentAudioDriver(); printf("Current audio driver:\n\t%s\n", cur); printf("Audio drivers:\n"); for (i = 0; i < SDL_GetNumAudioDrivers(); i++) { const char* name = SDL_GetAudioDriver(i); printf("\t%s\n", name); } } _audio.device = SDL_OpenAudioDevice(NULL, 0, &want, &_audio.spec, SDL_AUDIO_ALLOW_ANY_CHANGE); if (_audio.device <= 0) error("Could not open audio device"); if (!App_get_option_IV("mute")) _audio.volume = SDL_MIX_MAXVOLUME; (void)SDL_AtomicSet(&_audio.playback_rate, 1); SDL_PauseAudioDevice(_audio.device, 0); }