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
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 //-----------------------------------------------------------------------------
32 #include "chuck_bbq.h"
40 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
46 m_max_midi_device
= 1024;
47 m_digi_out
= new DigitalOut
;
48 m_digi_in
= new DigitalIn
;
49 m_midi_out
= new MidiOut
* [m_max_midi_device
];
50 m_midi_in
= new MidiIn
* [m_max_midi_device
];
51 m_in_count
= new DWORD__
[m_max_midi_device
];
52 m_out_count
= new DWORD__
[m_max_midi_device
];
53 memset( m_midi_out
, 0, m_max_midi_device
* sizeof(MidiOut
*) );
54 memset( m_midi_in
, 0, m_max_midi_device
* sizeof(MidiIn
*) );
55 memset( m_in_count
, 0, m_max_midi_device
* sizeof(DWORD__
) );
56 memset( m_out_count
, 0, m_max_midi_device
* sizeof(DWORD__
) );
57 //m_midi_out[0] = new MidiOut;
58 //m_midi_in[0] = new MidiIn;
64 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
70 SAFE_DELETE( m_digi_out
);
71 SAFE_DELETE( m_digi_in
);
72 SAFE_DELETE_ARRAY( m_midi_out
);
73 SAFE_DELETE_ARRAY( m_midi_in
);
74 SAFE_DELETE_ARRAY( m_in_count
);
75 SAFE_DELETE_ARRAY( m_out_count
);
81 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 BOOL__
BBQ::initialize( DWORD__ num_channels
, DWORD__ sampling_rate
,
86 DWORD__ bps
, DWORD__ buffer_size
, DWORD__ num_buffers
,
87 DWORD__ dac
, DWORD__ adc
)
89 Digitalio::m_dac_n
= dac
;
90 Digitalio::m_adc_n
= adc
;
92 return Digitalio::initialize(
93 num_channels
, sampling_rate
, bps
, buffer_size
, num_buffers
100 //-----------------------------------------------------------------------------
103 //-----------------------------------------------------------------------------
104 void BBQ::set_srate( DWORD__ srate
)
106 Digitalio::m_sampling_rate
= srate
;
112 //-----------------------------------------------------------------------------
113 // name: set_bufsize()
115 //-----------------------------------------------------------------------------
116 void BBQ::set_bufsize( DWORD__ bufsize
)
118 Digitalio::m_buffer_size
= bufsize
;
123 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
129 Digitalio::shutdown();
135 //-----------------------------------------------------------------------------
138 //-----------------------------------------------------------------------------
139 DigitalOut
* BBQ::digi_out()
147 //-----------------------------------------------------------------------------
150 //-----------------------------------------------------------------------------
151 DigitalIn
* BBQ::digi_in()
159 //-----------------------------------------------------------------------------
162 //-----------------------------------------------------------------------------
163 MidiOut
* BBQ::midi_out( DWORD__ index
)
165 if( index
>= m_max_midi_device
)
168 if( m_midi_out
[index
] == NULL
)
170 m_midi_out
[index
] = new MidiOut
;
171 if( !m_midi_out
[index
]->open( index
) )
173 delete m_midi_out
[index
];
174 m_midi_out
[index
] = NULL
;
175 m_out_count
[index
]++;
178 m_out_count
[index
] = 0;
181 return m_midi_out
[index
];
187 //-----------------------------------------------------------------------------
190 //-----------------------------------------------------------------------------
191 MidiIn
* BBQ::midi_in( DWORD__ index
)
193 if( index
>= m_max_midi_device
)
196 if( m_midi_in
[index
] == NULL
)
198 m_midi_in
[index
] = new MidiIn
;
199 if( !m_midi_in
[index
]->open( index
) )
201 delete m_midi_in
[index
];
202 m_midi_in
[index
] = NULL
;
206 m_in_count
[index
] = 0;
209 return m_midi_in
[index
];
215 //-----------------------------------------------------------------------------
216 // name: RangeMapper()
218 //-----------------------------------------------------------------------------
219 RangeMapper::RangeMapper( SAMPLE low_l
, SAMPLE high_l
,
220 SAMPLE low_r
, SAMPLE high_r
)
222 SAMPLE dx
= high_l
- low_l
;
223 if( fabs( (double)dx
) < .000001 )
226 m_slope
= ( high_r
- low_r
) / dx
;
228 m_inter
= high_r
- m_slope
* high_l
;
234 //-----------------------------------------------------------------------------
237 //-----------------------------------------------------------------------------
238 SAMPLE
RangeMapper::map( SAMPLE x
)
240 return m_slope
* x
+ m_inter
;