Nsound  0.9.4
Static Public Member Functions | Static Public Attributes | Static Protected Member Functions | Static Protected Attributes | Friends | List of all members
Nsound::Wavefile Class Reference

Very simple Wavefile reading class. More...

#include <Nsound/Wavefile.h>

Static Public Member Functions

static std::string decodeFormatTag (const uint16 format_tag)
 
static uint32 getDefaultSampleRate ()
 
static uint32 getDefaultSampleSize ()
 
static void setDefaultSampleRate (const int32 rate)
 
static void setDefaultSampleRate (const float64 &rate)
 
static void setDefaultSampleSize (uint32 size)
 
static void setIEEEFloat (boolean flag)
 
static void setDefaults (const float64 &sample_rate=44100.0, const float64 &sample_bits=16.0, const boolean &use_ieee_floats=false)
 
static boolean read (const std::string &fileName, AudioStream &astream)
 
static boolean readHeader (const std::string &filename, std::string &info)
 Reads the basic header information and sets the info string. More...
 
static boolean write (const std::string &fileName, const AudioStream &as, uint32 bits_per_sample=16)
 
static boolean write (const std::string &fileName, const Buffer &as, uint32 bits_per_sample, uint32 sample_rate)
 

Static Public Attributes

static const uint32 DATA_ = 1635017060
 
static const uint32 FACT_ = 1952670054
 
static const uint32 FMT_ = 544501094
 
static const uint32 PEAK_ = 1262568784
 
static const uint32 RIFF_ = 1179011410
 
static const uint32 WAVE_ = 1163280727
 
static const uint16 WAVE_FORMAT_PCM_ = 0x0001
 
static const uint16 WAVE_FORMAT_IEEE_FLOAT_ = 0x0003
 
static const raw_float64 SIGNED_64_BIT_ = 9223372036854775807.0
 
static const raw_float64 SIGNED_48_BIT_ = 140737488355327.0
 
static const raw_float64 SIGNED_32_BIT_ = 2147483647.0
 
static const raw_float64 SIGNED_24_BIT_ = 8388607.0
 
static const raw_float64 SIGNED_16_BIT_ = 32767.0
 
static const raw_float64 SIGNED_8_BIT_ = 127.0
 
static const raw_uint64 UNSIGNED_64_BIT_ = 18446744073709551615ULL
 
static const raw_uint64 UNSIGNED_48_BIT_ = 281474976710655ULL
 
static const raw_uint64 UNSIGNED_32_BIT_ = 4294967295ULL
 
static const raw_uint64 UNSIGNED_24_BIT_ = 16777215ULL
 
static const raw_uint64 UNSIGNED_16_BIT_ = 65535ULL
 
static const raw_uint64 UNSIGNED_8_BIT_ = 255ULL
 

Static Protected Member Functions

static boolean read (const std::string &filename, std::vector< Buffer * > *b_vector, AudioStream *as, std::stringstream *out)
 

Static Protected Attributes

static uint32 default_sample_rate_ = 44100
 
static uint32 default_sample_size_ = 16
 
static uint16 default_wave_format_ = Wavefile::WAVE_FORMAT_PCM_
 

Friends

Bufferoperator<< (Buffer &lhs, const char *rhs)
 
void operator>> (const Buffer &lhs, const char *rhs)
 
AudioStreamoperator<< (AudioStream &lhs, const char *rhs)
 
void operator>> (const AudioStream &lhs, const char *rhs)
 

Detailed Description

Very simple Wavefile reading class.

Definition at line 116 of file Wavefile.h.

Member Function Documentation

std::string Wavefile::decodeFormatTag ( const uint16  format_tag)
static

Definition at line 1309 of file Wavefile.cc.

