mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-02-24 16:16:10 +08:00
ggwave-gui : attempt to reinit playback/capture
This commit is contained in:
@@ -20,11 +20,12 @@ namespace {
|
||||
|
||||
std::string g_defaultCaptureDeviceName = "";
|
||||
|
||||
bool g_isInitialized = false;
|
||||
|
||||
SDL_AudioDeviceID g_devIdIn = 0;
|
||||
SDL_AudioDeviceID g_devIdOut = 0;
|
||||
|
||||
SDL_AudioSpec g_obtainedSpecIn;
|
||||
SDL_AudioSpec g_obtainedSpecOut;
|
||||
|
||||
GGWave *g_ggWave = nullptr;
|
||||
|
||||
}
|
||||
@@ -77,122 +78,120 @@ void GGWave_setDefaultCaptureDeviceName(std::string name) {
|
||||
bool GGWave_init(
|
||||
const int playbackId,
|
||||
const int captureId) {
|
||||
if (g_isInitialized) {
|
||||
|
||||
if (g_devIdIn && g_devIdOut) {
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("Initializing ...\n");
|
||||
if (g_devIdIn == 0 && g_devIdOut == 0) {
|
||||
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
||||
|
||||
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
||||
|
||||
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
SDL_SetHintWithPriority(SDL_HINT_AUDIO_RESAMPLING_MODE, "medium", SDL_HINT_OVERRIDE);
|
||||
|
||||
{
|
||||
int nDevices = SDL_GetNumAudioDevices(SDL_FALSE);
|
||||
printf("Found %d playback devices:\n", nDevices);
|
||||
for (int i = 0; i < nDevices; i++) {
|
||||
printf(" - Playback device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_FALSE));
|
||||
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
{
|
||||
int nDevices = SDL_GetNumAudioDevices(SDL_TRUE);
|
||||
printf("Found %d capture devices:\n", nDevices);
|
||||
for (int i = 0; i < nDevices; i++) {
|
||||
printf(" - Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
|
||||
|
||||
SDL_SetHintWithPriority(SDL_HINT_AUDIO_RESAMPLING_MODE, "medium", SDL_HINT_OVERRIDE);
|
||||
|
||||
{
|
||||
int nDevices = SDL_GetNumAudioDevices(SDL_FALSE);
|
||||
printf("Found %d playback devices:\n", nDevices);
|
||||
for (int i = 0; i < nDevices; i++) {
|
||||
printf(" - Playback device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_FALSE));
|
||||
}
|
||||
}
|
||||
{
|
||||
int nDevices = SDL_GetNumAudioDevices(SDL_TRUE);
|
||||
printf("Found %d capture devices:\n", nDevices);
|
||||
for (int i = 0; i < nDevices; i++) {
|
||||
printf(" - Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SDL_AudioSpec playbackSpec;
|
||||
SDL_zero(playbackSpec);
|
||||
bool reinit = false;
|
||||
|
||||
playbackSpec.freq = ::kBaseSampleRate;
|
||||
playbackSpec.format = AUDIO_S16SYS;
|
||||
playbackSpec.channels = 1;
|
||||
playbackSpec.samples = 16*1024;
|
||||
playbackSpec.callback = NULL;
|
||||
if (g_devIdOut == 0) {
|
||||
printf("Initializing playback ...\n");
|
||||
|
||||
SDL_AudioSpec obtainedSpecIn;
|
||||
SDL_AudioSpec obtainedSpecOut;
|
||||
SDL_AudioSpec playbackSpec;
|
||||
SDL_zero(playbackSpec);
|
||||
|
||||
playbackSpec.freq = ::kBaseSampleRate;
|
||||
playbackSpec.format = AUDIO_S16SYS;
|
||||
playbackSpec.channels = 1;
|
||||
playbackSpec.samples = 16*1024;
|
||||
playbackSpec.callback = NULL;
|
||||
|
||||
SDL_zero(g_obtainedSpecOut);
|
||||
|
||||
if (playbackId >= 0) {
|
||||
printf("Attempt to open playback device %d : '%s' ...\n", playbackId, SDL_GetAudioDeviceName(playbackId, SDL_FALSE));
|
||||
g_devIdOut = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(playbackId, SDL_FALSE), SDL_FALSE, &playbackSpec, &g_obtainedSpecOut, 0);
|
||||
} else {
|
||||
printf("Attempt to open default playback device ...\n");
|
||||
g_devIdOut = SDL_OpenAudioDevice(NULL, SDL_FALSE, &playbackSpec, &g_obtainedSpecOut, 0);
|
||||
}
|
||||
|
||||
if (!g_devIdOut) {
|
||||
printf("Couldn't open an audio device for playback: %s!\n", SDL_GetError());
|
||||
g_devIdOut = 0;
|
||||
} else {
|
||||
printf("Obtained spec for output device (SDL Id = %d):\n", g_devIdOut);
|
||||
printf(" - Sample rate: %d (required: %d)\n", g_obtainedSpecOut.freq, playbackSpec.freq);
|
||||
printf(" - Format: %d (required: %d)\n", g_obtainedSpecOut.format, playbackSpec.format);
|
||||
printf(" - Channels: %d (required: %d)\n", g_obtainedSpecOut.channels, playbackSpec.channels);
|
||||
printf(" - Samples per frame: %d (required: %d)\n", g_obtainedSpecOut.samples, playbackSpec.samples);
|
||||
|
||||
if (g_obtainedSpecOut.format != playbackSpec.format ||
|
||||
g_obtainedSpecOut.channels != playbackSpec.channels ||
|
||||
g_obtainedSpecOut.samples != playbackSpec.samples) {
|
||||
g_devIdOut = 0;
|
||||
SDL_CloseAudio();
|
||||
fprintf(stderr, "Failed to initialize playback SDL_OpenAudio!");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
reinit = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (g_devIdIn == 0) {
|
||||
SDL_AudioSpec captureSpec;
|
||||
captureSpec = g_obtainedSpecOut;
|
||||
captureSpec.freq = ::kBaseSampleRate;
|
||||
captureSpec.format = AUDIO_F32SYS;
|
||||
captureSpec.samples = 4096;
|
||||
|
||||
SDL_zero(g_obtainedSpecIn);
|
||||
|
||||
if (captureId >= 0) {
|
||||
printf("Attempt to open capture device %d : '%s' ...\n", captureId, SDL_GetAudioDeviceName(captureId, SDL_FALSE));
|
||||
g_devIdIn = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(captureId, SDL_TRUE), SDL_TRUE, &captureSpec, &g_obtainedSpecIn, 0);
|
||||
} else {
|
||||
printf("Attempt to open default capture device ...\n");
|
||||
g_devIdIn = SDL_OpenAudioDevice(g_defaultCaptureDeviceName.empty() ? nullptr : g_defaultCaptureDeviceName.c_str(),
|
||||
SDL_TRUE, &captureSpec, &g_obtainedSpecIn, 0);
|
||||
}
|
||||
if (!g_devIdIn) {
|
||||
printf("Couldn't open an audio device for capture: %s!\n", SDL_GetError());
|
||||
g_devIdIn = 0;
|
||||
} else {
|
||||
printf("Obtained spec for input device (SDL Id = %d):\n", g_devIdIn);
|
||||
printf(" - Sample rate: %d\n", g_obtainedSpecIn.freq);
|
||||
printf(" - Format: %d (required: %d)\n", g_obtainedSpecIn.format, captureSpec.format);
|
||||
printf(" - Channels: %d (required: %d)\n", g_obtainedSpecIn.channels, captureSpec.channels);
|
||||
printf(" - Samples per frame: %d\n", g_obtainedSpecIn.samples);
|
||||
|
||||
reinit = true;
|
||||
}
|
||||
}
|
||||
|
||||
int sampleSizeBytesIn = 4;
|
||||
int sampleSizeBytesOut = 2;
|
||||
|
||||
SDL_zero(obtainedSpecIn);
|
||||
SDL_zero(obtainedSpecOut);
|
||||
|
||||
if (playbackId >= 0) {
|
||||
printf("Attempt to open playback device %d : '%s' ...\n", playbackId, SDL_GetAudioDeviceName(playbackId, SDL_FALSE));
|
||||
g_devIdOut = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(playbackId, SDL_FALSE), SDL_FALSE, &playbackSpec, &obtainedSpecOut, 0);
|
||||
} else {
|
||||
printf("Attempt to open default playback device ...\n");
|
||||
g_devIdOut = SDL_OpenAudioDevice(NULL, SDL_FALSE, &playbackSpec, &obtainedSpecOut, 0);
|
||||
}
|
||||
|
||||
if (!g_devIdOut) {
|
||||
printf("Couldn't open an audio device for playback: %s!\n", SDL_GetError());
|
||||
g_devIdOut = 0;
|
||||
} else {
|
||||
printf("Obtained spec for output device (SDL Id = %d):\n", g_devIdOut);
|
||||
printf(" - Sample rate: %d (required: %d)\n", obtainedSpecOut.freq, playbackSpec.freq);
|
||||
printf(" - Format: %d (required: %d)\n", obtainedSpecOut.format, playbackSpec.format);
|
||||
printf(" - Channels: %d (required: %d)\n", obtainedSpecOut.channels, playbackSpec.channels);
|
||||
printf(" - Samples per frame: %d (required: %d)\n", obtainedSpecOut.samples, playbackSpec.samples);
|
||||
|
||||
if (obtainedSpecOut.format != playbackSpec.format ||
|
||||
obtainedSpecOut.channels != playbackSpec.channels ||
|
||||
obtainedSpecOut.samples != playbackSpec.samples) {
|
||||
SDL_CloseAudio();
|
||||
fprintf(stderr, "Failed to initialize playback SDL_OpenAudio!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
switch (obtainedSpecOut.format) {
|
||||
case AUDIO_U8:
|
||||
case AUDIO_S8:
|
||||
sampleSizeBytesOut = 1;
|
||||
break;
|
||||
case AUDIO_U16SYS:
|
||||
case AUDIO_S16SYS:
|
||||
sampleSizeBytesOut = 2;
|
||||
break;
|
||||
case AUDIO_S32SYS:
|
||||
case AUDIO_F32SYS:
|
||||
sampleSizeBytesOut = 4;
|
||||
break;
|
||||
}
|
||||
|
||||
SDL_AudioSpec captureSpec;
|
||||
captureSpec = obtainedSpecOut;
|
||||
captureSpec.freq = ::kBaseSampleRate;
|
||||
captureSpec.format = AUDIO_F32SYS;
|
||||
captureSpec.samples = 4096;
|
||||
|
||||
if (captureId >= 0) {
|
||||
printf("Attempt to open capture device %d : '%s' ...\n", captureId, SDL_GetAudioDeviceName(captureId, SDL_FALSE));
|
||||
g_devIdIn = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(captureId, SDL_TRUE), SDL_TRUE, &captureSpec, &obtainedSpecIn, 0);
|
||||
} else {
|
||||
printf("Attempt to open default capture device ...\n");
|
||||
g_devIdIn = SDL_OpenAudioDevice(g_defaultCaptureDeviceName.empty() ? nullptr : g_defaultCaptureDeviceName.c_str(),
|
||||
SDL_TRUE, &captureSpec, &obtainedSpecIn, 0);
|
||||
}
|
||||
if (!g_devIdIn) {
|
||||
printf("Couldn't open an audio device for capture: %s!\n", SDL_GetError());
|
||||
g_devIdIn = 0;
|
||||
} else {
|
||||
printf("Obtained spec for input device (SDL Id = %d):\n", g_devIdIn);
|
||||
printf(" - Sample rate: %d\n", obtainedSpecIn.freq);
|
||||
printf(" - Format: %d (required: %d)\n", obtainedSpecIn.format, captureSpec.format);
|
||||
printf(" - Channels: %d (required: %d)\n", obtainedSpecIn.channels, captureSpec.channels);
|
||||
printf(" - Samples per frame: %d\n", obtainedSpecIn.samples);
|
||||
}
|
||||
|
||||
switch (obtainedSpecIn.format) {
|
||||
switch (g_obtainedSpecIn.format) {
|
||||
case AUDIO_U8:
|
||||
case AUDIO_S8:
|
||||
sampleSizeBytesIn = 1;
|
||||
@@ -207,14 +206,31 @@ bool GGWave_init(
|
||||
break;
|
||||
}
|
||||
|
||||
g_ggWave = new GGWave(
|
||||
obtainedSpecIn.freq,
|
||||
obtainedSpecOut.freq,
|
||||
1024,
|
||||
sampleSizeBytesIn,
|
||||
sampleSizeBytesOut);
|
||||
switch (g_obtainedSpecOut.format) {
|
||||
case AUDIO_U8:
|
||||
case AUDIO_S8:
|
||||
sampleSizeBytesOut = 1;
|
||||
break;
|
||||
case AUDIO_U16SYS:
|
||||
case AUDIO_S16SYS:
|
||||
sampleSizeBytesOut = 2;
|
||||
break;
|
||||
case AUDIO_S32SYS:
|
||||
case AUDIO_F32SYS:
|
||||
sampleSizeBytesOut = 4;
|
||||
break;
|
||||
}
|
||||
|
||||
g_isInitialized = true;
|
||||
if (reinit) {
|
||||
if (g_ggWave) delete g_ggWave;
|
||||
|
||||
g_ggWave = new GGWave(
|
||||
g_obtainedSpecIn.freq,
|
||||
g_obtainedSpecOut.freq,
|
||||
1024,
|
||||
sampleSizeBytesIn,
|
||||
sampleSizeBytesOut);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -222,7 +238,7 @@ bool GGWave_init(
|
||||
GGWave * GGWave_instance() { return g_ggWave; }
|
||||
|
||||
bool GGWave_mainLoop() {
|
||||
if (g_isInitialized == false) {
|
||||
if (g_devIdIn == 0 && g_devIdOut == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -264,7 +280,7 @@ bool GGWave_mainLoop() {
|
||||
}
|
||||
|
||||
bool GGWave_deinit() {
|
||||
if (g_isInitialized == false) {
|
||||
if (g_devIdIn == 0 && g_devIdOut == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user