summaryrefslogtreecommitdiffstats
path: root/src/sound.c
blob: 9cdb75e6842e300a2981ab1807c68241628a34b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "sound.h"
#include <portaudio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.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;
    }
    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 (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();
}