1 /*
2 * Copyright 2006, Haiku.
3 *
4 * Copyright (c) 2002-2004 Matthijs Hollemans
5 * Copyright (c) 2003 Jerome Leveque
6 * Distributed under the terms of the MIT License.
7 *
8 * Authors:
9 * Jérôme Leveque
10 * Matthijs Hollemans
11 */
12
13 #include <string.h>
14
15 #include "debug.h"
16 #include <Path.h>
17 #include <Synth.h>
18 #include "SoftSynth.h"
19
20 BSynth* be_synth = NULL;
21
22 using namespace BPrivate;
23
24
BSynth()25 BSynth::BSynth()
26 {
27 _Init();
28 }
29
30
BSynth(synth_mode mode)31 BSynth::BSynth(synth_mode mode)
32 {
33 _Init();
34 fSynthMode = mode;
35 }
36
37
~BSynth()38 BSynth::~BSynth()
39 {
40 delete fSynth;
41 be_synth = NULL;
42 }
43
44
45 status_t
LoadSynthData(entry_ref * instrumentsFile)46 BSynth::LoadSynthData(entry_ref* instrumentsFile)
47 {
48 if (instrumentsFile == NULL) {
49 return B_BAD_VALUE;
50 }
51
52 BPath path(instrumentsFile);
53 status_t err = path.InitCheck();
54 if (err != B_OK)
55 return err;
56 return fSynth->SetInstrumentsFile(path.Path());
57 }
58
59
60 status_t
LoadSynthData(synth_mode mode)61 BSynth::LoadSynthData(synth_mode mode)
62 {
63 // Our softsynth doesn't support multiple modes like Be's synth did.
64 // Therefore, if you use this function, the synth will revert to its
65 // default instruments bank. However, we do keep track of the current
66 // synth_mode here, in order not to confuse old applications.
67
68 fSynthMode = mode;
69 if (fSynthMode == B_SAMPLES_ONLY) {
70 fprintf(stderr, "[midi] LoadSynthData: BSamples is not supported\n");
71 }
72
73 return fSynth->SetDefaultInstrumentsFile();
74 }
75
76
77 synth_mode
SynthMode()78 BSynth::SynthMode()
79 {
80 return fSynthMode;
81 }
82
83
84 void
Unload()85 BSynth::Unload()
86 {
87 fSynth->Unload();
88 }
89
90
91 bool
IsLoaded() const92 BSynth::IsLoaded() const
93 {
94 return fSynth->IsLoaded();
95 }
96
97
98 status_t
SetSamplingRate(int32 sample_rate)99 BSynth::SetSamplingRate(int32 sample_rate)
100 {
101 return fSynth->SetSamplingRate(sample_rate);
102 }
103
104
105 int32
SamplingRate() const106 BSynth::SamplingRate() const
107 {
108 return fSynth->SamplingRate();
109 }
110
111
112 status_t
SetInterpolation(interpolation_mode interp_mode)113 BSynth::SetInterpolation(interpolation_mode interp_mode)
114 {
115 return fSynth->SetInterpolation(interp_mode);
116 }
117
118
119 interpolation_mode
Interpolation() const120 BSynth::Interpolation() const
121 {
122 return fSynth->Interpolation();
123 }
124
125
126 void
SetReverb(reverb_mode rev_mode)127 BSynth::SetReverb(reverb_mode rev_mode)
128 {
129 fSynth->SetReverb(rev_mode);
130 }
131
132
133 reverb_mode
Reverb() const134 BSynth::Reverb() const
135 {
136 return fSynth->Reverb();
137 }
138
139
140 status_t
EnableReverb(bool reverb_enabled)141 BSynth::EnableReverb(bool reverb_enabled)
142 {
143 return fSynth->EnableReverb(reverb_enabled);
144 }
145
146
147 bool
IsReverbEnabled() const148 BSynth::IsReverbEnabled() const
149 {
150 return fSynth->IsReverbEnabled();
151 }
152
153
154 status_t
SetVoiceLimits(int16 maxSynthVoices,int16 maxSampleVoices,int16 limiterThreshhold)155 BSynth::SetVoiceLimits(
156 int16 maxSynthVoices, int16 maxSampleVoices, int16 limiterThreshhold)
157 {
158 status_t err = B_OK;
159 err = fSynth->SetMaxVoices(maxSynthVoices);
160 if (err == B_OK) {
161 err = fSynth->SetLimiterThreshold(limiterThreshhold);
162 }
163 return err;
164 }
165
166
167 int16
MaxSynthVoices() const168 BSynth::MaxSynthVoices() const
169 {
170 return fSynth->MaxVoices();
171 }
172
173
174 int16
MaxSampleVoices() const175 BSynth::MaxSampleVoices() const
176 {
177 fprintf(stderr, "[midi] MaxSampleVoices: BSamples not supported\n");
178 return 0;
179 }
180
181
182 int16
LimiterThreshhold() const183 BSynth::LimiterThreshhold() const
184 {
185 return fSynth->LimiterThreshold();
186 }
187
188
189 void
SetSynthVolume(double theVolume)190 BSynth::SetSynthVolume(double theVolume)
191 {
192 fSynth->SetVolume(theVolume);
193 }
194
195
196 double
SynthVolume() const197 BSynth::SynthVolume() const
198 {
199 return fSynth->Volume();
200 }
201
202
203 void
SetSampleVolume(double theVolume)204 BSynth::SetSampleVolume(double theVolume)
205 {
206 fprintf(stderr, "[midi] SetSampleVolume: BSamples not supported\n");
207 }
208
209
210 double
SampleVolume(void) const211 BSynth::SampleVolume(void) const
212 {
213 fprintf(stderr, "[midi] SampleVolume: BSamples not supported\n");
214 return 0;
215 }
216
217
218 status_t
GetAudio(int16 * pLeft,int16 * pRight,int32 max_samples) const219 BSynth::GetAudio(int16* pLeft, int16* pRight, int32 max_samples) const
220 {
221 if (fSynth->fMonitorSize <= 0) {
222 memset(pLeft, 0, max_samples * sizeof(int16));
223 memset(pRight, 0, max_samples * sizeof(int16));
224 return max_samples;
225 }
226
227 int32 nSamples = fSynth->fMonitorSize / sizeof(float)
228 / fSynth->fMonitorChans;
229 if (nSamples > max_samples)
230 nSamples = max_samples;
231 float* sPtr = fSynth->fMonitor;
232 for (int32 i = 0; i < nSamples; i++, sPtr += fSynth->fMonitorChans) {
233 *pLeft++ = (int16)(*sPtr * 32768);
234 *pRight++ = (int16)(*(sPtr + 1) * 32768);
235 }
236 return nSamples;
237 }
238
239
240 void
Pause()241 BSynth::Pause()
242 {
243 fSynth->Pause();
244 }
245
246
247 void
Resume()248 BSynth::Resume()
249 {
250 fSynth->Resume();
251 }
252
253
254 void
SetControllerHook(int16 controller,synth_controller_hook cback)255 BSynth::SetControllerHook(int16 controller, synth_controller_hook cback)
256 {
257 fprintf(stderr, "[midi] SetControllerHook is not supported\n");
258 }
259
260
261 void
_Init()262 BSynth::_Init()
263 {
264 delete be_synth;
265 be_synth = this;
266 fSynthMode = B_NO_SYNTH;
267 fClientCount = 0;
268 fSynth = new BSoftSynth();
269 }
270
271
272 int32
CountClients() const273 BSynth::CountClients() const
274 {
275 return fClientCount;
276 }
277
278
_ReservedSynth1()279 void BSynth::_ReservedSynth1() { }
_ReservedSynth2()280 void BSynth::_ReservedSynth2() { }
_ReservedSynth3()281 void BSynth::_ReservedSynth3() { }
_ReservedSynth4()282 void BSynth::_ReservedSynth4() { }
283
284