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 25 BSynth::BSynth() 26 { 27 _Init(); 28 } 29 30 31 BSynth::BSynth(synth_mode mode) 32 { 33 _Init(); 34 fSynthMode = mode; 35 } 36 37 38 BSynth::~BSynth() 39 { 40 delete fSynth; 41 be_synth = NULL; 42 } 43 44 45 status_t 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 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 78 BSynth::SynthMode() 79 { 80 return fSynthMode; 81 } 82 83 84 void 85 BSynth::Unload() 86 { 87 fSynth->Unload(); 88 } 89 90 91 bool 92 BSynth::IsLoaded() const 93 { 94 return fSynth->IsLoaded(); 95 } 96 97 98 status_t 99 BSynth::SetSamplingRate(int32 sample_rate) 100 { 101 return fSynth->SetSamplingRate(sample_rate); 102 } 103 104 105 int32 106 BSynth::SamplingRate() const 107 { 108 return fSynth->SamplingRate(); 109 } 110 111 112 status_t 113 BSynth::SetInterpolation(interpolation_mode interp_mode) 114 { 115 return fSynth->SetInterpolation(interp_mode); 116 } 117 118 119 interpolation_mode 120 BSynth::Interpolation() const 121 { 122 return fSynth->Interpolation(); 123 } 124 125 126 void 127 BSynth::SetReverb(reverb_mode rev_mode) 128 { 129 fSynth->SetReverb(rev_mode); 130 } 131 132 133 reverb_mode 134 BSynth::Reverb() const 135 { 136 return fSynth->Reverb(); 137 } 138 139 140 status_t 141 BSynth::EnableReverb(bool reverb_enabled) 142 { 143 return fSynth->EnableReverb(reverb_enabled); 144 } 145 146 147 bool 148 BSynth::IsReverbEnabled() const 149 { 150 return fSynth->IsReverbEnabled(); 151 } 152 153 154 status_t 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 168 BSynth::MaxSynthVoices() const 169 { 170 return fSynth->MaxVoices(); 171 } 172 173 174 int16 175 BSynth::MaxSampleVoices() const 176 { 177 fprintf(stderr, "[midi] MaxSampleVoices: BSamples not supported\n"); 178 return 0; 179 } 180 181 182 int16 183 BSynth::LimiterThreshhold() const 184 { 185 return fSynth->LimiterThreshold(); 186 } 187 188 189 void 190 BSynth::SetSynthVolume(double theVolume) 191 { 192 fSynth->SetVolume(theVolume); 193 } 194 195 196 double 197 BSynth::SynthVolume() const 198 { 199 return fSynth->Volume(); 200 } 201 202 203 void 204 BSynth::SetSampleVolume(double theVolume) 205 { 206 fprintf(stderr, "[midi] SetSampleVolume: BSamples not supported\n"); 207 } 208 209 210 double 211 BSynth::SampleVolume(void) const 212 { 213 fprintf(stderr, "[midi] SampleVolume: BSamples not supported\n"); 214 return 0; 215 } 216 217 218 status_t 219 BSynth::GetAudio(int16* pLeft, int16* pRight, int32 max_samples) const 220 { 221 // We don't print a "not supported" message here. That would cause 222 // significant slowdowns because applications ask for this many times. 223 224 if (fSynth->fMonitorSize <= 0) { 225 memset(pLeft, 0, max_samples * sizeof(int16)); 226 memset(pRight, 0, max_samples * sizeof(int16)); 227 return max_samples; 228 } 229 230 int32 nSamples = fSynth->fMonitorSize / sizeof(float) 231 / fSynth->fMonitorChans; 232 if (nSamples > max_samples) 233 nSamples = max_samples; 234 float* sPtr = fSynth->fMonitor; 235 for (int32 i = 0; i < nSamples; i++, sPtr += fSynth->fMonitorChans) { 236 *pLeft++ = (int16)(*sPtr * 32768); 237 *pRight++ = (int16)(*(sPtr + 1) * 32768); 238 } 239 return nSamples; 240 } 241 242 243 void 244 BSynth::Pause() 245 { 246 fSynth->Pause(); 247 } 248 249 250 void 251 BSynth::Resume() 252 { 253 fSynth->Resume(); 254 } 255 256 257 void 258 BSynth::SetControllerHook(int16 controller, synth_controller_hook cback) 259 { 260 fprintf(stderr, "[midi] SetControllerHook is not supported\n"); 261 } 262 263 264 void 265 BSynth::_Init() 266 { 267 delete be_synth; 268 be_synth = this; 269 fSynthMode = B_NO_SYNTH; 270 fClientCount = 0; 271 fSynth = new BSoftSynth(); 272 } 273 274 275 int32 276 BSynth::CountClients() const 277 { 278 return fClientCount; 279 } 280 281 282 void BSynth::_ReservedSynth1() { } 283 void BSynth::_ReservedSynth2() { } 284 void BSynth::_ReservedSynth3() { } 285 void BSynth::_ReservedSynth4() { } 286 287