SynthFile

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@653 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Pfeiffer 2002-08-08 16:19:57 +00:00
parent 0723a9ac98
commit f221d365d8
2 changed files with 317 additions and 0 deletions

View File

@ -0,0 +1,186 @@
/*
SynthFile.cpp
Copyright (c) 2002 OpenBeOS.
Author:
Michael Pfeiffer
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <Debug.h>
#include "SynthFile.h"
#include "SynthFileReader.h"
// SSound
SSound::SSound(uint16 id)
: fOffset(0)
, fId(id)
, fSamples(NULL)
, fFrameCount(0)
, fSampleSize(0)
, fChannelCount(0)
{
}
void
SSound::SetSample(void* samples, int32 frameCount, int16 sampleSize, int16 channelCount)
{
ASSERT(fSamples == NULL);
fSamples = samples;
fFrameCount = frameCount;
fSampleSize = sampleSize;
fChannelCount = channelCount;
}
// SSoundInRange
SSoundInRange::SSoundInRange(uint8 start, uint8 end, SSound* sound)
: fNoteStart(start)
, fNoteEnd(end)
, fSound(sound)
{
}
// SInstrument
SInstrument::SInstrument(uint8 instrument)
: fOffset(0)
, fId(instrument)
, fDefaultSound(NULL)
, fSounds(false) // does not own items
{
}
// SSynthFile
static int bySoundId(const SSound** a, const SSound** b)
{
return (*a)->Id() - (*b)->Id();
}
SSynthFile::SSynthFile(const char* fileName)
: fReader(fileName)
{
fStatus = fReader.InitCheck();
if (fStatus == B_OK) {
for (int i = 0; i < 128; i++) {
fInstruments.AddItem(NULL);
}
fStatus = fReader.Initialize(this);
fSounds.SortItems(bySoundId);
}
}
SSound*
SSynthFile::FindSound(uint16 sound)
{
const int n = fSounds.CountItems();
for (int i = 0; i < n; i ++) {
SSound* s = fSounds.ItemAt(i);
if (s->Id() == sound) return s;
}
return NULL;
}
SSound*
SSynthFile::GetSound(uint16 sound)
{
SSound* s = FindSound(sound);
if (s == NULL) {
s = new SSound(sound);
fSounds.AddItem(s);
}
return s;
}
void
SSynthFile::InstrumentAtPut(int i, SInstrument* instr)
{
ASSERT(fInstruments.CountItems() == 128);
SInstrument** array = (SInstrument**)fInstruments.Items();
array[i] = instr;
}
bool
SSynthFile::HasInstrument(uint8 instrument) const
{
ASSERT(instrument < 128);
return fInstruments.ItemAt(instrument) != NULL;
}
SInstrument*
SSynthFile::GetInstrument(uint8 instrument)
{
ASSERT(instrument < 128);
SInstrument* i = fInstruments.ItemAt(instrument);
if (i == NULL) {
i = new SInstrument(instrument);
InstrumentAtPut(instrument, i);
}
return i;
}
void
SSynthFile::Dump()
{
printf("SynthFile:\n");
printf("==========\n\n");
for (uint8 i = 0; i < 128; i++) {
if (HasInstrument(i)) {
SInstrument* instr = GetInstrument(i);
printf("Instrument id=%d name=%s sounds=%d\n",
(int)instr->Id(),
instr->Name(),
(int)instr->Sounds()->CountItems());
SSound* sound = instr->DefaultSound();
ASSERT(sound != NULL);
printf("Default Sound id=%d name='%s'\n",
(int)sound->Id(),
sound->Name());
for (int s = 0; s < instr->Sounds()->CountItems(); s ++) {
SSoundInRange* range = instr->Sounds()->ItemAt(s);
printf("Sound id=%d start=%d end=%d name='%s'\n",
(int)range->Sound()->Id(),
(int)range->Start(),
(int)range->End(),
range->Sound()->Name());
}
printf("\n");
}
}
printf("\nSounds:\n");
printf("-------\n\n");
for (int i = 0; i < fSounds.CountItems(); i ++) {
SSound* sound = fSounds.ItemAt(i);
printf("Id=%d name='%s'\n", (int)sound->Id(), sound->Name());
}
}

View File

@ -0,0 +1,131 @@
/*
SynthFile.h
Copyright (c) 2002 OpenBeOS.
Author:
Michael Pfeiffer
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef _SYNTH_FILE_H
#define _SYNTH_FILE_H
#include "SynthFileReader.h"
#include "TList.h"
class SSound {
uint32 fOffset;
uint16 fId;
BString fName;
void* fSamples;
int32 fFrameCount;
int16 fSampleSize;
int16 fChannelCount;
SSound() { }
public:
SSound(uint16 id);
uint16 Id() const { return fId; }
void SetName(const char* name) { fName = name; }
const char* Name() const { return fName.String(); }
void SetSample(void* samples, int32 frameCount, int16 sampleSize, int16 channelCount);
void* Samples() const { return fSamples; }
int32 FrameCount() const { return fFrameCount; }
int16 SampleSize() const { return fSampleSize; }
int16 ChannelCount() const { return fChannelCount; }
bool HasSamples() const { return fSamples != NULL; }
void SetOffset(uint32 offset) { fOffset = offset; }
uint32 Offset() const { return fOffset; }
};
class SSoundInRange {
uint8 fNoteStart;
uint8 fNoteEnd;
SSound* fSound;
SSoundInRange() { }
public:
SSoundInRange(uint8 start, uint8 end, SSound* sound);
uint8 Start() const { return fNoteStart; }
uint8 End() const { return fNoteEnd; }
SSound* Sound() const { return fSound; }
};
class SInstrument {
uint32 fOffset;
uint8 fId;
BString fName;
SSound* fDefaultSound;
TList<SSoundInRange> fSounds;
public:
SInstrument(uint8 instrument);
void SetOffset(uint32 offset) { fOffset = offset; }
uint32 Offset() const { return fOffset; }
uint8 Id() const { return fId; }
void SetName(const char* name) { fName = name; }
const char* Name() const { return fName.String(); }
void SetDefaultSound(SSound* sound) { fDefaultSound = sound; }
SSound* DefaultSound() const { return fDefaultSound; }
TList<SSoundInRange>* Sounds() { return &fSounds; }
};
class SSynthFile;
class SSynthFile {
TList<SInstrument> fInstruments;
TList<SSound> fSounds;
SSynthFileReader fReader;
status_t fStatus;
void InstrumentAtPut(int i, SInstrument* instr);
SSound* FindSound(uint16 sound);
public:
SSynthFile(const char* fileName);
status_t InitCheck() const { return fReader.InitCheck(); }
SSound* GetSound(uint16 sound);
bool HasInstrument(uint8 instrument) const;
SInstrument* GetInstrument(uint8 instrument);
void Dump();
};
#endif