1 /* 2 3 SynthFile.cpp 4 5 Copyright (c) 2002 Haiku. 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 fInstruments.ReplaceItem(i, instr); 127 } 128 129 130 bool 131 SSynthFile::HasInstrument(uint8 instrument) const 132 { 133 ASSERT(instrument < 128); 134 return fInstruments.ItemAt(instrument) != NULL; 135 } 136 137 SInstrument* 138 SSynthFile::GetInstrument(uint8 instrument) 139 { 140 ASSERT(instrument < 128); 141 SInstrument* i = fInstruments.ItemAt(instrument); 142 if (i == NULL) { 143 i = new SInstrument(instrument); 144 InstrumentAtPut(instrument, i); 145 } 146 return i; 147 } 148 149 void 150 SSynthFile::Dump() 151 { 152 printf("SynthFile:\n"); 153 printf("==========\n\n"); 154 for (uint8 i = 0; i < 128; i++) { 155 if (HasInstrument(i)) { 156 SInstrument* instr = GetInstrument(i); 157 printf("Instrument id=%d name=%s sounds=%d\n", 158 (int)instr->Id(), 159 instr->Name(), 160 (int)instr->Sounds()->CountItems()); 161 SSound* sound = instr->DefaultSound(); 162 ASSERT(sound != NULL); 163 printf("Default Sound id=%d name='%s'\n", 164 (int)sound->Id(), 165 sound->Name()); 166 for (int s = 0; s < instr->Sounds()->CountItems(); s ++) { 167 SSoundInRange* range = instr->Sounds()->ItemAt(s); 168 printf("Sound id=%d start=%d end=%d name='%s'\n", 169 (int)range->Sound()->Id(), 170 (int)range->Start(), 171 (int)range->End(), 172 range->Sound()->Name()); 173 } 174 printf("\n"); 175 } 176 } 177 178 printf("\nSounds:\n"); 179 printf("-------\n\n"); 180 for (int i = 0; i < fSounds.CountItems(); i ++) { 181 SSound* sound = fSounds.ItemAt(i); 182 printf("Id=%d name='%s'\n", (int)sound->Id(), sound->Name()); 183 } 184 } 185 186