SHARE
TWEET

Untitled

a guest Jan 17th, 2019 183 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #pragma once
  3. #pragma pack(1)
  4.  
  5. struct WAV_Header {
  6.     u32 ChunkID;       // "RIFF" (LE: 0x46464952)
  7.     u32 ChunkSize;     // (FileSize) - 8
  8.     u32 Format;        // "WAVE" (LE: 0x45564157)
  9.  
  10.     u32 Subchunk1ID;   // "fmt " (LE: 0x20746d66)
  11.     u32 SubchunkSize;  // 16 for PCM
  12.     u16 AudioFormat;   // 1 for Linear PCM
  13.     u16 NumChannels;   // Mono = 1, Stereo = 2, etc.
  14.     u32 SampleRate;    // 8000, 44100, etc.
  15.     u32 ByteRate;      // SampleRate * NumChannels * BitsPerSample/8
  16.     u16 BlockAlign;    // NumChannels * BitsPerSample/8
  17.     u16 BitsPerSample; // 8 bits = 8, 16 bits = 16, etc.
  18.  
  19.     u32 Subchunk2ID;   // "data" (LE: 0x61746164)
  20.     u32 Subchunk2Size; // NumSamples * NumChannels * BitsPerSample/8
  21.     // u16* Data;      // The actual sound data.
  22. };
  23.  
  24. #pragma pack()
  25.  
  26. template<typename Sample_Type>
  27. struct WAV_File {
  28.     WAV_Header header;
  29.     vector<Sample_Type> data;
  30.    
  31.     WAV_File(u32 sample_rate, u16 num_chans, u16 sample_bits = sizeof(Sample_Type)*8) {
  32.         header.ChunkID       = 0x46464952;
  33.         header.ChunkSize     = 0; // (FileSize) - 8
  34.         header.Format        = 0x45564157;
  35.  
  36.         header.Subchunk1ID   = 0x20746d66;
  37.         header.SubchunkSize  = 16;
  38.         header.AudioFormat   = 1;
  39.         header.NumChannels   = num_chans;
  40.         header.SampleRate    = sample_rate;
  41.         header.ByteRate      = sample_bits / 8 * num_chans * sample_rate;
  42.         header.BlockAlign    = sample_bits / 8 * num_chans;
  43.         header.BitsPerSample = sample_bits;
  44.  
  45.         header.Subchunk2ID   = 0x61746164;
  46.         header.Subchunk2Size = 0; // NumSamples * NumChannels * BitsPerSample/8
  47.     }
  48.  
  49.     void Reset() {
  50.         data.clear();
  51.     }
  52.  
  53.     void Write(Sample_Type val) {
  54.         data.push_back(val);
  55.     }
  56.  
  57.     void Save(string dir, string filename) {
  58.         FILE* f = fopen((dir + "\\" + filename).c_str(), "wb");
  59.         if  (!f) return;
  60.  
  61.         header.ChunkSize     = (sizeof(Sample_Type) * data.size()) + sizeof(header) - 8; // (FileSize) - 8
  62.         header.Subchunk2Size = (sizeof(Sample_Type) * data.size()); // NumSamples * NumChannels * BitsPerSample/8
  63.  
  64.         fwrite(&header, 1, sizeof(header), f);
  65.         fwrite(&data[0], sizeof(Sample_Type), data.size(), f);
  66.         fclose(f);
  67.     }
  68. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top