1 /* wave.h - Copyright (c) 1996-2002 by Timothy J. Weber */
2 
3 #ifndef __WAVE_H
4 #define __WAVE_H
5 
6 /* Headers required to use this module */
7 #include <stdio.h>
8 #include "rifffile.h"
9 
10 /***************************************************************************
11 	macros, constants, and enums
12 ***************************************************************************/
13 
14 /***************************************************************************
15 	typedefs, structs, classes
16 ***************************************************************************/
17 
18 class WaveFile
19 {
20 public:
21 	WaveFile();
22 	~WaveFile();
23 
24 	bool OpenRead(const char* name);
25 	bool OpenWrite(const char* name);
26 	bool ResetToStart();
27 	bool Close();
28 
GetFormatType()29 	unsigned short GetFormatType() const
30 		{ return formatType; };
SetFormatType(unsigned short type)31 	void SetFormatType(unsigned short type)
32 		{ formatType = type; changed = true; };
IsCompressed()33 	bool IsCompressed() const
34 		{ return formatType != 1; };
35 
GetNumChannels()36 	unsigned short GetNumChannels() const
37 		{ return numChannels; };
SetNumChannels(unsigned short num)38 	void SetNumChannels(unsigned short num)
39 		{ numChannels = num; changed = true; };
40 
GetSampleRate()41 	unsigned long GetSampleRate() const
42 		{ return sampleRate; };
SetSampleRate(unsigned long rate)43 	void SetSampleRate(unsigned long rate)
44 		{ sampleRate = rate; changed = true; };
45 
GetBytesPerSecond()46 	unsigned long GetBytesPerSecond() const
47 		{ return bytesPerSecond; };
SetBytesPerSecond(unsigned long bytes)48 	void SetBytesPerSecond(unsigned long bytes)
49 		{ bytesPerSecond = bytes; changed = true; };
50 
GetBytesPerSample()51 	unsigned short GetBytesPerSample() const
52 		{ return bytesPerSample; };
SetBytesPerSample(unsigned short bytes)53 	void SetBytesPerSample(unsigned short bytes)
54 		{ bytesPerSample = bytes; changed = true; };
55 
GetBitsPerChannel()56 	unsigned short GetBitsPerChannel() const
57 		{ return bitsPerChannel; };
SetBitsPerChannel(unsigned short bits)58 	void SetBitsPerChannel(unsigned short bits)
59 		{ bitsPerChannel = bits; changed = true; };
60 
GetNumSamples()61 	unsigned long GetNumSamples() const
62 		{ return (GetBytesPerSample())?
63 			GetDataLength() / GetBytesPerSample(): 0; };
SetNumSamples(unsigned long num)64 	void SetNumSamples(unsigned long num)
65 		{ SetDataLength(num * GetBytesPerSample()); };
66 
GetNumSeconds()67 	float GetNumSeconds() const
68 		{ return GetBytesPerSecond()?
69 			float(GetDataLength()) / GetBytesPerSecond(): 0; };
70 
GetDataLength()71 	unsigned long GetDataLength() const
72 		{ return dataLength; };
SetDataLength(unsigned long numBytes)73 	void SetDataLength(unsigned long numBytes)
74 		{ dataLength = numBytes; changed = true; };
75 
76 	bool FormatMatches(const WaveFile& other);
77 
78 	void CopyFormatFrom(const WaveFile& other);
79 
80 	void SetupFormat(int sampleRate = 44100, short bitsPerChannel = 16, short channels = 1);
81 
GetFile()82 	FILE* GetFile()
83 		{ return readFile? readFile->filep(): writeFile; };
84 
GetRiffFile()85 	RiffFile* GetRiffFile()
86 		{ return readFile? readFile : 0; };
87 
88 	bool WriteHeaderToFile(FILE* fp);
89 
90 	bool ReadSample(unsigned char& sample);
91 	bool WriteSample(unsigned char sample);
92 	bool ReadSample(short& sample);
93 	bool WriteSample(short sample);
94 	bool ReadSample(float& sample);
95 	bool WriteSample(float sample);
96 	bool ReadSample(double& sample);
97 	bool WriteSample(double sample);
98 
99 	bool ReadSamples(unsigned char* samples, size_t count = 1);
100 	bool WriteSamples(unsigned char* samples, size_t count = 1);
101 	bool ReadSamples(short* samples, size_t count = 1);
102 	bool WriteSamples(short* samples, size_t count = 1);
103 
104 	bool ReadRaw(char* buffer, size_t numBytes = 1);
105 	bool WriteRaw(char* buffer, size_t numBytes = 1);
106 
107 	bool GetFirstExtraItem(std::string& type, std::string& value);
108 	bool GetNextExtraItem(std::string& type, std::string& value);
109 
110 	bool CopyFrom(WaveFile& other);
111 
GetError()112 	const char* GetError() const
113 		{ return error; };
ClearError()114 	void ClearError()
115 		{ error = 0; };
116 
117 protected:
118 	RiffFile* readFile;
119 	FILE* writeFile;
120 
121 	unsigned short formatType;
122 	unsigned short numChannels;
123 	unsigned long sampleRate;
124 	unsigned long bytesPerSecond;
125 	unsigned short bytesPerSample;
126 	unsigned short bitsPerChannel;
127 	unsigned long dataLength;
128 
129 	const char* error;
130 	bool changed;  // true if any parameters changed since the header was last written
131 };
132 
133 /***************************************************************************
134 	public variables
135 ***************************************************************************/
136 
137 #ifndef IN_WAVE
138 #endif
139 
140 /***************************************************************************
141 	function prototypes
142 ***************************************************************************/
143 
144 #endif
145 /* __WAVE_H */
146