Nsound  0.9.4
Wavefile.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: Wavefile.h 900 2015-06-13 19:01:17Z weegreenblobbie $
4 //
5 // Nsound is a C++ library and Python module for audio synthesis featuring
6 // dynamic digital filters. Nsound lets you easily shape waveforms and write
7 // to disk or plot them. Nsound aims to be as powerful as Csound but easy to
8 // use.
9 //
10 // Copyright (c) 2004-2006 Nick Hilton
11 //
12 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
13 //
14 //-----------------------------------------------------------------------------
15 
16 //-----------------------------------------------------------------------------
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 // This program is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 // GNU Library General Public License for more details.
27 //
28 // You should have received a copy of the GNU General Public License
29 // along with this program; if not, write to the Free Software
30 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 //
32 //-----------------------------------------------------------------------------
33 
34 //-----------------------------------------------------------------------------
35 //
36 // This class reads and writes RIFF wave files with the following format:
37 //
38 // --------------------------------------------------------------------
39 // 0 | 'R' | 'I' | 'F' | 'F' |
40 // --------------------------------------------------------------------
41 // 4 | RIFF chunk size |
42 // --------------------------------------------------------------------
43 // 8 | 'W' | 'A' | 'V' | 'E' |
44 // --------------------------------------------------------------------
45 //
46 // Required format chunk:
47 //
48 // --------------------------------------------------------------------
49 // 0 | 'f' | 'm' | 't' | ' ' |
50 // --------------------------------------------------------------------
51 // 4 | FORMAT Chunk Length (16, 18, 30, 40) |
52 // --------------------------------------------------------------------
53 // 8 | Format Tag: 1=PCM, 3=IEEE_FLOAT | n_channels 1, 2, ... |
54 // --------------------------------------------------------------------
55 // 12 | Sample Rate |
56 // --------------------------------------------------------------------
57 // 16 | Average # of Bytes P/Second (Sample rate*Channels*(Bits/8) |
58 // --------------------------------------------------------------------
59 // 20 | Block Align ((Bits/8)*Channels) | Bits per Sample (8 ... 64) |
60 // --------------------------------------------------------------------
61 // 24 | optional data if FORMAT Chunk Length is 18 or 40 |
62 // --------------------------------------------------------------------
63 //
64 // Required data chunk, the raw audio data:
65 //
66 // --------------------------------------------------------------------
67 // 0 | 'd' | 'a' | 't' | 'a' |
68 // --------------------------------------------------------------------
69 // 4 | Data Length (actual length of raw data) |
70 // --------------------------------------------------------------------
71 // 8 | |
72 // | |
73 // | |
74 // | raw data |
75 // | |
76 // | |
77 // | |
78 // ----------------------------------EOF-------------------------------
79 //
80 // Optional 'TAG' chunk (aka ID3v1), all fields are plain ASCII unless
81 // otherwise stated.
82 //
83 // ----------------------------------------------------
84 // 0 | 'T' | 'A' | 'G' |
85 // --------------------------------------------------------------------
86 // 3 | Title - 30 bytes |
87 // --------------------------------------------------------------------
88 // 33 | Artist - 30 bytes |
89 // --------------------------------------------------------------------
90 // 63 | Album - 30 bytes |
91 // --------------------------------------------------------------------
92 // 93 | Year - 4 bytes |
93 // --------------------------------------------------------------------
94 // 97 | Comment - 30 bytes |
95 // --------------------------------------------------------------------
96 //127 | Genre - 1 byte |
97 // ------------------
98 //
99 // Total TAG size is 128 bytes.
100 //
101 // All other RIFF WAVE chunk ids are skipped.
102 //
103 //-----------------------------------------------------------------------------
104 #ifndef _NSOUND_WAVEFILE_H_
105 #define _NSOUND_WAVEFILE_H_
106 
107 #include <Nsound/Nsound.h>
108 
109 namespace Nsound
110 {
111 
112 class AudioStream;
113 class Buffer;
114 
116 class Wavefile
117 {
118  public:
119 
120  // Constants
121 
122  static const uint32 DATA_ = 1635017060;
123  static const uint32 FACT_ = 1952670054;
124  static const uint32 FMT_ = 544501094;
125  static const uint32 PEAK_ = 1262568784;
126  static const uint32 RIFF_ = 1179011410;
127  static const uint32 WAVE_ = 1163280727;
128 
129  static const uint16 WAVE_FORMAT_PCM_ = 0x0001;
130  static const uint16 WAVE_FORMAT_IEEE_FLOAT_ = 0x0003;
131 
132  // Windows won't allow floats to be initialized in the hearder.
133  static const raw_float64 SIGNED_64_BIT_; // = 9223372036854775807.0;
134  static const raw_float64 SIGNED_48_BIT_; // = 140737488355327.0;
135  static const raw_float64 SIGNED_32_BIT_; // = 2147483647.0;
136  static const raw_float64 SIGNED_24_BIT_; // = 8388607.0;
137  static const raw_float64 SIGNED_16_BIT_; // = 32767.0;
138  static const raw_float64 SIGNED_8_BIT_; // = 127.0;
139 
140  static const raw_uint64 UNSIGNED_64_BIT_ = 18446744073709551615ULL;
141  static const raw_uint64 UNSIGNED_48_BIT_ = 281474976710655ULL;
142  static const raw_uint64 UNSIGNED_32_BIT_ = 4294967295ULL;
143  static const raw_uint64 UNSIGNED_24_BIT_ = 16777215ULL;
144  static const raw_uint64 UNSIGNED_16_BIT_ = 65535ULL;
145  static const raw_uint64 UNSIGNED_8_BIT_ = 255ULL;
146 
147  static
148  std::string
149  decodeFormatTag(const uint16 format_tag);
150 
151  static
152  uint32
154 
155  static
156  uint32
158 
159  static
160  void
161  setDefaultSampleRate(const int32 rate);
162 
163  static
164  void
166  {setDefaultSampleRate(static_cast<int32>(rate));};
167 
168  static
169  void
171 
172  static
173  void
174  setIEEEFloat(boolean flag);
175 
176  static
177  void
178  setDefaults(
179  const float64 & sample_rate = 44100.0,
180  const float64 & sample_bits = 16.0,
181  const boolean & use_ieee_floats = false);
182 
183  // read(std::string file_name)
184  //
185  // This method opens the wavefile specified by file_name and loads
186  // the waveform into memory. This methods returns true if the
187  // wavefile was successfully read in.
188  //
189  static
190  boolean
191  read(const std::string & fileName, AudioStream & astream);
192 
194  //
196  static
197  boolean
198  readHeader(const std::string & filename, std::string & info);
199 
200  // write(std::string file_name)
201  //
202  // This method writes pulse code modulation (PCM) to the file
203  // specified by filename. If file_name exists already, it will be
204  // overwritten.
205  //
206  static
207  boolean
208  write(const std::string & fileName,
209  const AudioStream & as,
210  uint32 bits_per_sample = 16);
211 
212  // write(std::string file_name)
213  //
214  // This method writes pulse code modulation (PCM) to the file
215  // specified by filename. If file_name exists already, it will be
216  // overwritten.
217  //
218  static
219  boolean
220  write(const std::string & fileName,
221  const Buffer & as,
222  uint32 bits_per_sample,
223  uint32 sample_rate);
224 
225  #ifndef SWIG
226  friend Buffer & operator<<(Buffer & lhs, const char * rhs);
227  friend void operator>>(const Buffer & lhs, const char * rhs);
228  friend AudioStream & operator<<(AudioStream & lhs, const char * rhs);
229  friend void operator>>(const AudioStream & lhs, const char * rhs);
230  #endif
231 
232  protected:
233 
234  static uint32 default_sample_rate_; // = 44100;
235  static uint32 default_sample_size_; // = 16;
236  static uint16 default_wave_format_; // = WAVE_FORMAT_PCM_;
237 
238  static
239  boolean
240  read(
241  const std::string & filename,
242  std::vector<Buffer *> * b_vector,
243  AudioStream * as,
244  std::stringstream * out);
245 
246 }; // Wavefile
247 
248 // Must declare friend functions here to give them proper namespace scope.
249 Buffer & operator<<(Buffer & lhs, const char * rhs);
250 void operator>>(const Buffer & lhs, const char * rhs);
251 AudioStream & operator<<(AudioStream & lhs, const char * rhs);
252 void operator>>(const AudioStream & lhs, const char * rhs);
253 
254 class ID3v1Tag
255 {
256  public:
257 
258  ID3v1Tag(const std::string & filename = "", boolean show_warnings = true);
259 
261  boolean
262  read(const std::string & filename, boolean show_warnings = true);
263 
265  boolean
266  write(const std::string & filename, boolean show_warnings = true);
267 
268  std::string title;
269  std::string artist;
270  std::string album;
271  std::string year;
272  std::string comment;
273  char genre;
274 
275  #ifndef SWIG
276  friend
279  std::ostream &
280  operator<<(std::ostream & out, const ID3v1Tag & rhs);
281  #endif
282 };
283 
284 std::ostream &
285 operator<<(std::ostream & out, const ID3v1Tag & rhs);
286 
287 }; // Nsound
288 
289 #endif
290 
291 
292 //-----------------------------------------------------------------------------
293 // To get the tag constants I used this C program:
294 //
295 // #include <stdio.h>
296 //
297 // int
298 // main(int argc, char ** argv)
299 // {
300 // const unsigned int N_TAGS = 6;
301 //
302 // char * tags[N_TAGS];
303 //
304 // unsigned int i = 0;
305 //
306 // tags[0] = "RIFF";
307 // tags[1] = "WAVE";
308 // tags[2] = "fmt ";
309 // tags[3] = "data";
310 // tags[4] = "PEAK";
311 // tags[5] = "fact";
312 //
313 // for(i = 0; i < N_TAGS; ++i)
314 // {
315 // unsigned int * j = (unsigned int *)(tags[i]);
316 //
317 // printf("%s = %d\n", tags[i], *j);
318 // }
319 //
320 // return 0;
321 // }
static boolean read(const std::string &fileName, AudioStream &astream)
Definition: Wavefile.cc:920
unsigned int uint32
Definition: Nsound.h:153
static const raw_uint64 UNSIGNED_24_BIT_
Definition: Wavefile.h:143
static const raw_float64 SIGNED_48_BIT_
Definition: Wavefile.h:134
static const raw_float64 SIGNED_24_BIT_
Definition: Wavefile.h:136
static const uint32 RIFF_
Definition: Wavefile.h:126
std::ostream & operator<<(std::ostream &out, const Buffer &rhs_buffer)
Definition: Buffer.cc:1338
static boolean readHeader(const std::string &filename, std::string &info)
Reads the basic header information and sets the info string.
Definition: Wavefile.cc:934
std::string title
Definition: Wavefile.h:268
friend void operator>>(const Buffer &lhs, const char *rhs)
static const raw_uint64 UNSIGNED_16_BIT_
Definition: Wavefile.h:144
static const raw_float64 SIGNED_16_BIT_
Definition: Wavefile.h:137
static const uint16 WAVE_FORMAT_PCM_
Definition: Wavefile.h:129
double float64
Definition: Nsound.h:146
static void setDefaultSampleRate(const float64 &rate)
Definition: Wavefile.h:165
static uint16 default_wave_format_
Definition: Wavefile.h:236
static const raw_float64 SIGNED_32_BIT_
Definition: Wavefile.h:135
static boolean write(const std::string &fileName, const AudioStream &as, uint32 bits_per_sample=16)
Definition: Wavefile.cc:952
static uint32 default_sample_size_
Definition: Wavefile.h:235
static const uint32 PEAK_
Definition: Wavefile.h:125
static const raw_uint64 UNSIGNED_8_BIT_
Definition: Wavefile.h:145
static const raw_uint64 UNSIGNED_64_BIT_
Definition: Wavefile.h:140
static std::string decodeFormatTag(const uint16 format_tag)
Definition: Wavefile.cc:1309
void operator>>(const AudioStream &lhs, AudioPlayback &rhs)
boolean write(const std::string &filename, boolean show_warnings=true)
Returns true if it successfully wrote the tag to the end of the file, false otherwise.
Definition: Wavefile.cc:1764
static const uint32 FACT_
Definition: Wavefile.h:123
static void setDefaults(const float64 &sample_rate=44100.0, const float64 &sample_bits=16.0, const boolean &use_ieee_floats=false)
Definition: Wavefile.cc:117
Very simple Wavefile reading class.
Definition: Wavefile.h:116
static uint32 getDefaultSampleRate()
Definition: Wavefile.h:153
static void setIEEEFloat(boolean flag)
Definition: Wavefile.cc:98
std::string artist
Definition: Wavefile.h:269
unsigned long long raw_uint64
Definition: Nsound.h:158
friend std::ostream & operator<<(std::ostream &out, const ID3v1Tag &rhs)
Sends the contents of the Buffer to the output stream.
static const raw_float64 SIGNED_8_BIT_
Definition: Wavefile.h:138
static const raw_float64 SIGNED_64_BIT_
Definition: Wavefile.h:133
A Buffer for storing audio samples.
Definition: Buffer.h:60
static const uint32 DATA_
Definition: Wavefile.h:122
static const raw_uint64 UNSIGNED_32_BIT_
Definition: Wavefile.h:142
signed int int32
Definition: Nsound.h:142
static uint32 default_sample_rate_
Definition: Wavefile.h:234
double raw_float64
Definition: Nsound.h:159
ID3v1Tag(const std::string &filename="", boolean show_warnings=true)
Definition: Wavefile.cc:1532
static const raw_uint64 UNSIGNED_48_BIT_
Definition: Wavefile.h:141
static void setDefaultSampleRate(const int32 rate)
Definition: Wavefile.cc:70
static void setDefaultSampleSize(uint32 size)
Definition: Wavefile.cc:79
boolean read(const std::string &filename, boolean show_warnings=true)
Returns true if it found the tag, false otherwise.
Definition: Wavefile.cc:1663
std::string comment
Definition: Wavefile.h:272
static uint32 getDefaultSampleSize()
Definition: Wavefile.h:157
static const uint16 WAVE_FORMAT_IEEE_FLOAT_
Definition: Wavefile.h:130
static const uint32 FMT_
Definition: Wavefile.h:124
friend Buffer & operator<<(Buffer &lhs, const char *rhs)
unsigned short uint16
Definition: Nsound.h:152
std::string year
Definition: Wavefile.h:271
std::string album
Definition: Wavefile.h:270
static const uint32 WAVE_
Definition: Wavefile.h:127