Skip to content

Commit acdb74f

Browse files
committed
DLSFile: export mono
DLS Level 1 only supports mono samples
1 parent 9dc6aed commit acdb74f

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

src/main/DLSFile.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ uint32_t DLSFile::GetSize(void) {
8888
}
8989

9090

91-
int DLSFile::WriteDLSToBuffer(vector<uint8_t> &buf) {
91+
int DLSFile::WriteDLSToBuffer(vector<uint8_t> &buf, bool mono) {
9292
uint32_t theDWORD;
9393

9494
PushTypeOnVectBE<uint32_t>(buf, 0x52494646); //"RIFF"
@@ -123,7 +123,7 @@ int DLSFile::WriteDLSToBuffer(vector<uint8_t> &buf) {
123123
theDWORD += aWaves[i]->GetSize(); //each "wave" list
124124
WriteLIST(buf, 0x7776706C, theDWORD); //Write the "wvpl" LIST
125125
for (uint32_t i = 0; i < aWaves.size(); i++)
126-
aWaves[i]->Write(buf); //Write each "wave" list
126+
aWaves[i]->Write(buf, mono); //Write each "wave" list
127127

128128
theDWORD = 12 + (uint32_t) name.size(); //"INFO" + "INAM" + size + the string size
129129
WriteLIST(buf, 0x494E464F, theDWORD); //write the "INFO" list
@@ -463,14 +463,39 @@ uint32_t DLSWave::GetSize() {
463463
return size;
464464
}
465465

466-
void DLSWave::Write(vector<uint8_t> &buf) {
466+
void DLSWave::GetChannelData(vector<uint8_t> &buf, uint8_t whichChannel) {
467+
uint32_t chSize = dataSize / wChannels;
468+
PushTypeOnVect<uint32_t>(buf, chSize);
469+
470+
uint8_t bytesPerSample = wBitsPerSample / 8;
471+
uint8_t skip = wChannels * bytesPerSample;
472+
for (uint32_t i = whichChannel * bytesPerSample; i < dataSize; i += skip) {
473+
buf.insert(buf.end(), &data[i], &data[i + bytesPerSample]);
474+
}
475+
}
476+
477+
void DLSWave::WriteSample(vector<uint8_t> &buf, bool mono) {
478+
if (false && (wChannels != 1 && mono)) {
479+
// Just get the left sample.
480+
GetChannelData(buf, 0);
481+
} else {
482+
PushTypeOnVect<uint32_t>(buf, dataSize); //size: this is the ACTUAL size, not the even-aligned size
483+
if (dataSize % 2)
484+
buf.push_back(0);
485+
buf.insert(buf.end(), data, data + dataSize); //Write the sample
486+
}
487+
}
488+
489+
void DLSWave::Write(vector<uint8_t> &buf, bool mono) {
490+
mono = false;
467491
uint32_t theDWORD;
492+
uint16_t numChannels = mono ? 1 : wChannels;
468493

469494
RiffFile::WriteLIST(buf, 0x77617665, GetSize() - 8); //write "wave" list
470495
PushTypeOnVectBE<uint32_t>(buf, 0x666D7420); //"fmt "
471496
PushTypeOnVect<uint32_t>(buf, 18); //size
472497
PushTypeOnVect<uint16_t>(buf, wFormatTag); //wFormatTag
473-
PushTypeOnVect<uint16_t>(buf, wChannels); //wChannels
498+
PushTypeOnVect<uint16_t>(buf, numChannels); //wChannels
474499
PushTypeOnVect<uint32_t>(buf, dwSamplesPerSec); //dwSamplesPerSec
475500
PushTypeOnVect<uint32_t>(buf, dwAveBytesPerSec); //dwAveBytesPerSec
476501
PushTypeOnVect<uint16_t>(buf, wBlockAlign); //wBlockAlign
@@ -482,10 +507,7 @@ void DLSWave::Write(vector<uint8_t> &buf) {
482507
Wsmp->Write(buf); //write the "wsmp" chunk
483508

484509
PushTypeOnVectBE<uint32_t>(buf, 0x64617461); //"data"
485-
PushTypeOnVect<uint32_t>(buf, dataSize); //size: this is the ACTUAL size, not the even-aligned size
486-
buf.insert(buf.end(), data, data + dataSize); //Write the sample
487-
if (dataSize % 2)
488-
buf.push_back(0);
510+
WriteSample(buf, mono);
489511
theDWORD = 12 + (uint32_t) name.size(); //"INFO" + "INAM" + size + the string size
490512
RiffFile::WriteLIST(buf, 0x494E464F, theDWORD); //write the "INFO" list
491513
PushTypeOnVectBE<uint32_t>(buf, 0x494E414D); //"INAM"

src/main/DLSFile.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class DLSFile: public RiffFile {
9090

9191
uint32_t GetSize(void);
9292

93-
int WriteDLSToBuffer(std::vector<uint8_t> &buf);
93+
int WriteDLSToBuffer(std::vector<uint8_t> &buf, bool mono = false);
9494
bool SaveDLSFile(const std::wstring &filepath);
9595

9696
public:
@@ -266,7 +266,7 @@ class DLSWave {
266266
return dataSize;
267267
}
268268
uint32_t GetSize(void);
269-
void Write(std::vector<uint8_t> &buf);
269+
void Write(std::vector<uint8_t> &buf, bool mono);
270270

271271
private:
272272
DLSWsmp *Wsmp;
@@ -282,5 +282,8 @@ class DLSWave {
282282
unsigned char *data;
283283

284284
std::string name;
285+
286+
void GetChannelData(std::vector<uint8_t> &buf, uint8_t whichChannel);
287+
void WriteSample(vector<uint8_t> &buf, bool mono);
285288
};
286289

src/ui/windows/MusicPlayer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ void MusicPlayer::ChangeDLS(DLSFile* dlsfile)
295295
//Load in the new guy
296296
vector<BYTE> dlsBuf;
297297
dlsBuf.reserve(dlsfile->GetSize());
298-
dlsfile->WriteDLSToBuffer(dlsBuf);
298+
dlsfile->WriteDLSToBuffer(dlsBuf, true);
299299

300300
HRESULT result = DLSLoader.LoadDLSFromMem(&dlsBuf[0], dlsfile->GetSize(), Collection); // Loads the standard GM set
301301
//DLSLoader.LoadDLS(_T("TEST.dls"),Collection); // Loads the standard GM set

0 commit comments

Comments
 (0)