I'm attempting to read in .wav files, however, whenever I try to read in a 16Bit audio file, the "data" chunk size is not correct, however, for an 8bit file it is correct. The code is below: -
#pragma pack(push, 1)
-
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;
-
};
-
-
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...
-
//cout << padded_size;
-
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;
-
}
-
I have attempted to convert the "padded_size" from 8bit to 16bit, however, again, this does not seem to want to work. Any help would be greatly appreciated.
0 1176 Sign in to post your reply or Sign up for a free account.
Similar topics
by: SB |
last post by:
Hello. I have an input file which is laid out in the following manner...
Name
Day 1
am time 1
am time 2
appointment
pm time 1
pm time 2
appointment
Day 2
|
by: Jason Heyes |
last post by:
I want to read the binary contents of a file whose size is over 1 megabyte.
I tried to use this function.
bool read_file(const char *fname, std::vector<char> &data)
{
std::ifstream in(fname);...
|
by: abrtlt |
last post by:
I would like to have a web page in which, when the user clicks on any
of several specific elements, a specific audio file is played, without
reloading the page.
The specific audio file name is...
|
by: chitara |
last post by:
Hi guys!!
can anyone please tell me how to convert a audio file size to length.
Say I have a file(abc.mpeg) which is 4.6 MB in size & its length is 4.58 minutes.
So is there any function in php...
|
by: Udhay |
last post by:
Sir,
I want to read the data of an audio file in c++ (Windows).
What is the API which helps to read the data of an audio file.
I want to read BitRate,Audio Sample Size,Audio Sample rate and...
|
by: Udhay |
last post by:
How to get the frequency of an audio file and how to separate the low
and high frequency of an audio file
|
by: vijaylaxmi |
last post by:
hello,
i want to open my audio file in binary mode and want to perform action of reading and writing on binary mode.
then after whole process i wish that my file is save in its own format(audio)...
|
by: =?Utf-8?B?Qm9iQWNoZ2lsbA==?= |
last post by:
I am using Windows Media Player to play my half second audio files one after
the other using the code below...
My problem comes when I play a second audio file immediately after the first
one...
|
by: pankajs |
last post by:
hello !
i m reading a wav file by int b=ins.read(); and writing b on other wav file by
outs.write(b); in a loop until b==-1
where
InputStream ins = new FileInputStream("C:/audio.wav"); ...
|
by: nicebasic |
last post by:
I have written a small French-English Dictionary using VB.
I have come to a serious problem which is new to me. Maybe I didn't notice this problem before, but it's a bad bug in my program.
I...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |