*** empty log message ***
[chuck-blob.git] / v2 / chuck_bbq.cpp
blob428773c12b5daadab848950be05c3781ac12c2cb
1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 U.S.A.
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: chuck_bbq.cpp
27 // desc: implementation
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // date: Spring 2004
32 //-----------------------------------------------------------------------------
33 #include "chuck_bbq.h"
34 #include <math.h>
35 #include <memory.h>
36 #include <stdlib.h>
41 //-----------------------------------------------------------------------------
42 // name: BBQ()
43 // desc: ...
44 //-----------------------------------------------------------------------------
45 BBQ::BBQ()
47 m_max_midi_device = 1024;
48 m_digi_out = new DigitalOut;
49 m_digi_in = new DigitalIn;
50 m_midi_out = new MidiOut * [m_max_midi_device];
51 m_midi_in = new MidiIn * [m_max_midi_device];
52 m_in_count = new DWORD__[m_max_midi_device];
53 m_out_count = new DWORD__[m_max_midi_device];
54 memset( m_midi_out, 0, m_max_midi_device * sizeof(MidiOut *) );
55 memset( m_midi_in, 0, m_max_midi_device * sizeof(MidiIn *) );
56 memset( m_in_count, 0, m_max_midi_device * sizeof(DWORD__) );
57 memset( m_out_count, 0, m_max_midi_device * sizeof(DWORD__) );
58 //m_midi_out[0] = new MidiOut;
59 //m_midi_in[0] = new MidiIn;
65 //-----------------------------------------------------------------------------
66 // name: ~BBQ()
67 // desc: ...
68 //-----------------------------------------------------------------------------
69 BBQ::~BBQ()
71 SAFE_DELETE( m_digi_out );
72 SAFE_DELETE( m_digi_in );
73 SAFE_DELETE_ARRAY( m_midi_out );
74 SAFE_DELETE_ARRAY( m_midi_in );
75 SAFE_DELETE_ARRAY( m_in_count );
76 SAFE_DELETE_ARRAY( m_out_count );
82 //-----------------------------------------------------------------------------
83 // name: initialize()
84 // desc: ...
85 //-----------------------------------------------------------------------------
86 BOOL__ BBQ::initialize( DWORD__ num_dac_channels, DWORD__ num_adc_channels,
87 DWORD__ sampling_rate, DWORD__ bps, DWORD__ buffer_size,
88 DWORD__ num_buffers, DWORD__ dac, DWORD__ adc,
89 DWORD__ block, Chuck_VM * vm_ref, BOOL__ rt_audio,
90 void * callback, void * data )
92 set_inouts( adc, dac );
93 set_chans( num_adc_channels, num_dac_channels );
95 return Digitalio::initialize(
96 num_dac_channels, num_adc_channels,
97 sampling_rate, bps, buffer_size, num_buffers,
98 block, vm_ref, rt_audio, callback, data
105 //-----------------------------------------------------------------------------
106 // name: set_srate()
107 // desc: ...
108 //-----------------------------------------------------------------------------
109 void BBQ::set_srate( DWORD__ srate )
111 Digitalio::m_sampling_rate = srate;
117 //-----------------------------------------------------------------------------
118 // name: set_bufsize()
119 // desc: ...
120 //-----------------------------------------------------------------------------
121 void BBQ::set_bufsize( DWORD__ bufsize )
123 Digitalio::m_buffer_size = bufsize;
128 //-----------------------------------------------------------------------------
129 // name: set_bufnums()
130 // desc: ...
131 //-----------------------------------------------------------------------------
132 void BBQ::set_numbufs( DWORD__ numbufs )
134 Digitalio::m_num_buffers = numbufs;
140 //-----------------------------------------------------------------------------
141 // name: set_inouts()
142 // desc: ...
143 //-----------------------------------------------------------------------------
144 void BBQ::set_inouts( DWORD__ adc, DWORD__ dac )
146 Digitalio::m_dac_n = dac;
147 Digitalio::m_adc_n = adc;
153 //-----------------------------------------------------------------------------
154 // name: set_chans()
155 // desc: ...
156 //-----------------------------------------------------------------------------
157 void BBQ::set_chans( DWORD__ ins, DWORD__ outs )
159 Digitalio::m_num_channels_in = ins;
160 Digitalio::m_num_channels_out = outs;
166 //-----------------------------------------------------------------------------
167 // name: shutdown()
168 // desc: ...
169 //-----------------------------------------------------------------------------
170 void BBQ::shutdown()
172 Digitalio::shutdown();
178 //-----------------------------------------------------------------------------
179 // name: digi_out()
180 // desc: digital out
181 //-----------------------------------------------------------------------------
182 DigitalOut * BBQ::digi_out()
184 return m_digi_out;
190 //-----------------------------------------------------------------------------
191 // name: digi_in()
192 // desc: digital in
193 //-----------------------------------------------------------------------------
194 DigitalIn * BBQ::digi_in()
196 return m_digi_in;
202 //-----------------------------------------------------------------------------
203 // name: midi_out()
204 // desc: midi out
205 //-----------------------------------------------------------------------------
206 MidiOut * BBQ::midi_out( DWORD__ index )
208 if( index >= m_max_midi_device )
209 return NULL;
211 if( m_midi_out[index] == NULL )
213 m_midi_out[index] = new MidiOut;
214 if( !m_midi_out[index]->open( index ) )
216 delete m_midi_out[index];
217 m_midi_out[index] = NULL;
218 m_out_count[index]++;
220 else
221 m_out_count[index] = 0;
224 return m_midi_out[index];
230 //-----------------------------------------------------------------------------
231 // name: midi_in()
232 // desc: ...
233 //-----------------------------------------------------------------------------
234 MidiIn * BBQ::midi_in( DWORD__ index )
236 if( index >= m_max_midi_device )
237 return NULL;
239 if( m_midi_in[index] == NULL )
241 m_midi_in[index] = new MidiIn;
242 if( !m_midi_in[index]->open( index ) )
244 delete m_midi_in[index];
245 m_midi_in[index] = NULL;
246 m_in_count[index]++;
248 else
249 m_in_count[index] = 0;
252 return m_midi_in[index];
258 //-----------------------------------------------------------------------------
259 // name: RangeMapper()
260 // desc: constructor
261 //-----------------------------------------------------------------------------
262 RangeMapper::RangeMapper( SAMPLE low_l, SAMPLE high_l,
263 SAMPLE low_r, SAMPLE high_r )
265 SAMPLE dx = high_l - low_l;
266 if( fabs( (double)dx ) < .000001 )
267 m_slope = 0.0f;
268 else
269 m_slope = ( high_r - low_r ) / dx;
271 m_inter = high_r - m_slope * high_l;
277 //-----------------------------------------------------------------------------
278 // name: map()
279 // desc: ...
280 //-----------------------------------------------------------------------------
281 SAMPLE RangeMapper::map( SAMPLE x )
283 return m_slope * x + m_inter;