summaryrefslogtreecommitdiffstats
path: root/src/sound.c
blob: d4b0242c25fa38d3c8a059f53ac96be84eafa51d (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
115
116
117
118
119
120
121
122
123
124
125
#include "sound.h"
#include <portaudio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


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);
  Pa_Initialize();

  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("-------\nSelected device: dev: %s || %f || id:%d out:%d || lil:%f lol:%f\n-------\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;

  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!!!!!\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);

  printf("<3 <3 <3 <3 Portaudio stream started <3 <3 <3 <3\n");
  synth->sound_active = 1;
}

void
destroy_sound(synth_t * synth)
{
  printf(":( :( :( stopping stream </3 </3 </3\n");
  synth->sound_active = 0;
  Pa_StopStream( synth->stream );
  Pa_CloseStream( synth->stream );
  Pa_Terminate();
}