I have a G711 codec, and i would like to convert a wav file and save this. I read a sample.wav file, and convert it with the G711 ITU-T codec, and save the encoded data to an output.wav file.
Here is my code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "G711.h"
#include <vector>
using namespace std;
// WAVE PCM soundfile format (you can find more in https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ )
typedef struct header_file
{
char chunk_id[4];
int chunk_size;
char format[4];
char subchunk1_id[4];
int subchunk1_size;
short int audio_format;
short int num_channels;
int sample_rate; // sample_rate denotes the sampling rate.
int byte_rate;
short int block_align;
short int bits_per_sample;
char subchunk2_id[4];
int subchunk2_size; // subchunk2_size denotes the number of samples.
} header;
typedef struct header_file* header_p;
int main()
{
FILE * infile = fopen("sample.wav","rb"); // Open wave file in read mode
FILE * outfile = fopen("Output.wav","wb"); // Create output ( wave format) file in write mode
int BUFSIZE = 512; // BUFSIZE can be changed according to the frame size required (eg:512)
int count = 0; // For counting number of frames in wave file.
short int buff16[BUFSIZE]; // short int used for 16 bit as input data format is 16 bit PCM audio
header_p meta = (header_p)malloc(sizeof(header)); // header_p points to a header struct that contains the wave file metadata fields
int nb; // variable storing number of byes returned
if (infile)
{
fread(meta, 1, sizeof(header), infile);
fwrite(meta,1, sizeof(*meta), outfile);
cout << " Size of Header file is "<<sizeof(*meta)<<" bytes" << endl;
cout << " Sampling rate of the input wave file is "<< meta->sample_rate <<" Hz" << endl;
cout << " Number of samples in wave file are " << meta->subchunk2_size << " samples" << endl;
cout << " ID: " << meta->chunk_id << endl;
cout << "chunk_size" << meta->chunk_size << endl;
cout << "format" << meta->format << endl;
cout << "subchunk1"<<meta->subchunk1_id << endl;
cout << "subchunk1_size"<<meta->subchunk1_size << endl;
cout << "audio_format"<<meta->audio_format<<endl;
cout << "num_channels"<<meta->num_channels<<endl;
cout << "byte_rate"<<meta->byte_rate<<endl;
cout << "block_align"<<meta->block_align<<endl;
cout << "bits_per_sample"<<meta->bits_per_sample<<endl;
cout << "subchunk2_id"<<meta->subchunk2_id<<endl;
while (!feof(infile))
{
nb = fread(buff16,1,BUFSIZE,infile); // Reading data in chunks of BUFSIZE
size_t ds = sizeof(buff16) >> 1;
cout << "ds: " << ds <<endl;
vector<short> vec_src(sizeof(buff16), 0);
vector<unsigned char> vec_dst(ds, 0);
for (int i = 0; i < sizeof(buff16); i++) {
vec_src[i] = buff16[i];
}
short* src = &vec_src[0];
uint8_t* dst = &vec_dst[0];
G711::ALawEncode(dst, buff16, sizeof(buff16));
unsigned char arr[ds];
for (unsigned i = 0; i < ds; i++) {
arr[i] = *(dst + i);
}
count++; // Incrementing Number of frames
/* Insert your processing code here*/
fwrite(arr,sizeof(unsigned char),sizeof(arr),outfile); // Writing read data into output file
}
cout << " Number of frames in the input wave file are " <<count << endl;
}
getchar();
return 0;
}
I dont know very well C++, so please help me. If i run this code i get Output.wav file but i cant play. Whats wrong?
Related
I am trying to read correctly a WAVE file, PCM, mono, 16 bits (2 bytes per sample). I have managed to read the header. The problem is reading (writing) the data part.
As far as I understand the 16-bit samples in the data chunk are little-endian, and "split" into two chunks of 8 bits each. So for me a way to read the correct data should be:
Read file and put chunks into two differentint8_t variables (or a std::vector<int8_t>..)
In some way "join" these two variables to make a int16_t and being able to process it.
The problem is I have no idea on how to deal with the little-endianness and the fact that these samples aren't unsigned, so I can't use the << operator.
This is one of the test I've done, without success:
int8_t buffer[], firstbyte,secondbyte;
int16_t result;
std::vector<int16_t> data;
while(Read bytes and put them in buffer){
for (int j=0;j<bytesReadFromTheFile;j+=2){
firstbyte = buffer[j];
secondbyte = buffer[j+1];
result = (firstbyte);
result = (result << 8)+secondbyte; //shift first byte and add second
data.push_back(result);
}
}
To be more verbose, I am using this code found online and created a class starting from it (The process is the same, but the Class configuration is very long and has many features that aren't useful for this problem):
#include <iostream>
#include <string>
#include <fstream>
#include <cstdint>
using std::cin;
using std::cout;
using std::endl;
using std::fstream;
using std::string;
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[])
{
wav_hdr wavHeader;
int headerSize = sizeof(wav_hdr), filelength = 0;
const char* filePath;
string input;
if (argc <= 1)
{
cout << "Input wave file name: ";
cin >> input;
cin.get();
filePath = input.c_str();
}
else
{
filePath = argv[1];
cout << "Input wave file name: " << filePath << endl;
}
FILE* wavFile = fopen(filePath, "r");
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)
{
* /** 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;
}
fclose(wavFile);
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;
}
The problem is in the /** DO SOMETHING WITH THE WAVE DATA HERE **/ . I have no Idea on how to get the sample value.
I'm a Java programmer, not C++, but I've dealt with this often.
The PCM data is organized by frame. If it's mono, little-endian, 16-bit the first byte will be the lower half of the value, and the second byte will be the upper and include the sign bit. Big-endian will reverse the bytes. If it's stereo, a full frame (I think it's left then right but I'm not sure) is presented intact before moving on to the next frame.
I'm kind of amazed at all the code being shown. In Java, the following suffices for PCM encoded as signed values:
public short[] fromBufferToPCM(short[] audioPCM, byte[] buffer)
{
for (int i = 0, n = buffer.length; i < n; i += 2)
{
audioPCM[i] = (buffer[i] & 0xff) | (buffer[i + 1] << 8);
}
return audioBytes;
}
IDK how to translate that directly to C++, but we are simply OR-ing together the two bytes with the second one first being shifted 8 places to the left. The pure shift picks up the sign bit. (I can't recall why the & 0xff was included--I wrote this a long while back and it works.)
Curious why so many answers are in the comments and not posted as answers. I thought comments were for requests to clarify the OP's question.
something like this works:
int8_t * tempBuffer = new int8_t [numSamples];
int index_for_loop = 0;
float INT16_FAC = pow(2,15) - 1;
double * outbuffer = new double [numSamples];
inside while loop:
for(int i = 0; i < BUFFER_SIZE; i += 2)
{
firstbyte = buffer[i];
secondbyte = buffer[i + 1];
result = firstbyte;
result = (result << 8) +secondbyte;
tempBuffer[index_for_loop] = result;
index_for_loop += 1;
}
then normalize between -1 and 1 by doing:
for(int i = 0; i <numSamples; i ++)
{
outbuffer[i] = float(tempBuffer[i]) / INT16_FAC;
}
got normalize from:
sms-tools
Note : this works for mono files with 44100 samplerate and 16 bit resolution.
I am currently using the C++ to implement my code. The idea is I want to read a wav file through a program and then output its waveform on the screen. I have already found some references there:C++ Reading the Data part of a WAV file. and here is my code of reading the audio file. I have no idea of how to generate the waveform .
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
using std::string;
using std::fstream;
typedef struct WAV_HEADER
{
/* for the part of RIEF Chunk Descriptor */
uint8_t RIFF[4]; // RIFF Header Magic header
uint32_t ChunkSize; // RIFF Chunk Size
uint8_t WAVE[4]; // WAVE Header
/* for the part of "fmt" subChunk */
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
/* For the part of "data" subChunk */
uint8_t Subchunk2ID[4]; // "data" string
uint32_t Subchunk2Size; // Sampled data length
}wav_hdr;
int getFileSize(FILE* inFile);
int main(int argc, char* argv[])
{
wav_hdr wavHeader;
int headerSize = sizeof(wav_hdr), filelength = 0;
const char* filePath;
string input;
if (argc <= 1)
{
cout << "Input wave file name: ";
cin >> input;
cin.get();
filePath = input.c_str();
}
else
{
filePath = argv[1];
cout << "Input wave file name: " << filePath << endl;
}
FILE* wavFile = fopen(filePath, "r");
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)
{
/** 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;
}
fclose(wavFile);
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;
}
I would have left this as a comment with code gists and not an answer since it doesn't answer your question directly but I don't have enough reputation for this.
I did what you're trying to do with Qt, most of my code comes from Qt's documentation. You need to find the peak value of the samples and then draw that. Let me share some code that could give you an idea on how you might want to do it.
void Waveform::appendSamples()
{
buffer = audioDecoder->read();
qreal peak = getPeakValue(buffer.format());
const qint16 *data = buffer.constData<qint16>();
int count = buffer.sampleCount() / 2;
for (int i = 0; i < count; i += 1200){ // I want 40 samples per second currently assuming 48kHz
double val = data[i]/peak;
samples.append(val * 300); // *300 for scaling
}
}
qreal Waveform::getPeakValue(const QAudioFormat &format)
{
qreal ret(0);
if (format.isValid()){
switch (format.sampleType()) {
case QAudioFormat::Unknown:
break;
case QAudioFormat::Float:
if (format.sampleSize() != 32)
ret = 0;
else
ret = 1.00003;
break;
case QAudioFormat::SignedInt:
if (format.sampleSize() == 32)
ret = INT_MAX;
else if (format.sampleSize() == 16)
ret = SHRT_MAX;
else if (format.sampleSize() == 8)
ret = CHAR_MAX;
break;
case QAudioFormat::UnSignedInt:
if (format.sampleSize() == 32)
ret = UINT_MAX;
else if (format.sampleSize() == 16)
ret = USHRT_MAX;
else if (format.sampleSize() == 8)
ret = UCHAR_MAX;
break;
default:
break;
}
}
return ret;
}
For the drawing part in Qt (probably not useful to you)
QSGNode* WaveformRenderer::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
{
QSGSimpleRectNode *n = static_cast<QSGSimpleRectNode *>(oldNode);
if(!n){
n = new QSGSimpleRectNode();
n->setColor(Qt::red);
}
QVector<double> samples = wav->getSamples(); //retrieve the samples
int numberOfSamples = samples.size();
setItemWidth(qreal(numberOfSamples * 2)); //signal stuff for qml
for(int i = 0; i < numberOfSamples; ++i){
QSGSimpleRectNode *temp = new QSGSimpleRectNode();
temp->setColor(Qt::green);
temp->setRect(i * 2, height()/2 - samples[i], 2, samples[i] * 2);
n->appendChildNode(temp);
}
return n;
}
I am new in C++ in general, and thus, also in file handling in C++.
I need to read a .raw file which has 16-bit integers, and have dimension 512 x 512.
For that I am using following code:
ifstream myData("myData.raw");
short value;
int i = 0;
char buf[sizeof(short)];
while (myData.read(buf,sizeof(buf)))
{
memcpy(&value, buf, sizeof(value));
cout << value << " ";
i++;
}
cout << endl << "Total count: " << i << endl;
The value i am getting for i is not 512 x 512. So I guess something is not right.
Can someone please help me in this regard?
The default open mode is "text" and some characters will be possibly dropped or treated as end of file. ios::binary stops these alterations.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream myData("myData.raw", ios::binary);
short value;
int i = 0;
char buf[sizeof(short)];
while (myData.read(buf, sizeof(buf)))
{
memcpy(&value, buf, sizeof(value));
cout << value << " ";
i++;
}
cout << endl << "Total count: " << i << endl;
}
I'm trying to read in the Header information of a .wav file.
If I have a .wav file that has a low sample rate (22050) it will read all the information in perfectly, however, if I have a higher Sample Rate (8000) then it fails to read in some information:
"dataSize" set's when using a 22050 .wav file however, when using a 8000 .wav file it does not get set and just displays some random numbers.. e.g. "1672494080" when the actual size is around 4k-4.5k in size.
Any suggestions to where I am going wrong?
EDIT:
#include <iostream>
#include <fstream>
#include <vector>
#include <inttypes.h>
#include <stdint.h>
#include <math.h>
using namespace std;
struct riff_hdr
{
char id[4];
uint32_t size;
char type[4];
};
struct chunk_hdr
{
char id[4];
uint32_t size;
};
struct wavefmt
{
uint16_t format_tag;
uint16_t channels;
uint32_t sample_rate;
uint32_t avg_bytes_sec;
uint16_t block_align;
uint16_t bits_per_sample;
uint16_t extra_size;
};
riff_hdr riff;
chunk_hdr chunk;
wavefmt fmt = {0};
uint32_t padded_size;
vector<uint8_t> chunk_data;
bool readHeader(ifstream &file) {
file.read(reinterpret_cast<char*>(&riff), sizeof(riff));
if (memcmp(riff.id, "RIFF", 4) == 0)
{
cout << "size=" << riff.size << endl;
cout << "id=" << string(riff.type, 4) << endl;
if (memcmp(riff.type, "WAVE", 4) == 0)
{
// chunks can be in any order!
// there is no guarantee that "fmt" is the first chunk.
// there is no guarantee that "fmt" is immediately followed by "data".
// There can be other chunks present!
do {
file.read(reinterpret_cast<char*>(&chunk), sizeof(chunk));
padded_size = ((chunk.size + 2 - 1) & ~1);
cout << "id=" << string(chunk.id, 4) << endl;
cout << "size=" << chunk.size << endl;
cout << "padded size=" << padded_size << endl;
if (memcmp(chunk.id, "fmt\0", 4) == 0)
{
if (chunk.size < sizeof(wavefmt))
{
// error!
file.ignore(padded_size);
}else{
// THIS block doesn't seem to be executing
chunk_data.resize(padded_size);
file.read(reinterpret_cast<char*>(&chunk_data[0]), padded_size);
fmt = *(reinterpret_cast<wavefmt*>(&chunk_data[0]));
cout << "format_tag=" << fmt.format_tag << endl;
cout << "channels=" << fmt.channels << endl;
cout << "sample_rate=" << fmt.sample_rate << endl;
cout << "avg_bytes_sec=" << fmt.avg_bytes_sec << endl;
cout << "block_align=" << fmt.block_align << endl;
cout << "bits_per_sample=" << fmt.bits_per_sample << endl;
cout << "extra_size=" << fmt.extra_size << endl;
}
if(fmt.format_tag != 1)
{
uint8_t *extra_data = &chunk_data[sizeof(wavefmt)];
}
}else if(memcmp(chunk.id, "data", 4) == 0) {
file.ignore(padded_size);
}else{
file.ignore(padded_size);
}
}while ((!file) && (!file.eof()));
}
}
return true;
}
int main()
{
ifstream file("example2.wav");
readHeader(file);
return 0;
}
OUTPUT:
size=41398
id=WAVE
id=fmt
size=18
padded size=18
chunk_data size=0
Where am I going wrong?
You have two problems with your code:
There is a 2-byte integer after the bitsPerSample value that you are not reading. It specifies the size of any extra data in that chunk. If the value of format2 indicates a PCM format only, you can ignore the value of the integer (it will usually be 0 anyway, but it may also be garbage), but you still have to account for its presense. The integer cannot be ignored for non-PCM formats, you have to read the value and then read how many bytes it says. You need to make sure you are reading the entire chunk before then entering your while loop, otherwise you will not be on the correct starting position in the file to read further chunks.
You are not taking into account that chunks are padded to the nearest WORD boundary, but the chunk size does not include any padding. When you call seekg(), you need to round the value up to the next WORD boundary.
Update: based on the new code you posted, it should look more like this instead:
#include <iostream>
#include <fstream>
#include <vector>
#include <inttypes.h>
#include <stdint.h>
#include <math.h>
using namespace std;
// if your compiler does not have pshpack1.h and poppack.h, then
// use #pragma pack instead. It is important that these structures
// be byte-alignd!
#include <pshpack1.h>
struct s_riff_hdr
{
char id[4];
uint32_t size;
char type[4];
};
struct s_chunk_hdr
{
char id[4];
uint32_t size;
};
struct s_wavefmt
{
uint16_t format_tag;
uint16_t channels;
uint32_t sample_rate;
uint32_t avg_bytes_sec;
uint16_t block_align;
};
struct s_wavefmtex
{
s_wavefmt fmt;
uint16_t bits_per_sample;
uint16_t extra_size;
};
struct s_pcmwavefmt
{
s_wavefmt fmt;
uint16_t bits_per_sample;
};
#include <poppack.h>
bool readWave(ifstream &file)
{
s_riff_hdr riff_hdr;
s_chunk_hdr chunk_hdr;
uint32_t padded_size;
vector<uint8_t> fmt_data;
s_wavefmt *fmt = NULL;
file.read(reinterpret_cast<char*>(&riff_hdr), sizeof(riff_hdr));
if (!file) return false;
if (memcmp(riff_hdr.id, "RIFF", 4) != 0) return false;
cout << "size=" << riff_hdr.size << endl;
cout << "type=" << string(riff_hdr.type, 4) << endl;
if (memcmp(riff_hdr.type, "WAVE", 4) != 0) return false;
// chunks can be in any order!
// there is no guarantee that "fmt" is the first chunk.
// there is no guarantee that "fmt" is immediately followed by "data".
// There can be other chunks present!
do
{
file.read(reinterpret_cast<char*>(&chunk_hdr), sizeof(chunk_hdr));
if (!file) return false;
padded_size = ((chunk_hdr.size + 1) & ~1);
cout << "id=" << string(chunk_hdr.id, 4) << endl;
cout << "size=" << chunk_hdr.size << endl;
cout << "padded size=" << padded_size << endl;
if (memcmp(chunk_hdr.id, "fmt ", 4) == 0)
{
if (chunk_hdr.size < sizeof(s_wavefmt)) return false;
fmt_data.resize(padded_size);
file.read(reinterpret_cast<char*>(&fmt_data[0]), padded_size);
if (!file) return false;
fmt = reinterpret_cast<s_wavefmt*>(&fmt_data[0]);
cout << "format_tag=" << fmt->format_tag << endl;
cout << "channels=" << fmt->channels << endl;
cout << "sample_rate=" << fmt->sample_rate << endl;
cout << "avg_bytes_sec=" << fmt->avg_bytes_sec << endl;
cout << "block_align=" << fmt->block_align << endl;
if (fmt->format_tag == 1) // PCM
{
if (chunk_hdr.size < sizeof(s_pcmwavefmt)) return false;
s_pcmwavefmt *pcm_fmt = reinterpret_cast<s_pcmwavefmt*>(fmt);
cout << "bits_per_sample=" << pcm_fmt->bits_per_sample << endl;
}
else
{
if (chunk_hdr.size < sizeof(s_wavefmtex)) return false;
s_wavefmtex *fmt_ex = reinterpret_cast<s_wavefmtex*>(fmt);
cout << "bits_per_sample=" << fmt_ex->bits_per_sample << endl;
cout << "extra_size=" << fmt_ex->extra_size << endl;
if (fmt_ex->extra_size != 0)
{
if (chunk_hdr.size < (sizeof(s_wavefmtex) + fmt_ex->extra_size)) return false;
uint8_t *extra_data = reinterpret_cast<uint8_t*>(fmt_ex + 1);
// use extra_data, up to extra_size bytes, as needed...
}
}
}
else if (memcmp(chunk_hdr.id, "data", 4) == 0)
{
// process chunk data, according to fmt, as needed...
file.ignore(padded_size);
if (!file) return false;
}
else
{
// process other chunks as needed...
file.ignore(padded_size);
if (!file) return false;
}
}
while (!file.eof());
return true;
}
int main()
{
ifstream file("example2.wav");
readWave(file);
return 0;
}
I am writing a simple program to convert grayscale binary (P5) to grayscale ascii (P2) but am having trouble reading in the binary and converting it to int.
#include <iostream>
#include <fstream>
#include <sstream>
using namespace::std;
int usage(char* arg) {
// exit program
cout << arg << ": Error" << endl;
return -1;
}
int main(int argc, char* argv[]) {
int rows, cols, size, greylevels;
string filetype;
// open stream in binary mode
ifstream istr(argv[1], ios::in | ios::binary);
if(istr.fail()) return usage(argv[1]);
// parse header
istr >> filetype >> rows >> cols >> greylevels;
size = rows * cols;
// check data
cout << "filetype: " << filetype << endl;
cout << "rows: " << rows << endl;
cout << "cols: " << cols << endl;
cout << "greylevels: " << greylevels << endl;
cout << "size: " << size << endl;
// parse data values
int* data = new int[size];
int fail_tracker = 0; // find which pixel failing on
for(int* ptr = data; ptr < data+size; ptr++) {
char t_ch;
// read in binary char
istr.read(&t_ch, sizeof(char));
// convert to integer
int t_data = static_cast<int>(t_ch);
// check if legal pixel
if(t_data < 0 || t_data > greylevels) {
cout << "Failed on pixel: " << fail_tracker << endl;
cout << "Pixel value: " << t_data << endl;
return usage(argv[1]);
}
// if passes add value to data array
*ptr = t_data;
fail_tracker++;
}
// close the stream
istr.close();
// write a new P2 binary ascii image
ofstream ostr("greyscale_ascii_version.pgm");
// write header
ostr << "P2 " << rows << cols << greylevels << endl;
// write data
int line_ctr = 0;
for(int* ptr = data; ptr < data+size; ptr++) {
// print pixel value
ostr << *ptr << " ";
// endl every ~20 pixels for some readability
if(++line_ctr % 20 == 0) ostr << endl;
}
ostr.close();
// clean up
delete [] data;
return 0;
}
sample image - Pulled this from an old post. Removed the comment within the image file as I am not worried about this functionality now.
When compiled with g++ I get output:
$> ./a.out a.pgm
filetype: P5
rows: 1024
cols: 768
greylevels: 255
size: 786432
Failed on pixel: 1
Pixel value: -110
a.pgm: Error
The image is a little duck and there's no way the pixel value can be -110...where am I going wrong?
Thanks.
greylevels: 255
-110 is 146 as an unsigned char. It appears you are on a platform where char is a signed type, try using unsigned char.
If you cannot have negative values , use an unsigned int * instead of int* for your pixel pointers. This way you won't have values read as signed values
You need a correction in output:
ostr << "P2\n" << rows << " "<< cols << " "<< greylevels << endl;