BSynth/BSoftSynth: implemented GetAudio().

Patch by Pete Goodeve (modified by me, hopefully for the better and
without introducing bugs) which implements BSynth::GetAudio().
This commit is contained in:
Stefano Ceccherini
2014-09-21 19:46:04 +02:00
parent b2e8d9627a
commit d553b90c86
4 changed files with 39 additions and 9 deletions
+1 -2
View File
@@ -34,8 +34,7 @@ class BMidiSynthFile;
namespace BPrivate { class BSoftSynth; } namespace BPrivate { class BSoftSynth; }
class BSynth class BSynth {
{
public: public:
BSynth(); BSynth();
+16 -2
View File
@@ -43,7 +43,10 @@ BSoftSynth::BSoftSynth()
: fInitCheck(false), : fInitCheck(false),
fSynth(NULL), fSynth(NULL),
fSettings(NULL), fSettings(NULL),
fSoundPlayer(NULL) fSoundPlayer(NULL),
fMonitor(NULL),
fMonitorSize(0),
fMonitorChans(-1)
{ {
fInstrumentsFile = NULL; fInstrumentsFile = NULL;
SetDefaultInstrumentsFile(); SetDefaultInstrumentsFile();
@@ -65,6 +68,7 @@ BSoftSynth::~BSoftSynth()
// a big deal, but the Midi Kit will complain (on stdout) that we // a big deal, but the Midi Kit will complain (on stdout) that we
// didn't release our endpoints. // didn't release our endpoints.
delete[] fMonitor;
Unload(); Unload();
} }
@@ -527,13 +531,23 @@ BSoftSynth::PlayBuffer(void* cookie, void* data, size_t size,
{ {
BSoftSynth* synth = (BSoftSynth*)cookie; BSoftSynth* synth = (BSoftSynth*)cookie;
if (synth->fMonitor != NULL) {
delete[] synth->fMonitor;
synth->fMonitor = NULL;
}
// we use float samples // we use float samples
if (synth->fSynth) { if (synth->fSynth) {
fluid_synth_write_float( fluid_synth_write_float(
synth->fSynth, size / sizeof(float) / format.channel_count, synth->fSynth, size / sizeof(float) / format.channel_count,
data, 0, format.channel_count, data, 0, format.channel_count,
data, 1, format.channel_count); data, 1, format.channel_count);
synth->fMonitor = new float[size];
synth->fMonitorSize = size;
synth->fMonitorChans = format.channel_count;
memcpy(synth->fMonitor, data, size);
} }
} }
+6 -1
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2006, Haiku. * Copyright 2006-2014, Haiku.
* *
* Copyright (c) 2004-2005 Matthijs Hollemans * Copyright (c) 2004-2005 Matthijs Hollemans
* Copyright (c) 2003 Jerome Leveque * Copyright (c) 2003 Jerome Leveque
@@ -9,6 +9,7 @@
* Jérôme Duval * Jérôme Duval
* Jérôme Leveque * Jérôme Leveque
* Matthijs Hollemans * Matthijs Hollemans
* Pete Goodeve
*/ */
#ifndef _SOFT_SYNTH_H #ifndef _SOFT_SYNTH_H
@@ -107,6 +108,10 @@ private:
fluid_settings_t* fSettings; fluid_settings_t* fSettings;
BSoundPlayer* fSoundPlayer; BSoundPlayer* fSoundPlayer;
float* fMonitor;
size_t fMonitorSize;
int16 fMonitorChans;
}; };
} // namespace BPrivate } // namespace BPrivate
+15 -3
View File
@@ -221,10 +221,22 @@ BSynth::GetAudio(int16* pLeft, int16* pRight, int32 max_samples) const
// We don't print a "not supported" message here. That would cause // We don't print a "not supported" message here. That would cause
// significant slowdowns because applications ask for this many times. // significant slowdowns because applications ask for this many times.
memset(pLeft, 0, max_samples * sizeof(int16)); if (fSynth->fMonitorSize <= 0) {
memset(pRight, 0, max_samples * sizeof(int16)); memset(pLeft, 0, max_samples * sizeof(int16));
memset(pRight, 0, max_samples * sizeof(int16));
return max_samples;
}
return max_samples; int32 nSamples = fSynth->fMonitorSize / sizeof(float)
/ fSynth->fMonitorChans;
if (nSamples > max_samples)
nSamples = max_samples;
float* sPtr = fSynth->fMonitor;
for (int32 i = 0; i < nSamples; i++, sPtr += fSynth->fMonitorChans) {
*pLeft++ = (int16)(*sPtr * 32768);
*pRight++ = (int16)(*(sPtr + 1) * 32768);
}
return nSamples;
} }