Update dsd.h, dsd_audio.c, dsd_main.c and dsd_symbol.c

Adding portaudio support. Implementing it as a new audio_..._type in dsd.h.

Update CMakeLists.txt and create FindLibPortAudio.cmake

CMake portaudio check
This commit is contained in:
jpwichern
2014-06-05 19:00:39 +02:00
committed by Jiri Wichern
parent df3c0dcab4
commit 55b190ef49
6 changed files with 381 additions and 14 deletions

View File

@@ -17,6 +17,7 @@
#include "dsd.h"
void
processAudio (dsd_opts * opts, dsd_state * state)
{
@@ -213,16 +214,65 @@ writeSynthesizedVoice (dsd_opts * opts, dsd_state * state)
*/
}
void
playSynthesizedVoice (dsd_opts * opts, dsd_state * state)
{
ssize_t result;
if (state->audio_out_idx > opts->delay)
{
// output synthesized speech to sound card
result = write (opts->audio_out_fd, (state->audio_out_buf_p - state->audio_out_idx), (state->audio_out_idx * 2));
if(opts->audio_out_type == 2)
{
#ifdef USE_PORTAUDIO
PaError err = paNoError;
do
{
long available = Pa_GetStreamWriteAvailable( opts->audio_out_pa_stream );
if(available < 0)
err = available;
//printf("Frames available: %d\n", available);
if( err != paNoError )
break;
if(available > SAMPLE_RATE_OUT / 2)
{
printf("\nSyncing voice output stream\n");
err = Pa_StopStream( opts->audio_out_pa_stream );
if( err != paNoError )
break;
}
err = Pa_IsStreamActive( opts->audio_out_pa_stream );
if(err == 0)
{
printf("Start voice output stream\n");
err = Pa_StartStream( opts->audio_out_pa_stream );
}
else if(err == 1)
{
err = paNoError;
}
if( err != paNoError )
break;
//printf("write stream %d\n", state->audio_out_idx);
err = Pa_WriteStream( opts->audio_out_pa_stream, (state->audio_out_buf_p - state->audio_out_idx), state->audio_out_idx );
if( err != paNoError )
break;
} while(0);
if( err != paNoError )
{
fprintf( stderr, "An error occured while using the portaudio output stream\n" );
fprintf( stderr, "Error number: %d\n", err );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
}
#endif
}
else
result = write (opts->audio_out_fd, (state->audio_out_buf_p - state->audio_out_idx), (state->audio_out_idx * 2));
state->audio_out_idx = 0;
}
@@ -236,11 +286,99 @@ playSynthesizedVoice (dsd_opts * opts, dsd_state * state)
}
}
#ifdef USE_PORTAUDIO
int getPADevice(char* dev, int input, PaStream** stream)
{
int devnum = atoi(dev + 3);
printf("Using portaudio device %d.\n", devnum);
PaError err;
int numDevices = Pa_GetDeviceCount();
if( numDevices < 0 )
{
printf( "ERROR: Pa_GetDeviceCount returned 0x%x\n", numDevices );
err = numDevices;
goto error;
}
if( devnum >= numDevices)
{
printf( "ERROR: Requested device %d is larger than number of devices.\n", devnum );
return(1);
}
const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo( devnum );
/* print device name */
#ifdef WIN32
{ /* Use wide char on windows, so we can show UTF-8 encoded device names */
wchar_t wideName[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, deviceInfo->name, -1, wideName, MAX_PATH-1);
wprintf( L"Name = %s\n", wideName );
}
#else
printf( "Name = %s\n", deviceInfo->name );
#endif
if((input == 1) && (deviceInfo->maxInputChannels == 0))
{
printf( "ERROR: Requested device %d is not an input device.\n", devnum );
return(1);
}
if((input == 0) && (deviceInfo->maxOutputChannels == 0))
{
printf( "ERROR: Requested device %d is not an output device.\n", devnum );
return(1);
}
//Create stream parameters
PaStreamParameters parameters;
parameters.device = devnum;
parameters.channelCount = 1; /* mono */
parameters.sampleFormat = paInt16; //Shorts
parameters.suggestedLatency = 0.500; //0.050; // Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
parameters.hostApiSpecificStreamInfo = NULL;
//Open stream
err = Pa_OpenStream(
stream,
(input == 1) ? &parameters : NULL,
(input == 0) ? &parameters : NULL,
(input == 1) ? SAMPLE_RATE_IN : SAMPLE_RATE_OUT,
PA_FRAMES_PER_BUFFER,
paClipOff,
NULL /*callback*/,
NULL );
if( err != paNoError ) goto error;
return 0;
error:
fprintf( stderr, "An error occured while initializing a portaudio stream\n" );
fprintf( stderr, "Error number: %d\n", err );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
return err;
}
#endif
void
openAudioOutDevice (dsd_opts * opts, int speed)
{
// get info of device/file
struct stat stat_buf;
if(strncmp(opts->audio_in_dev, "pa:", 3) == 0)
{
opts->audio_out_type = 2;
#ifdef USE_PORTAUDIO
int err = getPADevice(opts->audio_out_dev, 0, &opts->audio_out_pa_stream);
if(err != 0)
exit(err);
#else
printf("Error, Portaudio support not compiled.\n");
exit(1);
#endif
}
else
{
struct stat stat_buf;
if(stat(opts->audio_out_dev, &stat_buf) != 0)
{
printf("Error, couldn't open %s\n", opts->audio_out_dev);
@@ -316,6 +454,7 @@ openAudioOutDevice (dsd_opts * opts, int speed)
}
#endif
}
printf ("Audio Out Device: %s\n", opts->audio_out_dev);
}
@@ -323,6 +462,28 @@ void
openAudioInDevice (dsd_opts * opts)
{
// get info of device/file
if(strncmp(opts->audio_in_dev, "pa:", 2) == 0)
{
opts->audio_in_type = 2;
#ifdef USE_PORTAUDIO
int err = getPADevice(opts->audio_in_dev, 1, &opts->audio_in_pa_stream);
if(err != 0)
exit(err);
if (opts->split == 0)
{
int err = getPADevice(opts->audio_in_dev, 0, &opts->audio_out_pa_stream);
if(err != 0)
exit(err);
}
#else
printf("Error, Portaudio support not compiled.\n");
exit(1);
#endif
}
else
{
struct stat stat_buf;
if (stat(opts->audio_in_dev, &stat_buf) != 0)
{
@@ -370,8 +531,8 @@ openAudioInDevice (dsd_opts * opts)
// get current
ioctl (opts->audio_in_fd, AUDIO_GETINFO, &aset);
aset.record.sample_rate = 48000;
aset.play.sample_rate = 48000;
aset.record.sample_rate = SAMPLE_RATE_IN;
aset.play.sample_rate = SAMPLE_RATE_IN;
aset.record.channels = 1;
aset.play.channels = 1;
aset.record.precision = 16;
@@ -411,7 +572,7 @@ openAudioInDevice (dsd_opts * opts)
{
printf ("ioctl reset error \n");
}
fmt = 48000;
fmt = SAMPLE_RATE_IN;
if (ioctl (opts->audio_in_fd, SNDCTL_DSP_SPEED, &fmt) < 0)
{
printf ("ioctl speed error \n");
@@ -428,6 +589,8 @@ openAudioInDevice (dsd_opts * opts)
}
#endif
}
}
if (opts->split == 1)
{
printf ("Audio In Device: %s\n", opts->audio_in_dev);