summaryrefslogblamecommitdiffstats
path: root/src/sound.c
blob: 04b3f9b5db524e6727be34b58ad2b96d252733a1 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                  
                      




                                 
                  

 

                                   

                  















                                             
 



                  
        
























                                                                                  

                                           



                                             
                                                                                           
                                                                                                                             
                                                                                     

                              
   
  








                                                                                                           








                                                                                                                            
 



                                                                                                 

                                                               

                          








                                  
#include "sound.h"
#include <portaudio.h>


static void
StreamFinished( void* synthData )
{
  (void)synthData;
}

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) {
      continue;
    }
    if (!strcmp(name, deviceInfo->name)) {
      Pa_Terminate();
      return c;
    }
    c++;
  }
  Pa_Terminate();
  return -1;
}

char *
get_soundcards()
{
  Pa_Initialize();
  int i;
  const PaDeviceInfo *deviceInfo;
  char *ret = (char *)malloc(sizeof(char) * 4096);
  strcpy(ret, "");
  for( i=0; i< Pa_GetDeviceCount(); i++ ) {
    deviceInfo = Pa_GetDeviceInfo(i);
    if (deviceInfo->maxOutputChannels == 0) {
      continue;
    }
    strcat(ret, deviceInfo->name);
    strcat(ret, ";");
  }
  ret[strlen(ret) - 1] = '\0';
  Pa_Terminate();

  return ret;
}

void
init_sound(synth_t * synth, PaStreamCallback *streamCallback, const int device_id)
{
  printf("Before\n");
  Pa_Initialize();
  printf("after init\n");

  int i, c=0;
  const   PaDeviceInfo *deviceInfo;
  for( i=0; i< Pa_GetDeviceCount(); i++ ) {
    deviceInfo = Pa_GetDeviceInfo(i);
    if (deviceInfo->maxOutputChannels == 0) {
      continue;
    }
    //if (!strcmp("HyperX Cloud II Wireless: USB Audio (hw:2,0)", deviceInfo->name)) break;
    printf("dev: %s || %f || %d channels\n", deviceInfo->name, deviceInfo->defaultSampleRate, deviceInfo->maxOutputChannels);
    //if (!strcmp("HDA Intel PCH: ALC1220 Analog (hw:0,0)", deviceInfo->name)) break;
    if (c == device_id) break;
    c++;
  }
  

  PaStreamParameters outputParameters;
  outputParameters.device = i; Pa_GetDefaultOutputDevice(); /* default output device */
  printf("-------\nSelected device: %s\n-------\n", Pa_GetDeviceInfo(outputParameters.device)->name);
  outputParameters.channelCount = 2;       /* stereo output */
  outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
  outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
  outputParameters.hostApiSpecificStreamInfo = NULL;

  PaError err;
  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!!!!!", Pa_GetDeviceInfo(outputParameters.device)->name);
  }
  
  Pa_SetStreamFinishedCallback(synth->stream, &StreamFinished);
  Pa_StartStream(synth->stream);

  synth->sound_active = 1;
}

void
destroy_sound(synth_t * synth)
{
  Pa_StopStream( synth->stream );
  Pa_CloseStream( synth->stream );
  Pa_Terminate();
}