#include "sound.h" #include #include #include #include static void StreamFinished( void* synthData ) { (void)synthData; } // unused int get_soundcard_id(const char * name) { Pa_Initialize(); int i, c=0; const PaDeviceInfo *deviceInfo; for( i=0; i< Pa_GetDeviceCount(); i++ ) { deviceInfo = Pa_GetDeviceInfo(i); if (deviceInfo->maxOutputChannels == 0 || deviceInfo->defaultLowInputLatency < 0) { continue; } if (!strcmp(name, deviceInfo->name)) { Pa_Terminate(); return c; } c++; } Pa_Terminate(); return -1; } char * get_soundcards() { int i; const PaDeviceInfo *deviceInfo; char *ret = (char *)malloc(sizeof(char) * 4096); strcpy(ret, "Select Output Device;"); for( i=0; i< Pa_GetDeviceCount(); i++ ) { deviceInfo = Pa_GetDeviceInfo(i); if (deviceInfo->maxOutputChannels == 0 || deviceInfo->defaultLowInputLatency < 0) { continue; } strcat(ret, deviceInfo->name); strcat(ret, ";"); } ret[strlen(ret) - 1] = '\0'; return ret; } void init_sound(synth_t * synth, PaStreamCallback *streamCallback, const char* device_name) { printf("Will try to initialized pulseaudion device [ %s ]\n", device_name); freopen("/dev/null","w",stderr); PaError err = Pa_Initialize(); freopen("/dev/tty", "w", stderr); if (err != paNoError) { fprintf(stderr, "PortAudio error: %s\n", Pa_GetErrorText(err)); } int i=0; const PaDeviceInfo *deviceInfo; for( i=0; i< Pa_GetDeviceCount(); i++ ) { deviceInfo = Pa_GetDeviceInfo(i); if (deviceInfo->maxOutputChannels == 0 || deviceInfo->defaultLowInputLatency < 0) { continue; } /* printf("dev: %s || %f || id:%d out:%d || lil:%f lol:%f\n", deviceInfo->name, */ /* deviceInfo->defaultSampleRate, deviceInfo->maxInputChannels, */ /* deviceInfo->maxOutputChannels, deviceInfo->defaultLowInputLatency, */ /* deviceInfo->defaultLowOutputLatency); */ if (!strcmp(deviceInfo->name, device_name)) break; } PaStreamParameters outputParameters; outputParameters.device = i;// Pa_GetDefaultOutputDevice(); /* default output device */ printf("Selected device: dev: %s || %f || id:%d out:%d || lil:%f lol:%f\n", deviceInfo->name, deviceInfo->defaultSampleRate, deviceInfo->maxInputChannels, deviceInfo->maxOutputChannels, deviceInfo->defaultLowInputLatency, deviceInfo->defaultLowOutputLatency); outputParameters.channelCount = 2; /* stereo output */ outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ outputParameters.suggestedLatency = deviceInfo->defaultLowOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; err = Pa_OpenStream(&(synth->stream), NULL, /* no input */ &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff | paDitherOff, /* we won't output out of range samples so don't bother clipping them */ streamCallback, synth ); if (err != paNoError) { printf("Error opening stream with %s!!!!!\n", deviceInfo->name); if (strcmp(device_name, "default")) { printf("Trying to use default device\n"); strcpy(synth->soundcard.name, "default"); init_sound(synth, streamCallback, synth->soundcard.name); } return; } Pa_SetStreamFinishedCallback(synth->stream, &StreamFinished); Pa_StartStream(synth->stream); synth->sound_active = 1; } void destroy_sound(synth_t * synth) { synth->sound_active = 0; Pa_StopStream( synth->stream ); Pa_CloseStream( synth->stream ); PaError err = Pa_Terminate(); if( err != paNoError ) fprintf(stderr, "PortAudio error: %s\n", Pa_GetErrorText( err ) ); }