1310 {
1311  switch(format_tag)
1312  {
1313  case 0x0000: return "Unknown";
1314  case 0x0001: return "Microsoft PCM";
1315  case 0x0002: return "Microsoft ADPCM";
1316  case 0x0003: return "IEEE Float";
1317  case 0x0004: return "Compaq VSELP";
1318  case 0x0005: return "IBM CVSD";
1319  case 0x0006: return "Microsoft ALAW";
1320  case 0x0007: return "Microsoft MULAW";
1321  case 0x000A: return "Microsoft Windows Media Audio Speech";
1322  case 0x0010: return "OKI ADPCM";
1323  case 0x0011: return "Intel DVI ADPCM";
1324  case 0x0012: return "Videologic MediaSpace ADPCM";
1325  case 0x0013: return "Sierra ADPCM";
1326  case 0x0014: return "Antex Electronics G.723 ADPCM";
1327  case 0x0015: return "DSP Solution DIGISTD";
1328  case 0x0016: return "DSP Solution DIGIFIX";
1329  case 0x0017: return "Dialogic OKI ADPCM";
1330  case 0x0018: return "MediaVision ADPCM";
1331  case 0x0019: return "HP CU";
1332  case 0x0020: return "Yamaha ADPCM";
1333  case 0x0021: return "Speech Compression Sonarc";
1334  case 0x0022: return "DSP Group True Speech";
1335  case 0x0023: return "Echo Speech EchoSC1";
1336  case 0x0024: return "Audiofile AF36";
1337  case 0x0025: return "APTX";
1338  case 0x0026: return "AudioFile AF10";
1339  case 0x0027: return "Prosody 1612";
1340  case 0x0028: return "LRC";
1341  case 0x0030: return "Dolby AC2";
1342  case 0x0031: return "Microsoft GSM610";
1343  case 0x0032: return "Microsoft MSNAudio";
1344  case 0x0033: return "Antex ADPCME";
1345  case 0x0034: return "Control Res VQLPC";
1346  case 0x0035: return "Digireal";
1347  case 0x0036: return "DigiADPCM AC2";
1348  case 0x0037: return "Control Res CR10";
1349  case 0x0038: return "NMS VBXADPCM AC2";
1350  case 0x0039: return "Roland RDAC";
1351  case 0x003A: return "EchoSC3";
1352  case 0x003B: return "Rockwell ADPCM";
1353  case 0x003C: return "Rockwell Digit LK";
1354  case 0x003D: return "Xebec";
1355  case 0x0040: return "Antex Electronics G.721";
1356  case 0x0041: return "Antex Electronics G.728 CELP";
1357  case 0x0042: return "Microsoft MSG723";
1358  case 0x0050: return "MPEG";
1359  case 0x0052: return "Voxware RT24";
1360  case 0x0053: return "InSoft PAC";
1361  case 0x0055: return "MPEG Layer 3";
1362  case 0x0057: return "AMR NB";
1363  case 0x0058: return "AMR WB";
1364  case 0x0059: return "Lucent G.723";
1365  case 0x0060: return "Cirrus";
1366  case 0x0061: return "ESPCM";
1367  case 0x0062: return "Voxware";
1368  case 0x0063: return "Canopus Atrac";
1369  case 0x0064: return "APICOM G.726 ADPCM";
1370  case 0x0065: return "APICOM G.722 ADPCM";
1371  case 0x0066: return "Microsoft DSAT";
1372  case 0x0067: return "Microsoft DSAT Display";
1373  case 0x0069: return "Voxware Byte Aligned";
1374  case 0x0070: return "Voxware AC8";
1375  case 0x0071: return "Voxware AC10";
1376  case 0x0072: return "Voxware AC16";
1377  case 0x0073: return "Voxware AC20";
1378  case 0x0074: return "Voxware Metavoice";
1379  case 0x0075: return "Voxware Metasound";
1380  case 0x0076: return "Voxware RT29HW";
1381  case 0x0077: return "Voxware VR12";
1382  case 0x0078: return "Voxware VR18";
1383  case 0x0079: return "Voxware TQ40";
1384  case 0x0080: return "Softsound";
1385  case 0x0081: return "Voxware TQ60";
1386  case 0x0082: return "MSRT24";
1387  case 0x0083: return "AT&T G.729A";
1388  case 0x0084: return "Motion Pixels MVI MV12";
1389  case 0x0085: return "DF G.726";
1390  case 0x0086: return "DF GSM610";
1391  case 0x0088: return "ISIAudio";
1392  case 0x0089: return "Onlive";
1393  case 0x0091: return "Siemens SBC24";
1394  case 0x0092: return "Dolby AC3 SPDIF";
1395  case 0x0093: return "Mediasonic G723";
1396  case 0x0094: return "Prosody 8KBPS";
1397  case 0x0097: return "ZyXEL ADPCM";
1398  case 0x0098: return "Philips LPCBB";
1399  case 0x0099: return "Packed";
1400  case 0x00A0: return "Malden PhonyTalk";
1401  case 0x00FF: return "AAC";
1402  case 0x0100: return "Rhetorex ADPCM";
1403  case 0x0101: return "IBM MULAW";
1404  case 0x0102: return "IBM ALAW";
1405  case 0x0103: return "IBM ADPCM";
1406  case 0x0111: return "Vivo G.723";
1407  case 0x0112: return "Vivo Siren";
1408  case 0x0123: return "DEC G.723";
1409  case 0x0125: return "Sanyo LD ADPCM";
1410  case 0x0130: return "Siprolab ACEPLNET";
1411  case 0x0131: return "Siprolab ACELP4800";
1412  case 0x0132: return "Siprolab ACELP8V3";
1413  case 0x0133: return "Siprolab G729";
1414  case 0x0134: return "Siprolab G729A";
1415  case 0x0135: return "Siprolab Kelvin";
1416  case 0x0140: return "G726 ADPCM";
1417  case 0x0150: return "Qualcomm Purevoice";
1418  case 0x0151: return "Qualcomm Halfrate";
1419  case 0x0155: return "Tub GSM";
1420  case 0x0160: return "WMAV1";
1421  case 0x0161: return "WMAV2";
1422  case 0x0162: return "WMAV3";
1423  case 0x0163: return "WMAV3 L";
1424  case 0x0200: return "Creative ADPCM";
1425  case 0x0202: return "Creative FastSpeech8";
1426  case 0x0203: return "Creative FastSpeech10";
1427  case 0x0210: return "UHER ADPCM";
1428  case 0x0220: return "Quarterdeck";
1429  case 0x0230: return "iLink VC";
1430  case 0x0240: return "Raw Sport";
1431  case 0x0250: return "IPI HSX";
1432  case 0x0251: return "IPI RPELP";
1433  case 0x0260: return "CS2";
1434  case 0x0270: return "Sony ATRAC3";
1435  case 0x028E: return "Siren";
1436  case 0x0300: return "Fujitsu FM Towns Snd";
1437  case 0x0400: return "BTV Digital";
1438  case 0x0401: return "IMC";
1439  case 0x0450: return "QDesign Music";
1440  case 0x0680: return "AT&T VME VMPCM";
1441  case 0x0681: return "TCP";
1442  case 0x1000: return "Olivetti OLIGSM";
1443  case 0x1001: return "Olivetti OLIADPCM";
1444  case 0x1002: return "Olivetti OLICELP";
1445  case 0x1003: return "Olivetti OLISBC";
1446  case 0x1004: return "Olivetti OLIOPR";
1447  case 0x1100: return "LH Codec";
1448  case 0x1400: return "Norris";
1449  case 0x1401: return "AT&T ISIAudio";
1450  case 0x1500: return "AT&T Soundspace Music Compression";
1451  case 0x2000: return "DVM";
1452  case 0x2001: return "DTS";
1453  case 0x2048: return "Sonic";
1454  case 0x4143: return "AAC AC";
1455  case 0x674f: return "Vorbis 1";
1456  case 0x6750: return "Vorbis 2";
1457  case 0x6751: return "Vorbis 3";
1458  case 0x676f: return "Vorbis 1+";
1459  case 0x6770: return "Vorbis 2+";
1460  case 0x6771: return "Vorbis 3+";
1461  case 0x706d: return "AAC PM";
1462  case 0x7A21: return "GSM AMR CBR";
1463  case 0x7A22: return "GSM AMR VBR";
1464  case 0xF1AC: return "FLAC";
1465  case 0xFFFE: return "WAVE_FORMAT_EXTENSIBLE";
1466  case 0xFFFF: return "Experimental";
1467  }
1468 
1469  return "UNKOWN";
1470 }
static uint32 Nsound::Wavefile::getDefaultSampleRate ( )
inlinestatic

