Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_DEPRECATE
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <cstdint>
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- using std::cin;
- using std::cout;
- using std::endl;
- using std::fstream;
- using std::string;
- using std::ofstream;
- typedef struct WAV_HEADER
- {
- /* RIFF Chunk Descriptor */
- uint8_t RIFF[4]; // RIFF Header Magic header
- uint32_t ChunkSize; // RIFF Chunk Size
- uint8_t WAVE[4]; // WAVE Header
- /* "fmt" sub-chunk */
- uint8_t fmt[4]; // FMT header
- uint32_t Subchunk1Size; // Size of the fmt chunk
- uint16_t AudioFormat; // Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM
- uint16_t NumOfChan; // Number of channels 1=Mono 2=Sterio
- uint32_t SamplesPerSec; // Sampling Frequency in Hz
- uint32_t bytesPerSec; // bytes per second
- uint16_t blockAlign; // 2=16-bit mono, 4=16-bit stereo
- uint16_t bitsPerSample; // Number of bits per sample
- /* "data" sub-chunk */
- uint8_t Subchunk2ID[4]; // "data" string
- uint32_t Subchunk2Size; // Sampled data length
- } wav_hdr;
- // Function prototypes
- int getFileSize(FILE* inFile);
- int main(int argc, char* argv[])
- {
- ofstream outfile("stereo.wav");
- wav_hdr wavHeader;
- int headerSize = sizeof(wav_hdr), filelength = 0;
- const char* filePath;
- filePath = "C:\\Users\\Kudłaty\\Music\\test2.wav";
- FILE* wavFile = fopen(filePath, "rb");
- if (wavFile == nullptr)
- {
- fprintf(stderr, "Unable to open wave file: %s\n", filePath);
- return 1;
- }
- //Read the header
- size_t bytesRead = fread(&wavHeader, 1, headerSize, wavFile);
- cout << "Header Read " << bytesRead << " bytes." << endl;
- if (bytesRead > 0)
- {
- //Read the data
- uint16_t bytesPerSample = wavHeader.bitsPerSample / 8; //Number of bytes per sample
- uint64_t numSamples = wavHeader.ChunkSize / bytesPerSample; //How many samples are in the wav file?
- static const uint16_t BUFFER_SIZE = 4096;
- int8_t* buffer = new int8_t[BUFFER_SIZE];
- while ((bytesRead = fread(buffer, sizeof buffer[0], BUFFER_SIZE / (sizeof buffer[0]), wavFile)) > 0)
- {
- for(int i = 0; BUFFER_SIZE; i++)
- {
- cout << std::hex << (int)buffer[i] << " ";
- }
- //cout << buffer << endl;
- /** DO SOMETHING WITH THE WAVE DATA HERE **/
- cout << "Read " << bytesRead << " bytes." << endl;
- }
- delete[] buffer;
- buffer = nullptr;
- filelength = getFileSize(wavFile);
- cout << "File is :" << filelength << " bytes." << endl;
- cout << "RIFF header :" << wavHeader.RIFF[0] << wavHeader.RIFF[1] << wavHeader.RIFF[2] << wavHeader.RIFF[3] << endl;
- cout << "WAVE header :" << wavHeader.WAVE[0] << wavHeader.WAVE[1] << wavHeader.WAVE[2] << wavHeader.WAVE[3] << endl;
- cout << "FMT :" << wavHeader.fmt[0] << wavHeader.fmt[1] << wavHeader.fmt[2] << wavHeader.fmt[3] << endl;
- cout << "Data size :" << wavHeader.ChunkSize << endl;
- // Display the sampling Rate from the header
- cout << "Sampling Rate :" << wavHeader.SamplesPerSec << endl;
- cout << "Number of bits used :" << wavHeader.bitsPerSample << endl;
- cout << "Number of channels :" << wavHeader.NumOfChan << endl;
- cout << "Number of bytes per second :" << wavHeader.bytesPerSec << endl;
- cout << "Data length :" << wavHeader.Subchunk2Size << endl;
- cout << "Audio Format :" << wavHeader.AudioFormat << endl;
- // Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM
- cout << "Block align :" << wavHeader.blockAlign << endl;
- cout << "Data string :" << wavHeader.Subchunk2ID[0] << wavHeader.Subchunk2ID[1] << wavHeader.Subchunk2ID[2] << wavHeader.Subchunk2ID[3] << endl;
- }
- //fwrite();
- fclose(wavFile);
- _getch();
- return 0;
- }
- // find the file size
- int getFileSize(FILE* inFile)
- {
- int fileSize = 0;
- fseek(inFile, 0, SEEK_END);
- fileSize = ftell(inFile);
- fseek(inFile, 0, SEEK_SET);
- return fileSize;
- }
- void convert(FILE* inFile, FILE* outFile)
- {
- wav_hdr wavHeader1;
- wav_hdr wavHeader2;
- int headerSize = sizeof(wav_hdr), filelength = 0;
- size_t bytesRead = fread(&wavHeader1, 1, headerSize, inFile);
- }
- void interleave(const uint16_t * in_L, // mono input buffer (left channel)
- const uint16_t * in_R, // mono input buffer (right channel)
- uint16_t * out, // stereo output buffer
- const size_t num_samples) // number of samples
- {
- for (size_t i = 0; i < num_samples; ++i)
- {
- out[i * 2] = in_L[i];
- out[i * 2 + 1] = in_R[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement