/** * \brief Negative tests around enumeration and naming of audio devices. * * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetNumAudioDevices * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetAudioDeviceName */ int audio_enumerateAndNameAudioDevicesNegativeTests() { int ret; int t; int i, j, no, nc; const char *name; /* Get number of devices. */ no = SDL_GetNumAudioDevices(0); nc = SDL_GetNumAudioDevices(1); /* Invalid device index when getting name */ for (t=0; t<2; t++) { /* Negative device index */ i = -1; name = SDL_GetAudioDeviceName(i, t); AssertTrue(name == NULL, "SDL_GetAudioDeviceName(%i, %i): returned a name, should return NULL", i, t); /* Device index past range */ for (j=0; j<3; j++) { i = (t) ? nc+j : no+j; name = SDL_GetAudioDeviceName(i, t); AssertTrue(name == NULL, "SDL_GetAudioDeviceName(%i, %i): returned a name, should return NULL", i, t); } /* Capture index past capture range but within output range */ if ((no>0) && (no>nc) && (t==1)) { i = no-1; name = SDL_GetAudioDeviceName(i, t); AssertTrue(name == NULL, "SDL_GetAudioDeviceName(%i, %i): returned a name, should return NULL", i, t); } } }
/** * \brief Enumerate and name available audio devices (output and capture). * * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName */ int audio_enumerateAndNameAudioDevices() { int t, tt; int i, n, nn; const char *name, *nameAgain; /* Iterate over types: t=0 output device, t=1 input/capture device */ for (t=0; t<2; t++) { /* Get number of devices. */ n = SDL_GetNumAudioDevices(t); SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(%i)", t); SDLTest_Log("Number of %s devices < 0, reported as %i", (t) ? "capture" : "output", n); SDLTest_AssertCheck(n >= 0, "Validate result is >= 0, got: %i", n); /* Variation of non-zero type */ if (t==1) { tt = t + SDLTest_RandomIntegerInRange(1,10); nn = SDL_GetNumAudioDevices(tt); SDLTest_AssertCheck(n==nn, "Verify result from SDL_GetNumAudioDevices(%i), expected same number of audio devices %i, got %i", tt, n, nn); nn = SDL_GetNumAudioDevices(-tt); SDLTest_AssertCheck(n==nn, "Verify result from SDL_GetNumAudioDevices(%i), expected same number of audio devices %i, got %i", -tt, n, nn); } /* List devices. */ if (n>0) { for (i=0; i<n; i++) { name = SDL_GetAudioDeviceName(i, t); SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t); SDLTest_AssertCheck(name != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, t); if (name != NULL) { SDLTest_AssertCheck(name[0] != '\0', "verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, t, name); if (t==1) { /* Also try non-zero type */ tt = t + SDLTest_RandomIntegerInRange(1,10); nameAgain = SDL_GetAudioDeviceName(i, tt); SDLTest_AssertCheck(nameAgain != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, tt); if (nameAgain != NULL) { SDLTest_AssertCheck(nameAgain[0] != '\0', "Verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, tt, nameAgain); SDLTest_AssertCheck(SDL_strcmp(name, nameAgain)==0, "Verify SDL_GetAudioDeviceName(%i, %i) and SDL_GetAudioDeviceName(%i %i) return the same string", i, t, i, tt); } } } } } } return TEST_COMPLETED; }
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 int lua_getAudioDevices(lutok::state& state){ state.new_table(); for (int capturing=0; capturing<2; capturing++){ state.push_string( (capturing == 0) ? "playback" : "capture"); state.new_table(); int ii=1; for (int i=0; i<SDL_GetNumAudioDevices(capturing); i++){ const char * name = SDL_GetAudioDeviceName( i, capturing); if (name){ state.push_integer(ii++); state.new_table(); state.push_literal("id"); state.push_integer(i); state.set_table(); state.push_literal("name"); state.push_string(name); state.set_table(); state.set_table(); } } state.set_table(); } return 1; }
/* static */ bool SdlAudioSink::IsOutputDevice(int id) { int ids = SDL_GetNumAudioDevices(0); // See comment in GetDevicesInfo for why this is sufficient. return (0 <= id && id < ids); }
void audio_populate_devices() { if (gAudioDevices != NULL) free(gAudioDevices); gAudioDeviceCount = SDL_GetNumAudioDevices(SDL_FALSE); if (gAudioDeviceCount <= 0) return; audio_device *systemAudioDevices = malloc(gAudioDeviceCount * sizeof(audio_device)); for (int i = 0; i < gAudioDeviceCount; i++) { const char *utf8Name = SDL_GetAudioDeviceName(i, SDL_FALSE); if (utf8Name == NULL) utf8Name = language_get_string(5511); safe_strcpy(systemAudioDevices[i].name, utf8Name, AUDIO_DEVICE_NAME_SIZE); } #ifndef __LINUX__ gAudioDeviceCount++; gAudioDevices = malloc(gAudioDeviceCount * sizeof(audio_device)); safe_strcpy(gAudioDevices[0].name, language_get_string(5510), AUDIO_DEVICE_NAME_SIZE); memcpy(&gAudioDevices[1], systemAudioDevices, (gAudioDeviceCount - 1) * sizeof(audio_device)); #else gAudioDevices = malloc(gAudioDeviceCount * sizeof(audio_device)); memcpy(gAudioDevices, systemAudioDevices, gAudioDeviceCount * sizeof(audio_device)); #endif // __LINUX__ free(systemAudioDevices); }
int main(int argc, char **argv) { /* Enable standard application logging */ SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); /* Load the SDL library */ if (SDL_Init(SDL_INIT_AUDIO) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); return (1); } SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver()); devcount = SDL_GetNumAudioDevices(0); if (devcount < 1) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Don't see any specific audio devices!\n"); } else { if (argv[1] == NULL) { argv[1] = "sample.wav"; } /* Load the wave file into memory */ if (SDL_LoadWAV(argv[1], &spec, &sound, &soundlen) == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", argv[1], SDL_GetError()); } else { test_multi_audio(); SDL_FreeWAV(sound); } } SDL_Quit(); return 0; }
static int lua_SDL_GetNumAudioDevices(lutok::state& state){ bool capture = false; if (state.is_boolean(1)){ capture = state.to_boolean(1); } state.push_integer(SDL_GetNumAudioDevices( (capture) ? 1 : 0)); return 1; }
std::vector<std::string> AudioManager::get_devices() { std::vector<std::string> device_list; auto num_devices = SDL_GetNumAudioDevices(0); for (int i = 0; i < num_devices; i++) { device_list.push_back(SDL_GetAudioDeviceName(i, 0)); } return device_list; }
/** * \brief Enumerate and name available audio devices (output and capture). * * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetNumAudioDevices * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetAudioDeviceName */ int audio_enumerateAndNameAudioDevices() { int ret; int t, tt; int i, n, nn; const char *name, *nameAgain; /* Iterate over types: t=0 output device, t=1 input/capture device */ for (t=0; t<2; t++) { /* Get number of devices. */ n = SDL_GetNumAudioDevices(t); AssertTrue(n>=0, "Number of %s devices < 0, reported as %i: %s", (t) ? "output" : "capture", n, SDL_GetError()); /* Variation of non-zero type */ if (t==1) { tt = t + RandomIntegerInRange(1,10); nn = SDL_GetNumAudioDevices(tt); AssertTrue(n==nn, "SDL_GetNumAudioDevices(%i) : expected same number of audio devices %i, got %i", tt, n, nn); nn = SDL_GetNumAudioDevices(-tt); AssertTrue(n==nn, "SDL_GetNumAudioDevices(%i) : expected same number of audio devices %i, got %i", -tt, n, nn); } /* List devices. */ if (n>0) { for (i=0; i<n; i++) { name = SDL_GetAudioDeviceName(i, t); AssertTrue(name != NULL, "SDL_GetAudioDeviceName(%i, %i): returned NULL name", i, t); AssertTrue(strlen(name)>0, "SDL_GetAudioDeviceName(%i, %i): returned empty name string", i, t); if (t==1) { /* Also try non-zero type */ nameAgain = SDL_GetAudioDeviceName(i, tt); AssertTrue(nameAgain != NULL, "SDL_GetAudioDeviceName(%i, %i): returned NULL name", i, tt); AssertTrue(strlen(nameAgain)>0, "SDL_GetAudioDeviceName(%i, %i): returned empty name string", i, tt); AssertTrue(strcmp(name, nameAgain)==0, "SDL_GetAudioDeviceName(%i, %i): returned unexpected name string %s, expected %s", i, tt, nameAgain, name); } } } } }
/** * \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 SND_DeviceList(void) { int i, count = SDL_GetNumAudioDevices(qfalse); Com_Printf("Printing audio device list. Number of devices: %i\n\n", count); for (i = 0; i < count; ++i) { Com_Printf(" Audio device %d: %s\n", i, SDL_GetAudioDeviceName(i, 0)); } }
/** * \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; }
/* static */ std::vector<std::pair<int, std::string>> SdlAudioSink::GetDevicesInfo() { std::vector<std::pair<int, std::string>> list; // The 0 in SDL_GetNumAudioDevices tells SDL we want playback devices. int is = SDL_GetNumAudioDevices(0); for (int i = 0; i < is; i++) { const char *n = SDL_GetAudioDeviceName(i, 0); if (n != nullptr) list.emplace_back(i, std::string(n)); } return list; }
/** * \brief Negative tests around enumeration and naming of audio devices. * * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName */ int audio_enumerateAndNameAudioDevicesNegativeTests() { int t; int i, j, no, nc; const char *name; /* Get number of devices. */ no = SDL_GetNumAudioDevices(0); SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)"); nc = SDL_GetNumAudioDevices(1); SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(1)"); /* Invalid device index when getting name */ for (t=0; t<2; t++) { /* Negative device index */ i = SDLTest_RandomIntegerInRange(-10,-1); name = SDL_GetAudioDeviceName(i, t); SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t); SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result NULL, expected NULL, got: %s", i, t, (name == NULL) ? "NULL" : name); /* Device index past range */ for (j=0; j<3; j++) { i = (t) ? nc+j : no+j; name = SDL_GetAudioDeviceName(i, t); SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t); SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result, expected: NULL, got: %s", i, t, (name == NULL) ? "NULL" : name); } /* Capture index past capture range but within output range */ if ((no>0) && (no>nc) && (t==1)) { i = no-1; name = SDL_GetAudioDeviceName(i, t); SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t); SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result, expected: NULL, got: %s", i, t, (name == NULL) ? "NULL" : name); } } return TEST_COMPLETED; }
/** * @brief Prints available devices. */ static int audio_printDevices( int iscapture ) { int i, n; /* Get number of devices. */ n = SDL_GetNumAudioDevices(iscapture); SDL_ATprintVerbose( 1, "%d %s Audio Devices\n", n, iscapture ? "Capture" : "Output" ); /* List devices. */ for (i=0; i<n; i++) { SDL_ATprintVerbose( 1, " %d) %s\n", i+1, SDL_GetAudioDeviceName( i, iscapture ) ); } return 0; }
static qboolean QDECL SDL_Enumerate(void (QDECL *cb) (const char *drivername, const char *devicecode, const char *readablename)) { #if SDL_MAJOR_VERSION >= 2 int max, i; if(SSDL_InitAudio()) { max = SDL_GetNumAudioDevices(false); for (i = 0; i < max; i++) { const char *devname = SDL_GetAudioDeviceName(i, false); if (devname) cb(SDRVNAME, devname, va("SDL (%s)", devname)); } } return true; #else return false; #endif }
static void print_devices(int iscapture) { const char *typestr = ((iscapture) ? "capture" : "output"); int n = SDL_GetNumAudioDevices(iscapture); SDL_Log("%s devices:\n", typestr); if (n == -1) SDL_Log(" Driver can't detect specific %s devices.\n\n", typestr); else if (n == 0) SDL_Log(" No %s devices found.\n\n", typestr); else { int i; for (i = 0; i < n; i++) { SDL_Log(" %s\n", SDL_GetAudioDeviceName(i, iscapture)); } SDL_Log("\n"); } }
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); }
bool AudioSystem::VInitialize() { m_pMainChannel = new AudioMixer(); if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { LOG_ERR("AudioSystem > Failed to initialize SDL Audio system."); return false; } // list audio devices for (int i = 0; i < SDL_GetNumAudioDevices(0); ++i) { LOG("Audio Device ", i, ": ", SDL_GetAudioDeviceName(i, 0)); } // acquire the audio device and spec SDL_AudioSpec wanted; SDL_zero(wanted); wanted.freq = 48000; wanted.format = AUDIO_F32; wanted.channels = 2; wanted.samples = 4096; wanted.callback = EmptyAudioCallback; wanted.userdata = m_pMainChannel; m_audioDevID = SDL_OpenAudioDevice(nullptr, 0, &wanted, &m_audioSpec, SDL_AUDIO_ALLOW_ANY_CHANGE); if (m_audioDevID == 0) { LOG_ERR("AudioSystem failed to obtain audio device spec! ", SDL_GetError()); } else { // start playing audio SDL_PauseAudioDevice(m_audioDevID, 0); } return true; }
static void print_devices(int iscapture) { const char *typestr = ((iscapture) ? "capture" : "output"); int n = SDL_GetNumAudioDevices(iscapture); SDL_Log("Found %d %s device%s:\n", n, typestr, n != 1 ? "s" : ""); if (n == -1) SDL_Log(" Driver can't detect specific %s devices.\n\n", typestr); else if (n == 0) SDL_Log(" No %s devices found.\n\n", typestr); else { int i; for (i = 0; i < n; i++) { const char *name = SDL_GetAudioDeviceName(i, iscapture); if (name != NULL) SDL_Log(" %d: %s\n", i, name); else SDL_Log(" %d Error: %s\n", i, SDL_GetError()); } SDL_Log("\n"); } }
int main(int argc, char **argv) { /* (argv[1] == NULL means "open default device.") */ const char *devname = argv[1]; SDL_AudioSpec wanted; int devcount; int i; /* Enable standard application logging */ SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); /* Load the SDL library */ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); return (1); } window = SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0); renderer = SDL_CreateRenderer(window, -1, 0); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); SDL_RenderPresent(renderer); SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver()); devcount = SDL_GetNumAudioDevices(SDL_TRUE); for (i = 0; i < devcount; i++) { SDL_Log(" Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE)); } SDL_zero(wanted); wanted.freq = 44100; wanted.format = AUDIO_F32SYS; wanted.channels = 1; wanted.samples = 4096; wanted.callback = NULL; SDL_zero(spec); /* DirectSound can fail in some instances if you open the same hardware for both capture and output and didn't open the output end first, according to the docs, so if you're doing something like this, always open your capture devices second in case you land in those bizarre circumstances. */ SDL_Log("Opening default playback device...\n"); devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wanted, &spec, SDL_AUDIO_ALLOW_ANY_CHANGE); if (!devid_out) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for playback: %s!\n", SDL_GetError()); SDL_Quit(); exit(1); } SDL_Log("Opening capture device %s%s%s...\n", devname ? "'" : "", devname ? devname : "[[default]]", devname ? "'" : ""); devid_in = SDL_OpenAudioDevice(argv[1], SDL_TRUE, &spec, &spec, 0); if (!devid_in) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError()); SDL_Quit(); exit(1); } SDL_Log("Ready! Hold down mouse or finger to record!\n"); #ifdef __EMSCRIPTEN__ emscripten_set_main_loop(loop, 0, 1); #else while (1) { loop(); SDL_Delay(16); } #endif return 0; }
void init(void) { texturesList = createList(); musicList = createList(); fontList = createList(); black.r = 0; black.g = 0; black.b = 0; white.r = 255; white.g = 255; white.b = 255; // Initialize SDL2 if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { fprintf(stderr, "Unable to initialize SDL: %s \n", SDL_GetError()); quit(1); } // Display available audio device int count = SDL_GetNumAudioDevices(0), i; for (i = 0; i < count; ++i ) { fprintf(stderr, "Audio device %d: %s\n", i, SDL_GetAudioDeviceName(i, 0)); } // init sound int audio_rate = 22050; Uint16 audio_format = AUDIO_S16SYS; int nb_audio_channels = 4; int audio_buffers = 4096; if(Mix_OpenAudio(audio_rate, audio_format, nb_audio_channels, audio_buffers) != 0) { fprintf(stderr, "Unable to initialize audio: %s\n", Mix_GetError()); quit(1); } // Get desktop information if (SDL_GetDesktopDisplayMode(0, &displaymode) < 0) { fprintf(stderr, "Could not get display mode: %s\n", SDL_GetError()); quit(1); } viewport.x = 0; viewport.y = 0; viewport.w = MIN(displaymode.w, 800); viewport.h = MIN(displaymode.h, 600); // Create an application window with the following settings: window = SDL_CreateWindow( "Game example", // window title SDL_WINDOWPOS_UNDEFINED, // initial x destination SDL_WINDOWPOS_UNDEFINED, // initial y destination viewport.w, // width, in pixels viewport.h, // height, in pixels SDL_WINDOW_SHOWN // flags ); // Check that the window was successfully made if(window==NULL){ // In the event that the window could not be made... fprintf(stderr, "Could not create window: %s\n", SDL_GetError()); quit(1); } renderer = SDL_CreateRenderer(window, -1, 0); // SDL_RENDERER_PRESENTVSYNC if (renderer < 0) { fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError()); quit(1); } SDL_RenderGetViewport(renderer, &viewport); if (TTF_Init() == -1) { fprintf(stderr, "Unable to initialize SDL_ttf: %s \n", TTF_GetError()); quit(1); } }
int main(int argc, char *argv[]) { AVFormatContext *pFormatCtx = NULL; int i, videoStream, audioStream; AVCodecContext *pCodecCtx = NULL; AVCodec *pCodec = NULL; AVFrame *pFrame = NULL; AVPacket packet; int frameFinished; //float aspect_ratio; AVCodecContext *aCodecCtx = NULL; AVCodec *aCodec = NULL; //SDL_Overlay *bmp = NULL; //SDL_Surface *screen = NULL; SDL_Window *m_pWindow = NULL; SDL_Renderer *m_pRenderer = NULL; SDL_Rect rect; SDL_Event event; SDL_AudioSpec wanted_spec, spec; //struct SwsContext *sws_ctx = NULL; AVDictionary *videoOptionsDict = NULL; AVDictionary *audioOptionsDict = NULL; if(argc < 2) { fprintf(stderr, "Usage: test <file>\n"); exit(1); } // Register all formats and codecs av_register_all(); if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) { fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError()); exit(1); } // Open video file if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0) return -1; // Couldn't open file // Retrieve stream information if(avformat_find_stream_info(pFormatCtx, NULL)<0) return -1; // Couldn't find stream information // Dump information about file onto standard error av_dump_format(pFormatCtx, 0, argv[1], 0); // Find the first video stream videoStream=-1; audioStream=-1; for(i=0; i<pFormatCtx->nb_streams; i++) { if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO && videoStream < 0) { videoStream=i; // printf("video stream:%d",i); } if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO && audioStream < 0) { audioStream=i; // printf("audio stream:%d",i); } } // for(i=0; i<pFormatCtx->nb_streams; i++) { // if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) { // printf("video stream:%d\n",i); // } // if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO ) { // printf("audio stream:%d\n",i); // } // } if(videoStream==-1) return -1; // Didn't find a video stream if(audioStream==-1) return -1; aCodecCtx=pFormatCtx->streams[audioStream]->codec; int count = SDL_GetNumAudioDevices(0); for (int i = 0; i < count; ++i) { SDL_Log("Audio device %d: %s", i, SDL_GetAudioDeviceName(i, 0)); } // Set audio settings from codec info wanted_spec.freq = aCodecCtx->sample_rate; wanted_spec.format = AUDIO_S16SYS; wanted_spec.channels = aCodecCtx->channels; wanted_spec.silence = 0; wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE; wanted_spec.callback = audio_callback; wanted_spec.userdata = aCodecCtx; // if(SDL_OpenAudio(&wanted_spec, &spec) < 0) // { // fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError()); // return -1; // } SDL_AudioDeviceID dev; dev = SDL_OpenAudioDevice(NULL, 0, &wanted_spec, &spec, SDL_AUDIO_ALLOW_FORMAT_CHANGE); if(dev == 0) { fprintf(stderr, "Failed to open audio: %s\n", SDL_GetError()); } else { if(wanted_spec.format != spec.format){ fprintf(stderr, "We didn't get AUDIO_S16SYS audio format.\n"); return -1; } } aCodec = avcodec_find_decoder(aCodecCtx->codec_id); if(!aCodec) { fprintf(stderr, "Unsupported codec!\n"); return -1; } avcodec_open2(aCodecCtx, aCodec, &audioOptionsDict); // audio_st = pFormatCtx->streams[index] packet_queue_init(&audioq); //SDL_PauseAudio(0); SDL_PauseAudioDevice(dev,0); // Get a pointer to the codec context for the video stream pCodecCtx=pFormatCtx->streams[videoStream]->codec; // Find the decoder for the video stream pCodec=avcodec_find_decoder(pCodecCtx->codec_id); if(pCodec==NULL) { fprintf(stderr, "Unsupported codec!\n"); return -1; // Codec not found } // Open codec if(avcodec_open2(pCodecCtx, pCodec, &videoOptionsDict)<0) return -1; // Could not open codec // Allocate video frame pFrame=av_frame_alloc(); AVFrame* m_pFrameYUV = av_frame_alloc(); //int t_alloc_ret = av_image_alloc(m_pFrameYUV->data,m_pFrameYUV->linesize,pCodecCtx->width,pCodecCtx->height,AV_PIX_FMT_YUV420P,1); // int t_size0 = avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->coded_width, pCodecCtx->coded_height); // int t_size1 = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->coded_width, pCodecCtx->coded_height,1); //uint8_t * out_buffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->coded_width, pCodecCtx->coded_height)); uint8_t * out_buffer = (uint8_t *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)); //avpicture_fill((AVPicture *)m_pFrameYUV , out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->coded_width, pCodecCtx->coded_height); av_image_fill_arrays(m_pFrameYUV->data , m_pFrameYUV->linesize, out_buffer,AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height,1); struct SwsContext *img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,pCodecCtx->sw_pix_fmt, pCodecCtx->width, pCodecCtx->height,AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); // Make a screen to put our video //#ifndef __DARWIN__ // screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 0, 0); //#else // screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 24, 0); //#endif // // // if(!screen) { // fprintf(stderr, "SDL: could not set video mode - exiting\n"); // exit(1); // } // Allocate a place to put our YUV image on that screen // bmp = SDL_CreateYUVOverlay(pCodecCtx->width, // pCodecCtx->height, // SDL_YV12_OVERLAY, // screen); // Make a screen to put our video m_pWindow = SDL_CreateWindow("test windows", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,pCodecCtx->width, pCodecCtx->height,SDL_WINDOW_SHOWN); if(!m_pWindow) { printf("SDL: could not create window - exiting:%s\n",SDL_GetError()); return -1; } m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0); SDL_RenderClear(m_pRenderer); SDL_Texture *m_pSdlTexture = SDL_CreateTexture(m_pRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height); rect.x = 0; rect.y = 0; rect.w = pCodecCtx->width; rect.h = pCodecCtx->height; // Read frames and save first five frames to disk i=0; while(av_read_frame(pFormatCtx, &packet)>=0) { // Is this a packet from the video stream? if(packet.stream_index==videoStream) { // Decode video frame avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet); // Did we get a video frame? if(frameFinished) { //SDL_LockTexture(m_pSdlTexture, &rect, m_pFrameYUV->data, m_pFrameYUV->linesize); //SDL_LockYUVOverlay(bmp); // AVPicture pict; // pict.data[0] = bmp->pixels[0]; // pict.data[1] = bmp->pixels[2]; // pict.data[2] = bmp->pixels[1]; // // pict.linesize[0] = bmp->pitches[0]; // pict.linesize[1] = bmp->pitches[2]; // pict.linesize[2] = bmp->pitches[1]; // Convert the image into YUV format that SDL uses // sws_scale // ( // sws_ctx, // (uint8_t const * const *)pFrame->data, // pFrame->linesize, // 0, // pCodecCtx->height, // pict.data, // pict.linesize // ); sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, m_pFrameYUV->data, m_pFrameYUV->linesize); //SDL_UnlockYUVOverlay(bmp); // SDL_UnlockTexture(m_pSdlTexture); SDL_UpdateYUVTexture(m_pSdlTexture, &rect, m_pFrameYUV->data[0], m_pFrameYUV->linesize[0], m_pFrameYUV->data[1], m_pFrameYUV->linesize[1], m_pFrameYUV->data[2], m_pFrameYUV->linesize[2]); // rect.x = 0; // rect.y = 0; // rect.w = pCodecCtx->width; // rect.h = pCodecCtx->height; //SDL_DisplayYUVOverlay(bmp, &rect); SDL_RenderClear( m_pRenderer );//this line seems nothing to do SDL_RenderCopy( m_pRenderer, m_pSdlTexture, NULL, &rect); SDL_RenderPresent(m_pRenderer); SDL_Delay(38); // av_free_packet(&packet); av_packet_unref(&packet); } } else if(packet.stream_index==audioStream) { packet_queue_put(&audioq, &packet); } else { // av_free_packet(&packet); av_packet_unref(&packet); } // Free the packet that was allocated by av_read_frame SDL_PollEvent(&event); switch(event.type) { case SDL_QUIT: quit = 1; SDL_Quit(); exit(0); break; default: break; } } // Free the YUV frame av_free(pFrame); // Close the codec avcodec_close(pCodecCtx); // Close the video file avformat_close_input(&pFormatCtx); return 0; }
/* =============== SNDDMA_Init =============== */ qboolean SNDDMA_Init(void) { SDL_AudioSpec desired; SDL_AudioSpec obtained; const char *driver_name; const char *device_name; int tmp; if (snd_inited) { return qtrue; } Cmd_AddCommand("sndlist", SND_DeviceList); // before rc 2 we have had own s_sdl_ cvars to set this // changed back to use genuine cvar names // - it's more compatible when existing profiles are used // - we don't have to touch menu & ui to make speed/khz work (uses s_khz!) s_bits = Cvar_Get("s_bits", "16", CVAR_LATCH | CVAR_ARCHIVE); s_khz = Cvar_Get("s_khz", "44", CVAR_LATCH | CVAR_ARCHIVE); s_sdlChannels = Cvar_Get("s_channels", "2", CVAR_LATCH | CVAR_ARCHIVE); s_sdlDevSamps = Cvar_Get("s_sdlDevSamps", "0", CVAR_LATCH | CVAR_ARCHIVE); s_sdlMixSamps = Cvar_Get("s_sdlMixSamps", "0", CVAR_LATCH | CVAR_ARCHIVE); s_device = Cvar_Get("s_device", "-1", CVAR_LATCH | CVAR_ARCHIVE); Com_Printf("SDL_Init( SDL_INIT_AUDIO )... "); if (!SDL_WasInit(SDL_INIT_AUDIO)) { if (SDL_Init(SDL_INIT_AUDIO) < 0) { Com_Printf("FAILED (%s)\n", SDL_GetError()); return qfalse; } } Com_Printf("OK\n"); driver_name = SDL_GetCurrentAudioDriver(); if (driver_name) { Com_Printf("SDL audio driver is \"%s\".\n", driver_name); } else { Com_Printf("SDL audio driver isn't initialized.\n"); } memset(&desired, '\0', sizeof(desired)); memset(&obtained, '\0', sizeof(obtained)); tmp = ((int) s_bits->value); if ((tmp != 16) && (tmp != 8)) { tmp = 16; } desired.freq = (int) s_khz->value * 1000; // desired freq expects Hz not kHz if (!desired.freq) { desired.freq = 22050; } // dirty correction for profile values if (desired.freq == 11000) { desired.freq = 11025; } else if (desired.freq == 22000) { desired.freq = 22050; } else if (desired.freq == 44000) { desired.freq = 44100; } else { desired.freq = 22050; } desired.format = ((tmp == 16) ? AUDIO_S16SYS : AUDIO_U8); // I dunno if this is the best idea, but I'll give it a try... // should probably check a cvar for this... if (s_sdlDevSamps->value) { desired.samples = s_sdlDevSamps->value; } else { // just pick a sane default. if (desired.freq <= 11025) { desired.samples = 256; } else if (desired.freq <= 22050) { desired.samples = 512; } else if (desired.freq <= 44100) { desired.samples = 1024; } else { desired.samples = 2048; // (*shrug*) } } desired.channels = (int) s_sdlChannels->value; desired.callback = SNDDMA_AudioCallback; if (s_device->integer >= 0 && s_device->integer < SDL_GetNumAudioDevices(qfalse)) { device_name = SDL_GetAudioDeviceName(s_device->integer, 0); Com_Printf("Acquiring audio device: %s\n", device_name); } else { device_name = NULL; //Reset the cvar just in case Cvar_Set("s_device", "-1"); } device_id = SDL_OpenAudioDevice(device_name, qfalse, &desired, &obtained, SDL_AUDIO_ALLOW_FORMAT_CHANGE); if(device_id == 0) { Com_Printf("SDL_OpenAudioDevice() failed: %s\n", SDL_GetError()); SDL_QuitSubSystem(SDL_INIT_AUDIO); return qfalse; } SNDDMA_PrintAudiospec("SDL_AudioSpec", &obtained); // dma.samples needs to be big, or id's mixer will just refuse to // work at all; we need to keep it significantly bigger than the // amount of SDL callback samples, and just copy a little each time // the callback runs. // 32768 is what the OSS driver filled in here on my system. I don't // know if it's a good value overall, but at least we know it's // reasonable...this is why I let the user override. tmp = s_sdlMixSamps->value; if (!tmp) { tmp = (obtained.samples * obtained.channels) * 10; } if (tmp & (tmp - 1)) // not a power of two? Seems to confuse something. { int val = 1; while (val < tmp) val <<= 1; tmp = val; } dmapos = 0; dma.samplebits = obtained.format & 0xFF; // first byte of format is bits. dma.channels = obtained.channels; dma.samples = tmp; dma.submission_chunk = 1; dma.speed = obtained.freq; dmasize = (dma.samples * (dma.samplebits / 8)); dma.buffer = calloc(1, dmasize); if (!dma.buffer) { Com_Printf("Unable to allocate dma buffer\n"); SDL_QuitSubSystem(SDL_INIT_AUDIO); return qfalse; } Com_Printf("Starting SDL audio callback...\n"); SDL_PauseAudioDevice(device_id, 0); // start callback. Com_Printf("SDL audio initialized.\n"); snd_inited = qtrue; return qtrue; }