can somebody help me with this code??? ,, im reading a wave file written in FLStudio where fmt is not there , they got "JUNK" instead of fmt so i am literally jogging the file around so because i am a noob and i need to convert data to 32 bits im having these questions like what im doing wrong? if im finding fmt at 48 byte riff is good file size is good then junk. here is my cpp code "
#include <iostream>
#include <fstream>
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;
int main() {
static_assert(sizeof(wav_hdr) == 44, "");
std::string in_name = "./assets/audio/mono_boom_16.wav"; // raw pcm data without wave header
std::fstream in(in_name, std::ios::in | std::ios::binary);
wav_hdr hdr;
in.seekg(0,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.RIFF), sizeof(hdr.RIFF));
for (int i = 0; i < sizeof(hdr.RIFF); ++i) {
// TODO: read/write in blocks
std::cout<<hdr.RIFF;
}
std::cout<<' '<<std::endl;
in.seekg(4,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.ChunkSize), sizeof(hdr.ChunkSize));
std::cout<<hdr.ChunkSize<<std::endl;
in.seekg(8,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.WAVE), sizeof(hdr.WAVE));
for (int i = 0; i < sizeof(hdr.WAVE); ++i) {
// TODO: read/write in blocks
std::cout<<hdr.WAVE;
}
std::cout<<' '<<std::endl;
in.seekg(48,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.fmt), sizeof(hdr.fmt));
for (int i = 0; i < sizeof(hdr.fmt); ++i) {
// TODO: read/write in blocks
std::cout<<hdr.fmt;
}
std::cout<<' '<<std::endl;
in.seekg(52,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.Subchunk1Size), sizeof(hdr.Subchunk1Size));
std::cout<<hdr.Subchunk1Size<<std::endl;
in.seekg(56,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.AudioFormat), sizeof(hdr.AudioFormat));
std::cout<<hdr.AudioFormat<<std::endl;
in.seekg(58,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.NumOfChan), sizeof(hdr.NumOfChan));
std::cout<<hdr.NumOfChan<<std::endl;
in.seekg(60,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.SamplesPerSec), sizeof(hdr.SamplesPerSec));
std::cout<<hdr.SamplesPerSec<<std::endl;
in.seekg(64,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.bytesPerSec), sizeof(hdr.bytesPerSec));
std::cout<<hdr.bytesPerSec<<std::endl;
in.seekg(68,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.blockAlign), sizeof(hdr.blockAlign));
std::cout<<hdr.blockAlign<<std::endl;
in.seekg(70,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.bitsPerSample), sizeof(hdr.bitsPerSample));
std::cout<<hdr.bitsPerSample<<std::endl;
in.seekg(72,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.Subchunk2ID), sizeof(hdr.Subchunk2ID));
for (int i = 0; i < sizeof(hdr.Subchunk2ID); ++i) {
// TODO: read/write in blocks
std::cout<<hdr.Subchunk2ID;
}
std::cout<<' '<<std::endl;
in.seekg(76,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.Subchunk2Size), sizeof(hdr.Subchunk2Size));
std::cout<<hdr.Subchunk2Size<<std::endl;
uint16_t * dat = new uint16_t[hdr.Subchunk2Size];
uint16_t dit;
float dumpi ;
float * dump = new float[hdr.Subchunk2Size] ;
for(int i = 78; i < hdr.Subchunk2Size; i++){
in.seekg(i,std::ios::beg);
in.read(reinterpret_cast<char *>(&dit), sizeof(dit));
dumpi = dit;
dump = 65535.f / dumpi;
std::cout<< dump<<' ';
i++;
}
std::cout<<' '<<std::endl;
return 0;
}
"
#include <iostream>
#include <fstream>
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;
int main() {
static_assert(sizeof(wav_hdr) == 44, "");
std::string in_name = "./assets/audio/mono_boom_16.wav"; // raw pcm data without wave header
std::fstream in(in_name, std::ios::in | std::ios::binary);
wav_hdr hdr;
in.seekg(0,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.RIFF), sizeof(hdr.RIFF));
for (int i = 0; i < sizeof(hdr.RIFF); ++i) {
// TODO: read/write in blocks
std::cout<<hdr.RIFF;
}
std::cout<<' '<<std::endl;
in.seekg(4,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.ChunkSize), sizeof(hdr.ChunkSize));
std::cout<<hdr.ChunkSize<<std::endl;
in.seekg(8,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.WAVE), sizeof(hdr.WAVE));
for (int i = 0; i < sizeof(hdr.WAVE); ++i) {
// TODO: read/write in blocks
std::cout<<hdr.WAVE;
}
std::cout<<' '<<std::endl;
in.seekg(48,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.fmt), sizeof(hdr.fmt));
for (int i = 0; i < sizeof(hdr.fmt); ++i) {
// TODO: read/write in blocks
std::cout<<hdr.fmt;
}
std::cout<<' '<<std::endl;
in.seekg(52,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.Subchunk1Size), sizeof(hdr.Subchunk1Size));
std::cout<<hdr.Subchunk1Size<<std::endl;
in.seekg(56,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.AudioFormat), sizeof(hdr.AudioFormat));
std::cout<<hdr.AudioFormat<<std::endl;
in.seekg(58,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.NumOfChan), sizeof(hdr.NumOfChan));
std::cout<<hdr.NumOfChan<<std::endl;
in.seekg(60,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.SamplesPerSec), sizeof(hdr.SamplesPerSec));
std::cout<<hdr.SamplesPerSec<<std::endl;
in.seekg(64,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.bytesPerSec), sizeof(hdr.bytesPerSec));
std::cout<<hdr.bytesPerSec<<std::endl;
in.seekg(68,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.blockAlign), sizeof(hdr.blockAlign));
std::cout<<hdr.blockAlign<<std::endl;
in.seekg(70,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.bitsPerSample), sizeof(hdr.bitsPerSample));
std::cout<<hdr.bitsPerSample<<std::endl;
in.seekg(72,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.Subchunk2ID), sizeof(hdr.Subchunk2ID));
for (int i = 0; i < sizeof(hdr.Subchunk2ID); ++i) {
// TODO: read/write in blocks
std::cout<<hdr.Subchunk2ID;
}
std::cout<<' '<<std::endl;
in.seekg(76,std::ios::beg);
in.read(reinterpret_cast<char *>(&hdr.Subchunk2Size), sizeof(hdr.Subchunk2Size));
std::cout<<hdr.Subchunk2Size<<std::endl;
uint16_t * dat = new uint16_t[hdr.Subchunk2Size];
uint16_t dit;
float dumpi ;
float * dump = new float[hdr.Subchunk2Size] ;
for(int i = 78; i < hdr.Subchunk2Size; i++){
in.seekg(i,std::ios::beg);
in.read(reinterpret_cast<char *>(&dit), sizeof(dit));
dumpi = dit;
dump = 65535.f / dumpi;
std::cout<< dump<<' ';
i++;
}
std::cout<<' '<<std::endl;
return 0;
}
"
Statistics: Posted by L3G0 — Sat Aug 24, 2024 3:12 pm — Replies 6 — Views 128