| 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 |
#ifndef __portaudio_dsp__ |
| 38 |
#define __portaudio_dsp__ |
| 39 |
|
| 40 |
#include <stdio.h> |
| 41 |
#include <assert.h> |
| 42 |
#include <portaudio.h> |
| 43 |
|
| 44 |
#include "faust/audio/audio.h" |
| 45 |
#include "faust/audio/dsp.h" |
| 46 |
|
| 47 |
static int audioCallback(const void *ibuf, void *obuf, unsigned long frames, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void * drv); |
| 48 |
|
| 49 |
static bool pa_error(int err) |
| 50 |
{ |
| 51 |
if (err != paNoError) { |
| 52 |
printf("PortAudio error: %s\n", Pa_GetErrorText(err)); |
| 53 |
return true; |
| 54 |
} else { |
| 55 |
return false; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
class portaudio : public audio { |
| 68 |
|
| 69 |
private: |
| 70 |
|
| 71 |
dsp* fDsp; |
| 72 |
PaStream* fAudioStream; |
| 73 |
long fSampleRate; |
| 74 |
long fBufferSize; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
int fDevNumInChans; |
| 80 |
int fDevNumOutChans; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
float** fInChannel; |
| 86 |
float** fOutChannel; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
void allocChannels(int size, int numInChan, int numOutChan) |
| 92 |
{ |
| 93 |
fInChannel = new float*[numInChan]; |
| 94 |
fOutChannel = new float*[numOutChan]; |
| 95 |
|
| 96 |
for (int i = 0; i < numInChan; i++) { |
| 97 |
fInChannel[i] = (float*) calloc (size, sizeof(float)); |
| 98 |
for (int j = 0; j < size; j++) |
| 99 |
fInChannel[i][j] = 0.0; |
| 100 |
} |
| 101 |
|
| 102 |
for (int i = 0; i < numOutChan; i++) { |
| 103 |
fOutChannel[i] = (float*) calloc (size, sizeof(float)); |
| 104 |
for (int j = 0; j < size; j++) |
| 105 |
fOutChannel[i][j] = 0.0; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public: |
| 110 |
|
| 111 |
portaudio(long srate, long bsize) : fDsp(0), fAudioStream(0), |
| 112 |
fSampleRate(srate), fBufferSize(bsize), fDevNumInChans(0), fDevNumOutChans(0) {} |
| 113 |
virtual ~portaudio() |
| 114 |
{ |
| 115 |
stop(); |
| 116 |
delete [] fInChannel; |
| 117 |
delete [] fOutChannel; |
| 118 |
} |
| 119 |
|
| 120 |
virtual bool init(const char* name, dsp* DSP) |
| 121 |
{ |
| 122 |
fDsp = DSP; |
| 123 |
if (pa_error(Pa_Initialize())) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
const PaDeviceInfo* idev = Pa_GetDeviceInfo(Pa_GetDefaultInputDevice()); |
| 128 |
const PaDeviceInfo* odev = Pa_GetDeviceInfo(Pa_GetDefaultOutputDevice()); |
| 129 |
|
| 130 |
fDevNumInChans = (fDsp->getNumInputs() > 0) ? idev->maxInputChannels : 0; |
| 131 |
fDevNumOutChans = (fDsp->getNumOutputs() > 0) ? odev->maxOutputChannels : 0; |
| 132 |
|
| 133 |
PaStreamParameters inputParameters; |
| 134 |
PaStreamParameters outputParameters; |
| 135 |
|
| 136 |
inputParameters.device = Pa_GetDefaultInputDevice(); |
| 137 |
inputParameters.sampleFormat = paFloat32; |
| 138 |
inputParameters.channelCount = fDevNumInChans; |
| 139 |
inputParameters.hostApiSpecificStreamInfo = 0; |
| 140 |
|
| 141 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 142 |
outputParameters.sampleFormat = paFloat32; |
| 143 |
outputParameters.channelCount = fDevNumOutChans; |
| 144 |
outputParameters.hostApiSpecificStreamInfo = 0; |
| 145 |
|
| 146 |
PaError err; |
| 147 |
if ((err = Pa_IsFormatSupported( |
| 148 |
((fDevNumInChans > 0) ? &inputParameters : 0), |
| 149 |
((fDevNumOutChans > 0) ? &outputParameters : 0), fSampleRate)) != 0) { |
| 150 |
printf("stream format is not supported err = %d\n", err); |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
allocChannels(fBufferSize, max(fDevNumInChans, fDsp->getNumInputs()), max(fDevNumOutChans, fDsp->getNumOutputs())); |
| 155 |
fDsp->init(fSampleRate); |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
virtual bool start() |
| 160 |
{ |
| 161 |
if (pa_error(Pa_OpenDefaultStream(&fAudioStream, fDevNumInChans, fDevNumOutChans, paFloat32, fSampleRate, fBufferSize, audioCallback, this))) { |
| 162 |
return false; |
| 163 |
} |
| 164 |
if (pa_error(Pa_StartStream(fAudioStream)) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
virtual void stop() |
| 171 |
{ |
| 172 |
if (fAudioStream) { |
| 173 |
Pa_StopStream (fAudioStream); |
| 174 |
Pa_CloseStream(fAudioStream); |
| 175 |
fAudioStream = 0; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
int processAudio(const float* ibuf, float* obuf, unsigned long frames) |
| 180 |
{ |
| 181 |
const float* fInputBuffer = ibuf; |
| 182 |
float* fOutputBuffer = obuf; |
| 183 |
|
| 184 |
|
| 185 |
for (unsigned long s = 0; s < frames; s++) { |
| 186 |
for (int c = 0; c < fDevNumInChans; c++) { |
| 187 |
fInChannel[c][s] = fInputBuffer[c + s*fDevNumInChans]; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
fDsp->compute(frames, fInChannel, fOutChannel); |
| 193 |
|
| 194 |
|
| 195 |
for (unsigned long s = 0; s < frames; s++) { |
| 196 |
for (int c = 0; c < fDevNumOutChans; c++) { |
| 197 |
fOutputBuffer[c + s*fDevNumOutChans] = fOutChannel[c][s]; |
| 198 |
} |
| 199 |
} |
| 200 |
return 0; |
| 201 |
} |
| 202 |
}; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
static int audioCallback(const void* ibuf, void* obuf, unsigned long frames, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void* drv) |
| 208 |
{ |
| 209 |
portaudio* pa = (portaudio*) drv; |
| 210 |
return pa->processAudio((const float*)ibuf, (float*)obuf, frames); |
| 211 |
} |
| 212 |
|
| 213 |
#endif |