xref: /haiku/src/kits/midi/Synth.cpp (revision 81f5654c124bf46fba0fd251f208e2d88d81e1ce)
1 /*
2  * Copyright (c) 2002-2004 Matthijs Hollemans
3  * Copyright (c) 2003 Jerome Leveque
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <string.h>
25 
26 #include "debug.h"
27 #include "Synth.h"
28 #include "SoftSynth.h"
29 
30 BSynth* be_synth = NULL;
31 
32 using namespace BPrivate;
33 
34 //------------------------------------------------------------------------------
35 
36 BSynth::BSynth()
37 {
38 	Init();
39 }
40 
41 //------------------------------------------------------------------------------
42 
43 BSynth::BSynth(synth_mode mode)
44 {
45 	Init();
46 	synthMode = mode;
47 }
48 
49 //------------------------------------------------------------------------------
50 
51 BSynth::~BSynth()
52 {
53 	delete synth;
54 	be_synth = NULL;
55 }
56 
57 //------------------------------------------------------------------------------
58 
59 status_t BSynth::LoadSynthData(entry_ref* instrumentsFile)
60 {
61 	if (instrumentsFile == NULL)
62 	{
63 		return B_BAD_VALUE;
64 	}
65 
66 	return synth->SetInstrumentsFile(instrumentsFile->name);
67 }
68 
69 //------------------------------------------------------------------------------
70 
71 status_t BSynth::LoadSynthData(synth_mode mode)
72 {
73 	// Our softsynth doesn't support multiple modes like Be's synth did.
74 	// Therefore, if you use this function, the synth will revert to its
75 	// default instruments bank. However, we do keep track of the current
76 	// synth_mode here, in order not to confuse old applications.
77 
78 	synthMode = mode;
79 	if (synthMode == B_SAMPLES_ONLY)
80 	{
81 		fprintf(stderr, "[midi] LoadSynthData: BSamples is not supported\n");
82 	}
83 
84 	return synth->SetDefaultInstrumentsFile();
85 }
86 
87 //------------------------------------------------------------------------------
88 
89 synth_mode BSynth::SynthMode(void)
90 {
91 	return synthMode;
92 }
93 
94 //------------------------------------------------------------------------------
95 
96 void BSynth::Unload(void)
97 {
98 	synth->Unload();
99 }
100 
101 //------------------------------------------------------------------------------
102 
103 bool BSynth::IsLoaded(void) const
104 {
105 	return synth->IsLoaded();
106 }
107 
108 //------------------------------------------------------------------------------
109 
110 status_t BSynth::SetSamplingRate(int32 sample_rate)
111 {
112 	return synth->SetSamplingRate(sample_rate);
113 }
114 
115 //------------------------------------------------------------------------------
116 
117 int32 BSynth::SamplingRate() const
118 {
119 	return synth->SamplingRate();
120 }
121 
122 //------------------------------------------------------------------------------
123 
124 status_t BSynth::SetInterpolation(interpolation_mode interp_mode)
125 {
126 	return synth->SetInterpolation(interp_mode);
127 }
128 
129 //------------------------------------------------------------------------------
130 
131 interpolation_mode BSynth::Interpolation() const
132 {
133 	return synth->Interpolation();
134 }
135 
136 //------------------------------------------------------------------------------
137 
138 void BSynth::SetReverb(reverb_mode rev_mode)
139 {
140 	synth->SetReverb(rev_mode);
141 }
142 
143 //------------------------------------------------------------------------------
144 
145 reverb_mode BSynth::Reverb() const
146 {
147 	return synth->Reverb();
148 }
149 
150 //------------------------------------------------------------------------------
151 
152 status_t BSynth::EnableReverb(bool reverb_enabled)
153 {
154 	return synth->EnableReverb(reverb_enabled);
155 }
156 
157 //------------------------------------------------------------------------------
158 
159 bool BSynth::IsReverbEnabled() const
160 {
161 	return synth->IsReverbEnabled();
162 }
163 
164 //------------------------------------------------------------------------------
165 
166 status_t BSynth::SetVoiceLimits(
167 	int16 maxSynthVoices, int16 maxSampleVoices, int16 limiterThreshhold)
168 {
169 	status_t err = B_OK;
170 	err = synth->SetMaxVoices(maxSynthVoices);
171 	if (err == B_OK)
172 	{
173 		err = synth->SetLimiterThreshold(limiterThreshhold);
174 	}
175 	return err;
176 }
177 
178 //------------------------------------------------------------------------------
179 
180 int16 BSynth::MaxSynthVoices(void) const
181 {
182 	return synth->MaxVoices();
183 }
184 
185 //------------------------------------------------------------------------------
186 
187 int16 BSynth::MaxSampleVoices(void) const
188 {
189 	fprintf(stderr, "[midi] MaxSampleVoices: BSamples not supported\n");
190 	return 0;
191 }
192 
193 //------------------------------------------------------------------------------
194 
195 int16 BSynth::LimiterThreshhold(void) const
196 {
197 	return synth->LimiterThreshold();
198 }
199 
200 //------------------------------------------------------------------------------
201 
202 void BSynth::SetSynthVolume(double theVolume)
203 {
204 	synth->SetVolume(theVolume);
205 }
206 
207 //------------------------------------------------------------------------------
208 
209 double BSynth::SynthVolume(void) const
210 {
211 	return synth->Volume();
212 }
213 
214 //------------------------------------------------------------------------------
215 
216 void BSynth::SetSampleVolume(double theVolume)
217 {
218 	fprintf(stderr, "[midi] SetSampleVolume: BSamples not supported\n");
219 }
220 
221 //------------------------------------------------------------------------------
222 
223 double BSynth::SampleVolume(void) const
224 {
225 	fprintf(stderr, "[midi] SampleVolume: BSamples not supported\n");
226 	return 0;
227 }
228 
229 //------------------------------------------------------------------------------
230 
231 status_t BSynth::GetAudio(
232 	int16* pLeft, int16* pRight, int32 max_samples) const
233 {
234 	// We don't print a "not supported" message here. That would cause
235 	// significant slowdowns because applications ask for this many times.
236 
237 	memset(pLeft, 0, max_samples * sizeof(int16));
238 	memset(pRight, 0, max_samples * sizeof(int16));
239 
240 	return max_samples;
241 }
242 
243 //------------------------------------------------------------------------------
244 
245 void BSynth::Pause(void)
246 {
247 	synth->Pause();
248 }
249 
250 //------------------------------------------------------------------------------
251 
252 void BSynth::Resume(void)
253 {
254 	synth->Resume();
255 }
256 
257 //------------------------------------------------------------------------------
258 
259 void BSynth::SetControllerHook(int16 controller, synth_controller_hook cback)
260 {
261 	fprintf(stderr, "[midi] SetControllerHook is not supported\n");
262 }
263 
264 //------------------------------------------------------------------------------
265 
266 void BSynth::Init()
267 {
268 	delete be_synth;
269 	be_synth = this;
270 	synthMode = B_NO_SYNTH;
271 	clientCount = 0;
272 	synth = new BSoftSynth();
273 }
274 
275 //------------------------------------------------------------------------------
276 
277 int32 BSynth::CountClients(void) const
278 {
279 	return clientCount;
280 }
281 
282 //------------------------------------------------------------------------------
283 
284 void BSynth::_ReservedSynth1() { }
285 void BSynth::_ReservedSynth2() { }
286 void BSynth::_ReservedSynth3() { }
287 void BSynth::_ReservedSynth4() { }
288 
289 //------------------------------------------------------------------------------
290