Definition at line 153 of file Wavefile.h.

References default_sample_rate_.

Referenced by Nsound::operator>>().

153 {return default_sample_rate_;};
static uint32 default_sample_rate_
Definition: Wavefile.h:234
static uint32 Nsound::Wavefile::getDefaultSampleSize ( )
inlinestatic

Definition at line 157 of file Wavefile.h.

References default_sample_size_.

Referenced by Nsound::operator>>().

157 {return default_sample_size_;};
static uint32 default_sample_size_
Definition: Wavefile.h:235
void Wavefile::setDefaultSampleRate ( const int32  rate)
static

Definition at line 70 of file Wavefile.cc.

References default_sample_rate_, and M_ASSERT_VALUE.

Referenced by main(), my_main(), setDefaults(), and setDefaultSampleRate().

71 {
72  M_ASSERT_VALUE(rate, >, 0);
73 
74  default_sample_rate_ = rate;
75 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
static uint32 default_sample_rate_
Definition: Wavefile.h:234
static void Nsound::Wavefile::setDefaultSampleRate ( const float64 rate)
inlinestatic

Definition at line 165 of file Wavefile.h.

References setDefaultSampleRate().

166  {setDefaultSampleRate(static_cast<int32>(rate));};
static void setDefaultSampleRate(const int32 rate)
Definition: Wavefile.cc:70
void Wavefile::setDefaultSampleSize ( uint32  size)
static

Definition at line 79 of file Wavefile.cc.

References default_sample_size_, and M_THROW.

Referenced by Buffer_UnitTest(), BufferResample_UnitTest(), DelayLine_UnitTest(), FFTransform_UnitTest(), FilterCombLowPassFeedback_UnitTest(), FilterDelay_UnitTest(), FilterLeastSquaresFIR_UnitTest(), FilterParametricEqualizer_UnitTest(), Generator_UnitTest(), main(), setDefaults(), Sine_UnitTest(), Triangle_UnitTest(), and Wavefile_UnitTest().

80 {
81  if(size == 64 ||
82  size == 48 ||
83  size == 32 ||
84  size == 24 ||
85  size == 16 ||
86  size == 8)
87  {
88  default_sample_size_ = size;
89  }
90  else
91  {
92  M_THROW("setDefaultSampleSize(): Can't use size " << size);
93  }
94 }
static uint32 default_sample_size_
Definition: Wavefile.h:235
#define M_THROW(message)
Definition: Macros.h:108
void Wavefile::setIEEEFloat ( boolean  flag)
static

Definition at line 98 of file Wavefile.cc.

References default_sample_size_, default_wave_format_, WAVE_FORMAT_IEEE_FLOAT_, and WAVE_FORMAT_PCM_.

Referenced by BufferResample_UnitTest(), DelayLine_UnitTest(), FFTransform_UnitTest(), FilterLeastSquaresFIR_UnitTest(), FilterParametricEqualizer_UnitTest(), Generator_UnitTest(), main(), setDefaults(), Sine_UnitTest(), and Triangle_UnitTest().

99 {
100  if(flag)
101  {
103  if(default_sample_size_ > 32)
104  {
106  }
107  else
108  {
110  }
111  }
113 }
static const uint16 WAVE_FORMAT_PCM_
Definition: Wavefile.h:129
static uint16 default_wave_format_
Definition: Wavefile.h:236
static uint32 default_sample_size_
Definition: Wavefile.h:235
static const uint16 WAVE_FORMAT_IEEE_FLOAT_
Definition: Wavefile.h:130
void Wavefile::setDefaults ( const float64 sample_rate = 44100.0,
const float64 sample_bits = 16.0,
const boolean use_ieee_floats = false 
)
static

Definition at line 117 of file Wavefile.cc.

References setDefaultSampleRate(), setDefaultSampleSize(), and setIEEEFloat().

Referenced by main().

121 {
122  setDefaultSampleRate(sample_rate);
123  setDefaultSampleSize(static_cast<uint32>(sample_bits));
124  setIEEEFloat(use_ieee_floats);
125 }
static void setIEEEFloat(boolean flag)
Definition: Wavefile.cc:98
static void setDefaultSampleRate(const int32 rate)
Definition: Wavefile.cc:70
static void setDefaultSampleSize(uint32 size)
Definition: Wavefile.cc:79
boolean Wavefile::read ( const std::string &  fileName,
AudioStream astream 
)
static

Definition at line 920 of file Wavefile.cc.

Referenced by Nsound::operator<<(), and readHeader().

921 {
922  boolean was_successful = read(filename, NULL, &as, NULL);
923 
924  return was_successful;
925 }
static boolean read(const std::string &fileName, AudioStream &astream)
Definition: Wavefile.cc:920
boolean Wavefile::readHeader ( const std::string &  filename,
std::string &  info 
)
static

Reads the basic header information and sets the info string.

Reads the basic header information and sets the info string.

Definition at line 934 of file Wavefile.cc.

References read().

Referenced by main().

937 {
938  std::stringstream ss;
939  boolean success = Wavefile::read(filename, NULL, NULL, &ss);
940  info = ss.str();
941  return success;
942 }
static boolean read(const std::string &fileName, AudioStream &astream)
Definition: Wavefile.cc:920
boolean Wavefile::write ( const std::string &  fileName,
const AudioStream as,
uint32  bits_per_sample = 16 
)
static

Definition at line 952 of file Wavefile.cc.

References DATA_, default_wave_format_, FACT_, FMT_, Nsound::AudioStream::getLength(), Nsound::AudioStream::getNChannels(), Nsound::AudioStream::getSampleRate(), M_THROW, PEAK_, RIFF_, SIGNED_16_BIT_, SIGNED_24_BIT_, SIGNED_32_BIT_, SIGNED_48_BIT_, SIGNED_64_BIT_, SIGNED_8_BIT_, WAVE_, WAVE_FORMAT_IEEE_FLOAT_, WAVE_FORMAT_PCM_, writeFloat32(), writeFloat64(), and writeInt().

Referenced by Nsound::operator>>().

955 {
956  if( bits_per_sample != 8 &&
957  bits_per_sample != 16 &&
958  bits_per_sample != 24 &&
959  bits_per_sample != 32 &&
960  bits_per_sample != 48 &&
961  bits_per_sample != 64)
962  {
963  M_THROW("Wavefile::write(): "
964  << "bits per sample must be 8, 16, 24, 32, 48, 64");
965  return false;
966  }
967 
968  // open the file
969  FILE * output;
970  output = fopen(filename.c_str(), "wb");
971 
972  if(output == NULL)
973  {
974  M_THROW("Wavefile::write(): "
975  << "unable to open file '"
976  << filename);
977  return false;
978  }
979 
980  // verify channels_
981  uint32 channels = as.getNChannels();
982 
983  uint32 n_samples = as.getLength();
984 
985  // The data length is the total number of bytes of the raw sound
986  // data. There are 8 bits per byte.
987  uint32 data_length = n_samples * channels * (bits_per_sample / 8);
988 
989  // riff_chunk_lenth is always 36 bytes larger than the data length,
990  // the 36 bytes is added to include all the header information for
991  // the riff wavefile. Take a look at Wavefile.hxx to see a block
992  // diagram of the header format.
993  uint32 riff_chunk_length = data_length + 36;
994 
995  // calculate block_alignemnt
996  uint32 block_alignment = channels * (bits_per_sample / 8);
997 
998  // calculate bytes_per_second
999  uint32 sample_rate = static_cast<uint32>(as.getSampleRate());
1000  uint32 bytes_per_second = sample_rate * block_alignment;
1001 
1003 
1005  // Write out the data
1006 
1007  writeInt(output,4,Wavefile::RIFF_);
1008  writeInt(output,4,riff_chunk_length);
1009  writeInt(output,4,Wavefile::WAVE_);
1010  writeInt(output,4,Wavefile::FMT_);
1011  writeInt(output,4,16); // format_chunk_length = 16
1012  writeInt(output,2,format_tag);
1013  writeInt(output,2,channels);
1014  writeInt(output,4,sample_rate);
1015  writeInt(output,4,bytes_per_second);
1016  writeInt(output,2,block_alignment);
1017  writeInt(output,2,bits_per_sample);
1018 
1019  if(format_tag == WAVE_FORMAT_IEEE_FLOAT_)
1020  {
1021  writeInt(output,4,Wavefile::FACT_);
1022  writeInt(output,4, 4);
1023  writeInt(output,4, n_samples);
1024 
1025  writeInt(output,4,Wavefile::PEAK_);
1026  writeInt(output,4, 16);
1027  writeInt(output,4, 0);
1028  writeInt(output,4, 0);
1029  writeInt(output,4, 0);
1030  writeInt(output,4, 0);
1031  }
1032 
1033  writeInt(output,4,Wavefile::DATA_);
1034  writeInt(output,4,data_length);
1035 
1037  // set data sizes
1038  uint32 n_bytes = 0;
1039  float64 data_scale = static_cast<float64>(0.0);
1040  switch(bits_per_sample)
1041  {
1042  case 64:
1043  {
1044  n_bytes = 8;
1045  data_scale = Wavefile::SIGNED_64_BIT_;
1046  break;
1047  }
1048  case 48:
1049  {
1050  n_bytes = 6;
1051  data_scale = Wavefile::SIGNED_48_BIT_;
1052  break;
1053  }
1054  case 32:
1055  {
1056  n_bytes = 4;
1057  data_scale = Wavefile::SIGNED_32_BIT_;
1058  break;
1059  }
1060  case 24:
1061  {
1062  n_bytes = 3;
1063  data_scale = Wavefile::SIGNED_24_BIT_;
1064  break;
1065  }
1066  case 16:
1067  {
1068  n_bytes = 2;
1069  data_scale = Wavefile::SIGNED_16_BIT_;
1070  break;
1071  }
1072  case 8:
1073  {
1074  n_bytes = 1;
1075  data_scale = Wavefile::SIGNED_8_BIT_;
1076  break;
1077  }
1078  }
1079 
1080  // write the data
1081  int64 scaled = 0;
1082  int64 positive_data_scale = static_cast<int64>(data_scale);
1083  int64 negitive_data_scale = static_cast<int64>(-1.0 * data_scale);
1084 
1085  for(uint32 i = 0; i < n_samples; i++)
1086  {
1087  for(uint32 ch = 0; ch < channels; ++ch)
1088  {
1089  if(format_tag == WAVE_FORMAT_PCM_)
1090  {
1091  scaled = static_cast<int64>(as[ch][i] * data_scale);
1092 
1093  if(scaled > positive_data_scale)
1094  {
1095  scaled = positive_data_scale;
1096  }
1097  else if(scaled < negitive_data_scale)
1098  {
1099  scaled = negitive_data_scale;
1100  }
1101  writeInt(output,n_bytes,scaled);
1102  }
1103  else if(format_tag == WAVE_FORMAT_IEEE_FLOAT_)
1104  {
1105  if(bits_per_sample == 32)
1106  {
1107  writeFloat32(output, static_cast<float32>(as[ch][i]));
1108  }
1109  else if(bits_per_sample == 64)
1110  {
1111  writeFloat64(output, as[ch][i]);
1112  }
1113  else
1114  {
1115  M_THROW(
1116  "Wavefile::read(\""
1117  << filename
1118  << "\"): format is "
1119  << "IEEE Float but bits_per_sample = "
1120  << bits_per_sample);
1121  }
1122  }
1123  }
1124  }
1125 
1126  fclose(output);
1127  return true;
1128 }
unsigned int uint32
Definition: Nsound.h:153
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
static void writeFloat64(FILE *output, float64 value)
Definition: Wavefile.cc:382
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
uint32 getLength() const
Returns the number of samples of audio data in the stream.
Definition: AudioStream.cc:197
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 uint16 default_wave_format_
Definition: Wavefile.h:236
static const raw_float64 SIGNED_32_BIT_
Definition: Wavefile.h:135
static const uint32 PEAK_
Definition: Wavefile.h:125
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
static const uint32 FACT_
Definition: Wavefile.h:123
static void writeInt(FILE *output, uint32 n_bytes, int64 value)
Definition: Wavefile.cc:346
static void writeFloat32(FILE *output, float32 value)
Definition: Wavefile.cc:364
#define M_THROW(message)
Definition: Macros.h:108
static const raw_float64 SIGNED_8_BIT_
Definition: Wavefile.h:138
static const raw_float64 SIGNED_64_BIT_
Definition: Wavefile.h:133
static const uint32 DATA_
Definition: Wavefile.h:122
static const uint16 WAVE_FORMAT_IEEE_FLOAT_
Definition: Wavefile.h:130
signed long long int64
Definition: Nsound.h:143
static const uint32 FMT_
Definition: Wavefile.h:124
static const uint32 WAVE_
Definition: Wavefile.h:127
boolean Wavefile::write ( const std::string &  fileName,
const Buffer as,
uint32  bits_per_sample,
uint32  sample_rate 
)
static

Definition at line 1132 of file Wavefile.cc.

References DATA_, default_wave_format_, FACT_, FMT_, Nsound::Buffer::getLength(), M_THROW, PEAK_, RIFF_, SIGNED_16_BIT_, SIGNED_24_BIT_, SIGNED_32_BIT_, SIGNED_48_BIT_, SIGNED_64_BIT_, SIGNED_8_BIT_, WAVE_, WAVE_FORMAT_IEEE_FLOAT_, WAVE_FORMAT_PCM_, writeFloat32(), writeFloat64(), and writeInt().

1137 {
1138  if( bits_per_sample != 8 &&
1139  bits_per_sample != 16 &&
1140  bits_per_sample != 24 &&
1141  bits_per_sample != 32 &&
1142  bits_per_sample != 48 &&
1143  bits_per_sample != 64)
1144  {
1145  M_THROW("Wavefile::write(): "
1146  << "bits per sample must be 8, 16, 24, 32, 64");
1147  return false;
1148  }
1149 
1150  // open the file
1151 
1152  FILE * output;
1153  output = fopen(filename.c_str(), "wb");
1154  if(output == NULL)
1155  {
1156  M_THROW("Wavefile::write(): "
1157  << "unable to open file '"
1158  << filename);
1159  return false;
1160  }
1161 
1162  // verify channels_
1163  uint32 channels = 1;
1164 
1165  uint32 n_samples = buffer.getLength();
1166 
1167  // The data length is the total number of bytes of the raw sound
1168  // data. There are 8 bits per byte.
1169  uint32 data_length = n_samples * channels * (bits_per_sample / 8);
1170 
1171  // riff_chunk_lenth is always 36 bytes larger than the data length,
1172  // the 36 bytes is added to include all the header information for
1173  // the riff wavefile. Take a look at Wavefile.hxx to see a block
1174  // diagram of the header format.
1175  uint32 riff_chunk_length = data_length + 36;
1176 
1177  // calculate block_alignemnt
1178  uint32 block_alignment = channels * (bits_per_sample / 8);
1179 
1180  // calculate bytes_per_second
1181  uint32 bytes_per_second = sample_rate * block_alignment;
1182 
1184 
1186  // Write out the data
1187 
1188  writeInt(output,4,Wavefile::RIFF_);
1189  writeInt(output,4,riff_chunk_length);
1190  writeInt(output,4,Wavefile::WAVE_);
1191  writeInt(output,4,Wavefile::FMT_);
1192  writeInt(output,4,16); // format_chunk_length = 16
1193  writeInt(output,2,format_tag);
1194  writeInt(output,2,channels);
1195  writeInt(output,4,sample_rate);
1196  writeInt(output,4,bytes_per_second);
1197  writeInt(output,2,block_alignment);
1198  writeInt(output,2,bits_per_sample);
1199 
1200  if(format_tag == WAVE_FORMAT_IEEE_FLOAT_)
1201  {
1202  writeInt(output,4,Wavefile::FACT_);
1203  writeInt(output,4, 4);
1204  writeInt(output,4, n_samples);
1205 
1206  writeInt(output,4,Wavefile::PEAK_);
1207  writeInt(output,4, 16);
1208  writeInt(output,4, 0);
1209  writeInt(output,4, 0);
1210  writeInt(output,4, 0);
1211  writeInt(output,4, 0);
1212  }
1213 
1214  writeInt(output,4,Wavefile::DATA_);
1215  writeInt(output,4,data_length);
1216 
1218  // set data sizes
1219  uint32 n_bytes = 0;
1220  float64 data_scale = static_cast<float64>(0.0);
1221  switch(bits_per_sample)
1222  {
1223  case 64:
1224  {
1225  n_bytes = 8;
1226  data_scale = Wavefile::SIGNED_64_BIT_;
1227  break;
1228  }
1229  case 48:
1230  {
1231  n_bytes = 6;
1232  data_scale = Wavefile::SIGNED_48_BIT_;
1233  break;
1234  }
1235  case 32:
1236  {
1237  n_bytes = 4;
1238  data_scale = Wavefile::SIGNED_32_BIT_;
1239  break;
1240  }
1241  case 24:
1242  {
1243  n_bytes = 3;
1244  data_scale = Wavefile::SIGNED_24_BIT_;
1245  break;
1246  }
1247  case 16:
1248  {
1249  n_bytes = 2;
1250  data_scale = Wavefile::SIGNED_16_BIT_;
1251  break;
1252  }
1253  case 8:
1254  {
1255  n_bytes = 1;
1256  data_scale = Wavefile::SIGNED_8_BIT_;
1257  break;
1258  }
1259  }
1260 
1262  // write the data
1263  int64 scaled;
1264  int64 positive_data_scale = static_cast<int64>(data_scale);
1265  int64 negitive_data_scale = static_cast<int64>(-1.0 * data_scale);
1266 
1267  for(uint32 i = 0; i < n_samples; ++i)
1268  {
1269  if(format_tag == WAVE_FORMAT_PCM_)
1270  {
1271  scaled = static_cast<int64>(buffer[i] * data_scale);
1272 
1273  if(scaled > positive_data_scale)
1274  {
1275  scaled = positive_data_scale;
1276  }
1277  else if(scaled < negitive_data_scale)
1278  {
1279  scaled = negitive_data_scale;
1280  }
1281  writeInt(output,n_bytes,scaled);
1282  }
1283  else if(format_tag == WAVE_FORMAT_IEEE_FLOAT_)
1284  {
1285  if(bits_per_sample == 32)
1286  {
1287  writeFloat32(output, static_cast<float32>(buffer[i]));
1288  }
1289  else if(bits_per_sample == 64)
1290  {
1291  writeFloat64(output, buffer[i]);
1292  }
1293  else
1294  {
1295  M_THROW("Wavefile::read(): format is "
1296  << "IEEE Float but bits_per_sample = "
1297  << bits_per_sample);
1298  }
1299  }
1300  }
1301 
1302  fclose(output);
1303  return true;
1304 }
unsigned int uint32
Definition: Nsound.h:153
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
static void writeFloat64(FILE *output, float64 value)
Definition: Wavefile.cc:382
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 uint16 default_wave_format_
Definition: Wavefile.h:236
static const raw_float64 SIGNED_32_BIT_
Definition: Wavefile.h:135
static const uint32 PEAK_
Definition: Wavefile.h:125
static const uint32 FACT_
Definition: Wavefile.h:123
static void writeInt(FILE *output, uint32 n_bytes, int64 value)
Definition: Wavefile.cc:346
static void writeFloat32(FILE *output, float32 value)
Definition: Wavefile.cc:364
#define M_THROW(message)
Definition: Macros.h:108
static const raw_float64 SIGNED_8_BIT_
Definition: Wavefile.h:138
static const raw_float64 SIGNED_64_BIT_
Definition: Wavefile.h:133
static const uint32 DATA_
Definition: Wavefile.h:122
static const uint16 WAVE_FORMAT_IEEE_FLOAT_
Definition: Wavefile.h:130
signed long long int64
Definition: Nsound.h:143
static const uint32 FMT_
Definition: Wavefile.h:124
static const uint32 WAVE_
Definition: Wavefile.h:127
static boolean Nsound::Wavefile::read ( const std::string &  filename,
std::vector< Buffer * > *  b_vector,
AudioStream as,
std::stringstream *  out 
)
staticprotected

Friends And Related Function Documentation

Buffer& operator<< ( Buffer lhs,
const char *  rhs 
)
friend
void operator>> ( const Buffer lhs,
const char *  rhs 
)
friend
AudioStream& operator<< ( AudioStream lhs,
const char *  rhs 
)
friend
void operator>> ( const AudioStream lhs,
const char *  rhs 
)
friend

Member Data Documentation

const uint32 Nsound::Wavefile::DATA_ = 1635017060
static

Definition at line 122 of file Wavefile.h.

Referenced by findID3v1Tag(), and write().

const uint32 Nsound::Wavefile::FACT_ = 1952670054
static

Definition at line 123 of file Wavefile.h.

Referenced by write().

const uint32 Nsound::Wavefile::FMT_ = 544501094
static

Definition at line 124 of file Wavefile.h.

Referenced by write().

const uint32 Nsound::Wavefile::PEAK_ = 1262568784
static

Definition at line 125 of file Wavefile.h.

Referenced by write().

const uint32 Nsound::Wavefile::RIFF_ = 1179011410
static

Definition at line 126 of file Wavefile.h.

Referenced by findID3v1Tag(), and write().

const uint32 Nsound::Wavefile::WAVE_ = 1163280727
static

Definition at line 127 of file Wavefile.h.

Referenced by findID3v1Tag(), and write().

const uint16 Nsound::Wavefile::WAVE_FORMAT_PCM_ = 0x0001
static

Definition at line 129 of file Wavefile.h.

Referenced by setIEEEFloat(), and write().

const uint16 Nsound::Wavefile::WAVE_FORMAT_IEEE_FLOAT_ = 0x0003
static

Definition at line 130 of file Wavefile.h.

Referenced by setIEEEFloat(), and write().

const raw_float64 Wavefile::SIGNED_64_BIT_ = 9223372036854775807.0
static

Definition at line 133 of file Wavefile.h.

Referenced by write().

const raw_float64 Wavefile::SIGNED_48_BIT_ = 140737488355327.0
static

Definition at line 134 of file Wavefile.h.

Referenced by write().

const raw_float64 Wavefile::SIGNED_32_BIT_ = 2147483647.0
static

Definition at line 135 of file Wavefile.h.

Referenced by write().

const raw_float64 Wavefile::SIGNED_24_BIT_ = 8388607.0
static

Definition at line 136 of file Wavefile.h.

Referenced by write().

const raw_float64 Wavefile::SIGNED_16_BIT_ = 32767.0
static

Definition at line 137 of file Wavefile.h.

Referenced by write().

const raw_float64 Wavefile::SIGNED_8_BIT_ = 127.0
static

Definition at line 138 of file Wavefile.h.

Referenced by write().

const raw_uint64 Nsound::Wavefile::UNSIGNED_64_BIT_ = 18446744073709551615ULL
static

Definition at line 140 of file Wavefile.h.

const raw_uint64 Nsound::Wavefile::UNSIGNED_48_BIT_ = 281474976710655ULL
static

Definition at line 141 of file Wavefile.h.

const raw_uint64 Nsound::Wavefile::UNSIGNED_32_BIT_ = 4294967295ULL
static

Definition at line 142 of file Wavefile.h.

const raw_uint64 Nsound::Wavefile::UNSIGNED_24_BIT_ = 16777215ULL
static

Definition at line 143 of file Wavefile.h.

const raw_uint64 Nsound::Wavefile::UNSIGNED_16_BIT_ = 65535ULL
static

Definition at line 144 of file Wavefile.h.

const raw_uint64 Nsound::Wavefile::UNSIGNED_8_BIT_ = 255ULL
static

Definition at line 145 of file Wavefile.h.

uint32 Wavefile::default_sample_rate_ = 44100
staticprotected

Definition at line 234 of file Wavefile.h.

Referenced by getDefaultSampleRate(), and setDefaultSampleRate().

uint32 Wavefile::default_sample_size_ = 16
staticprotected

Definition at line 235 of file Wavefile.h.

Referenced by getDefaultSampleSize(), setDefaultSampleSize(), and setIEEEFloat().

uint16 Wavefile::default_wave_format_ = Wavefile::WAVE_FORMAT_PCM_
staticprotected

Definition at line 236 of file Wavefile.h.

Referenced by setIEEEFloat(), and write().


The documentation for this class was generated from the following files: