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