OSDN Git Service

イニシャルコミット。
[marathon/ShapeFusion.git] / Sounds / SoundsElements.h
1 /*
2  * This file is part of ShapeFusion (Copyright 2000 Tito Dal Canton)
3  *
4  * ShapeFusion is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * ShapeFusion is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with ShapeFusion; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19 #ifndef SOUNDSELEMENTS_H
20 #define SOUNDSELEMENTS_H
21
22 #include <fstream>
23 #include <vector>
24 using std::vector;
25
26 #include "../BigEndianBuffer.h"
27
28 /* ---------- constants */
29
30 #define MAXIMUM_PERMUTATIONS_PER_SOUND          5
31 #define SOUND_FILE_VERSION                                      1
32 #define SIZEOF_sound_file_header                        260
33 #define SIZEOF_sound_definition                         64
34
35 enum /* sound behaviors */
36 {
37         _sound_is_quiet,
38         _sound_is_normal,
39         _sound_is_loud,
40         NUMBER_OF_SOUND_BEHAVIOR_DEFINITIONS
41 };
42
43 enum // Sounds flags
44 {
45         _sound_cannot_be_restarted= 0x0001,
46         _sound_does_not_self_abort= 0x0002,
47         _sound_resists_pitch_changes= 0x0004, // 0.5 external pitch changes
48         _sound_cannot_change_pitch= 0x0008, // no external pitch changes
49         _sound_cannot_be_obstructed= 0x0010, // ignore obstructions
50         _sound_cannot_be_media_obstructed= 0x0020, // ignore media obstructions
51         _sound_is_ambient= 0x0040 // will not be loaded unless _ambient_sound_flag is asserted
52 };
53
54 enum /* sound chances */
55 {
56         _ten_percent= 32768*9/10,
57         _twenty_percent= 32768*8/10,
58         _thirty_percent= 32768*7/10,
59         _fourty_percent= 32768*6/10,
60         _fifty_percent= 32768*5/10,
61         _sixty_percent= 32768*4/10,
62         _seventy_percent= 32768*3/10,
63         _eighty_percent= 32768*2/10,
64         _ninty_percent= 32768*1/10,
65         _always= 0
66 };
67
68 /* ---------- structures */
69
70 /*typedef struct {
71         short sound_index;
72 }  ambient_sound_definition;
73
74 typedef struct {
75         short sound_index;
76 } random_sound_definition;*/
77
78 class SoundsElement // : public wxObject
79 {
80 private:
81         bool    mVerboseLoading;
82
83 protected:
84         // So that subclasses can change their status
85         bool    mGoodData;
86
87 public:
88         SoundsElement(bool verbose) : mVerboseLoading(verbose), mGoodData(false) {}
89         ~SoundsElement(void) {}
90         
91         bool IsGood() const {return mGoodData;}
92         bool IsVerbose() const {return mVerboseLoading;}
93 };
94
95 class AppleSoundHeader : public SoundsElement
96 {
97 private:
98         // Stuff for reading System 7 Sound info
99         /*sampled sound header encoding options*/
100         enum {
101                 standardSoundHeader             = 0x00,
102                 extendedSoundHeader             = 0xFF,
103                 compressedSoundHeader   = 0xFE
104         };
105
106         bool mSixteenBit;
107         bool mStereo;
108         bool mSigned;
109         int mBytesPerFrame;
110         unsigned int mSampleRate; // 16.16 unsigned fixed
111         int mLoopStart;
112         int mLoopEnd;
113         unsigned char mBaseFrequency;
114
115         std::vector<unsigned char> mData;
116
117         bool SaveToWaveOrAiff(wxString path, bool aiff);
118
119 public:
120         AppleSoundHeader(bool verbose = false);
121         ~AppleSoundHeader();
122
123         bool operator==(const AppleSoundHeader& right) const;
124         
125         bool LoadFromFile(wxString path);
126         bool LoadFromWave(wxString path);
127         bool SaveToWave(wxString path) { return SaveToWaveOrAiff(path, false); }
128         bool LoadFromAiff(wxString path);
129         bool SaveToAiff(wxString path) { return SaveToWaveOrAiff(path, true); }
130         void PlaySound(void);
131
132         bool IsSixteenBit() const { return mSixteenBit; }
133         bool IsStereo() const { return mStereo; }
134         bool IsSigned() const { return mSigned; }
135         int GetBytesPerFrame() const { return mBytesPerFrame; }
136         unsigned int GetSampleRate() const { return mSampleRate; }
137         
138         void SetSixteenBit(bool s) { mSixteenBit = s; }
139         void SetStereo(bool s) { mStereo = s; }
140         void SetSigned(bool s) { mSigned = s; }
141         void SetBytesPerFrame(int b) { mBytesPerFrame = b; }
142         void SetSampleRate(unsigned int sr) { mSampleRate = sr; }
143
144         // Utilities
145         unsigned int Size(void);
146         unsigned char* Data(void);
147     BigEndianBuffer& SaveObject(BigEndianBuffer& buffer);
148     BigEndianBuffer& LoadObject(BigEndianBuffer& buffer);
149 };
150
151 class SoundsDefinition : public SoundsElement 
152 {
153 private:
154         /* Marathon Sound Definition */
155         short mSoundCode;
156         short mBehaviorIndex;
157         unsigned short mFlags;
158         unsigned short mChance; // play sound if AbsRandom()>=chance
159         /* if low_pitch==0, use FIXED_ONE;
160          * if high_pitch==0 use low pitch;
161          * else choose in [low_pitch,high_pitch]
162          */
163         float mLowPitch, mHighPitch;
164         /* filled in later */
165 //      short mPermutations;
166         unsigned short mPermutationsPlayed;
167 //      int mGroupOffset, mSingleLength, mTotalLength; // magic numbers necessary to load sounds
168 //      std::vector<int> mSoundOffsets;
169         std::vector<AppleSoundHeader> mSounds;
170         unsigned int mLastPlayed; // machine ticks
171         // Pointer to loaded sound and size of sound object pointed to
172 //      short *ptr;
173 //      int size;
174         
175         bool mRemap8bit;
176
177 public:
178         SoundsDefinition(bool verbose = false);
179         ~SoundsDefinition();
180         
181         bool HaveSameAttributesAs(const SoundsDefinition& right) const;
182         bool HaveSameSoundsAs(const SoundsDefinition& right) const;
183         bool operator== (const SoundsDefinition& right) const;
184         bool operator!=(const SoundsDefinition& right) const;
185         
186         short GetSoundCode(void) const {return mSoundCode;}
187         short GetBehaviorIndex(void) const {return mBehaviorIndex;}
188         
189         bool IsNotRestartable(void) const {return mFlags & _sound_cannot_be_restarted;}
190         bool IsNotSelfAbortable(void) const {return mFlags & _sound_does_not_self_abort;}
191         bool IsPitchChangeResistant(void) const {return mFlags & _sound_resists_pitch_changes;}
192         bool IsNotPitchChangeable(void) const {return mFlags & _sound_cannot_change_pitch;}
193         bool IsNotObstructed(void) const {return mFlags & _sound_cannot_be_obstructed;}
194         bool IsNotMediaObstructed(void) const {return mFlags & _sound_cannot_be_media_obstructed;}
195         bool IsAmbient(void) const {return mFlags & _sound_is_ambient;}
196         
197         short GetChance(void) const;
198         
199         float GetLowPitch(void) const {return mLowPitch;}
200         float GetHighPitch(void) const {return mHighPitch;}
201         
202         void SetSoundCode(short s) {mSoundCode = s;}
203         void SetBehaviorIndex(int i) {mBehaviorIndex = i;}
204         
205         void SetNotRestartable(bool f) {mFlags |= _sound_cannot_be_restarted;}
206         void SetNotSelfAbortable(bool f) {mFlags |= _sound_does_not_self_abort;}
207         void SetPitchChangeResistant(bool b) {mFlags |= _sound_resists_pitch_changes;}
208         void SetNotPitchChangeable(bool b) {mFlags |= _sound_cannot_change_pitch;}
209         void SetNotObstructed(bool b) {mFlags |= _sound_cannot_be_obstructed;}
210         void SetNotMediaObstructed(bool b) {mFlags |= _sound_cannot_be_media_obstructed;}
211         void SetAmbient(bool b) {mFlags |= _sound_is_ambient;}
212         
213         void SetChance(short i);
214         
215         void SetLowPitch(int p) {mLowPitch = p;}
216         void SetHighPitch(int p) {mHighPitch = p;}
217         
218         unsigned int GetPermutationCount(void) const {return mSounds.size();}
219         void DeletePermutation(unsigned int permutation_index);
220         AppleSoundHeader* GetPermutation(unsigned int permutation_index);
221         AppleSoundHeader* NewPermutation(wxString path);
222         
223         // Utilities
224         unsigned int GetSizeInFile(void);
225     BigEndianBuffer& SaveObject(BigEndianBuffer& buffer, unsigned int& offset);
226     BigEndianBuffer& LoadObject(BigEndianBuffer& buffer);
227 };
228
229 #endif
230