Debian Codegraph @

faudiostream/architecture/faust/audio/portaudio-dsp.h

raw symbols SCIP
1 /************************************************************************
2
3 IMPORTANT NOTE : this file contains two clearly delimited sections :
4 the ARCHITECTURE section (in two parts) and the USER section. Each section
5 is governed by its own copyright and license. Please check individually
6 each section for license and copyright information.
7 *************************************************************************/
8
9 /*******************BEGIN ARCHITECTURE SECTION (part 1/2)****************/
10
11 /************************************************************************
12 FAUST Architecture File
13 Copyright (C) 2003-2011 GRAME, Centre National de Creation Musicale
14 ---------------------------------------------------------------------
15 This Architecture section is free software; you can redistribute it
16 and/or modify it under the terms of the GNU General Public License
17 as published by the Free Software Foundation; either version 3 of
18 the License, or (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; If not, see <http://www.gnu.org/licenses/>.
27
28 EXCEPTION : As a special exception, you may create a larger work
29 that contains this FAUST architecture section and distribute
30 that work under terms of your choice, so long as this FAUST
31 architecture section is not modified.
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 PORT AUDIO INTERFACE
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 // number of physical input and output channels of the PA device
78 //----------------------------------------------------------------------------
79 int fDevNumInChans;
80 int fDevNumOutChans;
81
82 //----------------------------------------------------------------------------
83 // tables of noninterleaved input and output channels for FAUST
84 //----------------------------------------------------------------------------
85 float** fInChannel;
86 float** fOutChannel;
87
88 //----------------------------------------------------------------------------
89 // allocated the noninterleaved input and output channels for FAUST
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 // split input samples
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 // process samples
192 fDsp->compute(frames, fInChannel, fOutChannel);
193
194 // merge output samples
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 // Port Audio Callback
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