Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <cstdint>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9.  
  10. using std::cin;
  11. using std::cout;
  12. using std::endl;
  13. using std::fstream;
  14. using std::string;
  15. using std::ofstream;
  16.  
  17. typedef struct  WAV_HEADER
  18. {
  19.     /* RIFF Chunk Descriptor */
  20.     uint8_t         RIFF[4];        // RIFF Header Magic header
  21.     uint32_t        ChunkSize;      // RIFF Chunk Size
  22.     uint8_t         WAVE[4];        // WAVE Header
  23.                                     /* "fmt" sub-chunk */
  24.     uint8_t         fmt[4];         // FMT header
  25.     uint32_t        Subchunk1Size;  // Size of the fmt chunk
  26.     uint16_t        AudioFormat;    // Audio format 1=PCM,6=mulaw,7=alaw,     257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM
  27.     uint16_t        NumOfChan;      // Number of channels 1=Mono 2=Sterio
  28.     uint32_t        SamplesPerSec;  // Sampling Frequency in Hz
  29.     uint32_t        bytesPerSec;    // bytes per second
  30.     uint16_t        blockAlign;     // 2=16-bit mono, 4=16-bit stereo
  31.     uint16_t        bitsPerSample;  // Number of bits per sample
  32.                                     /* "data" sub-chunk */
  33.     uint8_t         Subchunk2ID[4]; // "data"  string
  34.     uint32_t        Subchunk2Size;  // Sampled data length
  35. } wav_hdr;
  36.  
  37. // Function prototypes
  38. int getFileSize(FILE* inFile);
  39.  
  40. int main(int argc, char* argv[])
  41. {
  42.     ofstream outfile("stereo.wav");
  43.     wav_hdr wavHeader;
  44.     int headerSize = sizeof(wav_hdr), filelength = 0;
  45.  
  46.  
  47.     const char* filePath;
  48.     filePath = "C:\\Users\\Kudłaty\\Music\\test2.wav";
  49.  
  50.  
  51.     FILE* wavFile = fopen(filePath, "rb");
  52.     if (wavFile == nullptr)
  53.     {
  54.         fprintf(stderr, "Unable to open wave file: %s\n", filePath);
  55.         return 1;
  56.     }
  57.  
  58.     //Read the header
  59.     size_t bytesRead = fread(&wavHeader, 1, headerSize, wavFile);
  60.     cout << "Header Read " << bytesRead << " bytes." << endl;
  61.     if (bytesRead > 0)
  62.     {
  63.         //Read the data
  64.         uint16_t bytesPerSample = wavHeader.bitsPerSample / 8;      //Number     of bytes per sample
  65.         uint64_t numSamples = wavHeader.ChunkSize / bytesPerSample; //How many samples are in the wav file?
  66.         static const uint16_t BUFFER_SIZE = 4096;
  67.         int8_t* buffer = new int8_t[BUFFER_SIZE];
  68.         while ((bytesRead = fread(buffer, sizeof buffer[0], BUFFER_SIZE / (sizeof buffer[0]), wavFile)) > 0)
  69.         {
  70.             for(int i = 0; BUFFER_SIZE; i++)
  71.             {
  72.             cout << std::hex << (int)buffer[i] << " ";
  73.             }
  74.             //cout <<  buffer << endl;
  75.             /** DO SOMETHING WITH THE WAVE DATA HERE **/
  76.             cout << "Read " << bytesRead << " bytes." << endl;
  77.         }
  78.         delete[] buffer;
  79.         buffer = nullptr;
  80.         filelength = getFileSize(wavFile);
  81.  
  82.         cout << "File is                    :" << filelength << " bytes." << endl;
  83.         cout << "RIFF header                :" << wavHeader.RIFF[0] << wavHeader.RIFF[1] << wavHeader.RIFF[2] << wavHeader.RIFF[3] << endl;
  84.         cout << "WAVE header                :" << wavHeader.WAVE[0] << wavHeader.WAVE[1] << wavHeader.WAVE[2] << wavHeader.WAVE[3] << endl;
  85.         cout << "FMT                        :" << wavHeader.fmt[0] << wavHeader.fmt[1] << wavHeader.fmt[2] << wavHeader.fmt[3] << endl;
  86.         cout << "Data size                  :" << wavHeader.ChunkSize << endl;
  87.  
  88.         // Display the sampling Rate from the header
  89.         cout << "Sampling Rate              :" << wavHeader.SamplesPerSec << endl;
  90.         cout << "Number of bits used        :" << wavHeader.bitsPerSample << endl;
  91.         cout << "Number of channels         :" << wavHeader.NumOfChan << endl;
  92.         cout << "Number of bytes per second :" << wavHeader.bytesPerSec << endl;
  93.         cout << "Data length                :" << wavHeader.Subchunk2Size << endl;
  94.         cout << "Audio Format               :" << wavHeader.AudioFormat << endl;
  95.         // Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM
  96.  
  97.         cout << "Block align                :" << wavHeader.blockAlign << endl;
  98.         cout << "Data string                :" << wavHeader.Subchunk2ID[0] << wavHeader.Subchunk2ID[1] << wavHeader.Subchunk2ID[2] << wavHeader.Subchunk2ID[3] << endl;
  99.     }
  100.  
  101.     //fwrite();
  102.     fclose(wavFile);
  103.     _getch();
  104.     return 0;
  105. }
  106.  
  107. // find the file size
  108. int getFileSize(FILE* inFile)
  109. {
  110.     int fileSize = 0;
  111.     fseek(inFile, 0, SEEK_END);
  112.  
  113.     fileSize = ftell(inFile);
  114.  
  115.     fseek(inFile, 0, SEEK_SET);
  116.     return fileSize;
  117. }
  118.  
  119. void convert(FILE* inFile, FILE* outFile)
  120. {
  121.     wav_hdr wavHeader1;
  122.     wav_hdr wavHeader2;
  123.     int headerSize = sizeof(wav_hdr), filelength = 0;
  124.     size_t bytesRead = fread(&wavHeader1, 1, headerSize, inFile);
  125.  
  126. }
  127.  
  128. void interleave(const uint16_t * in_L,     // mono input buffer (left channel)
  129.                 const uint16_t * in_R,     // mono input buffer (right channel)
  130.                 uint16_t * out,            // stereo output buffer
  131.                 const size_t num_samples)  // number of samples
  132. {
  133.     for (size_t i = 0; i < num_samples; ++i)
  134.     {
  135.         out[i * 2] = in_L[i];
  136.         out[i * 2 + 1] = in_R[i];
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement