1 /************************************************************************
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 *************************************************************************/
9 /*******************BEGIN ARCHITECTURE SECTION (part 1/2)****************/
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.
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.
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/>.
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.
34 ************************************************************************
35 ************************************************************************/
38 #ifndef __portaudio_dsp__
39 #define __portaudio_dsp__
43 #include <portaudio.h>
48 #define max(x,y) (((x)>(y)) ? (x) : (y))
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
)
54 if (err
!= paNoError
) {
55 printf( "PortAudio error: %s\n", Pa_GetErrorText( err
) );
59 /******************************************************************************
60 *******************************************************************************
64 *******************************************************************************
65 *******************************************************************************/
66 class portaudio
: public audio
{
69 PaStream
* fAudioStream
;
70 long fSampleRate
, fBufferSize
;
71 //----------------------------------------------------------------------------
72 // number of physical input and output channels of the PA device
73 //----------------------------------------------------------------------------
77 //----------------------------------------------------------------------------
78 // tables of noninterleaved input and output channels for FAUST
79 //----------------------------------------------------------------------------
80 float* fInChannel
[256];
81 float* fOutChannel
[256];
83 //----------------------------------------------------------------------------
84 // allocated the noninterleaved input and output channels for FAUST
85 //----------------------------------------------------------------------------
86 void allocChannels (int size
, int numInChan
, int numOutChan
)
88 assert (numInChan
< 256);
89 assert (numOutChan
< 256);
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;
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;
105 portaudio(long srate
, long bsize
) : fDsp(0), fAudioStream(0),
106 fSampleRate(srate
), fBufferSize(bsize
), fDevNumInChans(0), fDevNumOutChans(0) {}
107 virtual ~portaudio() { stop(); }
109 virtual bool init(const char* name
, dsp
* DSP
){
111 pa_error(Pa_Initialize());
113 const PaDeviceInfo
* idev
= Pa_GetDeviceInfo(Pa_GetDefaultInputDevice());
114 const PaDeviceInfo
* odev
= Pa_GetDeviceInfo(Pa_GetDefaultOutputDevice());
116 fDevNumInChans
= (fDsp
->getNumInputs() > 0) ? idev
->maxInputChannels
: 0 ;
117 fDevNumOutChans
= (fDsp
->getNumOutputs() > 0) ? odev
->maxOutputChannels
: 0;
119 PaStreamParameters inputParameters
;
120 PaStreamParameters outputParameters
;
122 inputParameters
.device
= Pa_GetDefaultInputDevice();
123 inputParameters
.sampleFormat
= paFloat32
;
124 inputParameters
.channelCount
= fDevNumInChans
;
125 inputParameters
.hostApiSpecificStreamInfo
= 0;
127 outputParameters
.device
= Pa_GetDefaultOutputDevice();
128 outputParameters
.sampleFormat
= paFloat32
;
129 outputParameters
.channelCount
= fDevNumOutChans
;
130 outputParameters
.hostApiSpecificStreamInfo
= 0;
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
);
140 allocChannels(fBufferSize
, max(fDevNumInChans
, fDsp
->getNumInputs()), max(fDevNumOutChans
, fDsp
->getNumOutputs()));
141 fDsp
->init(fSampleRate
);
145 virtual bool start() {
146 pa_error(Pa_OpenDefaultStream(&fAudioStream
, fDevNumInChans
, fDevNumOutChans
, paFloat32
, fSampleRate
, fBufferSize
, audioCallback
, this));
147 Pa_StartStream(fAudioStream
);
151 virtual void stop() {
153 Pa_StopStream (fAudioStream
);
154 Pa_CloseStream(fAudioStream
);
159 int processAudio(const float *ibuf
, float *obuf
, unsigned long frames
) {
160 const float* fInputBuffer
= ibuf
;
161 float* fOutputBuffer
= obuf
;
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
];
171 fDsp
->compute(frames
, fInChannel
, fOutChannel
);
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
];
183 //----------------------------------------------------------------------------
184 // Port Audio Callback
185 //----------------------------------------------------------------------------
186 static int audioCallback(const void *ibuf
, void *obuf
, unsigned long frames
, const PaStreamCallbackTimeInfo
*, PaStreamCallbackFlags
, void * drv
)
188 portaudio
* pa
= (portaudio
*) drv
;
189 return pa
->processAudio ((const float*)ibuf
, (float*)obuf
, frames
);