1 /* 2 3 SynthFile.cpp 4 5 Copyright (c) 2002 OpenBeOS. 6 7 Author: 8 Michael Pfeiffer 9 10 Permission is hereby granted, free of charge, to any person obtaining a copy of 11 this software and associated documentation files (the "Software"), to deal in 12 the Software without restriction, including without limitation the rights to 13 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 of the Software, and to permit persons to whom the Software is furnished to do 15 so, subject to the following conditions: 16 17 The above copyright notice and this permission notice shall be included in all 18 copies or substantial portions of the Software. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 THE SOFTWARE. 27 28 */ 29 30 #include <Debug.h> 31 32 #include "SynthFile.h" 33 #include "SynthFileReader.h" 34 35 // SSound 36 37 SSound::SSound(uint16 id) 38 : fOffset(0) 39 , fId(id) 40 , fSamples(NULL) 41 , fFrameCount(0) 42 , fSampleSize(0) 43 , fChannelCount(0) 44 { 45 } 46 47 48 void 49 SSound::SetSample(void* samples, int32 frameCount, int16 sampleSize, int16 channelCount) 50 { 51 ASSERT(fSamples == NULL); 52 fSamples = samples; 53 fFrameCount = frameCount; 54 fSampleSize = sampleSize; 55 fChannelCount = channelCount; 56 } 57 58 // SSoundInRange 59 SSoundInRange::SSoundInRange(uint8 start, uint8 end, SSound* sound) 60 : fNoteStart(start) 61 , fNoteEnd(end) 62 , fSound(sound) 63 { 64 } 65 66 67 // SInstrument 68 69 SInstrument::SInstrument(uint8 instrument) 70 : fOffset(0) 71 , fId(instrument) 72 , fDefaultSound(NULL) 73 , fSounds(false) // does not own items 74 { 75 } 76 77 78 // SSynthFile 79 80 static int bySoundId(const SSound** a, const SSound** b) 81 { 82 return (*a)->Id() - (*b)->Id(); 83 } 84 85 SSynthFile::SSynthFile(const char* fileName) 86 : fReader(fileName) 87 { 88 fStatus = fReader.InitCheck(); 89 if (fStatus == B_OK) { 90 for (int i = 0; i < 128; i++) { 91 fInstruments.AddItem(NULL); 92 } 93 94 fStatus = fReader.Initialize(this); 95 fSounds.SortItems(bySoundId); 96 } 97 } 98 99 SSound* 100 SSynthFile::FindSound(uint16 sound) 101 { 102 const int n = fSounds.CountItems(); 103 for (int i = 0; i < n; i ++) { 104 SSound* s = fSounds.ItemAt(i); 105 if (s->Id() == sound) return s; 106 } 107 return NULL; 108 } 109 110 SSound* 111 SSynthFile::GetSound(uint16 sound) 112 { 113 SSound* s = FindSound(sound); 114 if (s == NULL) { 115 s = new SSound(sound); 116 fSounds.AddItem(s); 117 } 118 return s; 119 } 120 121 122 void 123 SSynthFile::InstrumentAtPut(int i, SInstrument* instr) 124 { 125 ASSERT(fInstruments.CountItems() == 128); 126 SInstrument** array = (SInstrument**)fInstruments.Items(); 127 array[i] = instr; 128 } 129 130 131 bool 132 SSynthFile::HasInstrument(uint8 instrument) const 133 { 134 ASSERT(instrument < 128); 135 return fInstruments.ItemAt(instrument) != NULL; 136 } 137 138 SInstrument* 139 SSynthFile::GetInstrument(uint8 instrument) 140 { 141 ASSERT(instrument < 128); 142 SInstrument* i = fInstruments.ItemAt(instrument); 143 if (i == NULL) { 144 i = new SInstrument(instrument); 145 InstrumentAtPut(instrument, i); 146 } 147 return i; 148 } 149 150 void 151 SSynthFile::Dump() 152 { 153 printf("SynthFile:\n"); 154 printf("==========\n\n"); 155 for (uint8 i = 0; i < 128; i++) { 156 if (HasInstrument(i)) { 157 SInstrument* instr = GetInstrument(i); 158 printf("Instrument id=%d name=%s sounds=%d\n", 159 (int)instr->Id(), 160 instr->Name(), 161 (int)instr->Sounds()->CountItems()); 162 SSound* sound = instr->DefaultSound(); 163 ASSERT(sound != NULL); 164 printf("Default Sound id=%d name='%s'\n", 165 (int)sound->Id(), 166 sound->Name()); 167 for (int s = 0; s < instr->Sounds()->CountItems(); s ++) { 168 SSoundInRange* range = instr->Sounds()->ItemAt(s); 169 printf("Sound id=%d start=%d end=%d name='%s'\n", 170 (int)range->Sound()->Id(), 171 (int)range->Start(), 172 (int)range->End(), 173 range->Sound()->Name()); 174 } 175 printf("\n"); 176 } 177 } 178 179 printf("\nSounds:\n"); 180 printf("-------\n\n"); 181 for (int i = 0; i < fSounds.CountItems(); i ++) { 182 SSound* sound = fSounds.ItemAt(i); 183 printf("Id=%d name='%s'\n", (int)sound->Id(), sound->Name()); 184 } 185 } 186 187