473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C++ Reading in 16bit Audio File - data size not correct

4
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:

Expand|Select|Wrap|Line Numbers
  1.  #pragma pack(push, 1)
  2.     struct s_riff_hdr
  3.     {
  4.       char id[4];
  5.       uint32_t size;
  6.       char type[4];
  7.     };
  8.  
  9.     struct s_chunk_hdr
  10.     {
  11.        char id[4];
  12.        uint32_t size;
  13.     };
  14.  
  15.     struct s_wavefmt
  16.     {
  17.       uint16_t format_tag;
  18.       uint16_t channels;
  19.       uint32_t sample_rate;
  20.       uint32_t avg_bytes_sec;
  21.       uint16_t block_align;
  22.     };
  23.  
  24.     struct s_wavefmtex
  25.     {
  26.       s_wavefmt fmt;
  27.       uint16_t bits_per_sample;
  28.       uint16_t extra_size;
  29.     };
  30.  
  31.     struct s_pcmwavefmt
  32.     {
  33.       s_wavefmt fmt;
  34.       uint16_t bits_per_sample;
  35.     };
  36.  
  37.     bool readWave(ifstream &file)
  38.     {
  39.        s_riff_hdr riff_hdr;
  40.        s_chunk_hdr chunk_hdr;
  41.        uint32_t padded_size;
  42.        vector<uint8_t> fmt_data;
  43.        s_wavefmt *fmt = NULL;
  44.  
  45.        file.read(reinterpret_cast<char*>(&riff_hdr), sizeof(riff_hdr));
  46.        if (!file) return false;
  47.  
  48.        if (memcmp(riff_hdr.id, "RIFF", 4) != 0) return false;
  49.  
  50.        cout << "size=" << riff_hdr.size << endl;
  51.        cout << "type=" << string(riff_hdr.type, 4) << endl;
  52.  
  53.        if (memcmp(riff_hdr.type, "WAVE", 4) != 0) return false;
  54.  
  55.        // chunks can be in any order!
  56.        // there is no guarantee that "fmt" is the first chunk.
  57.        // there is no guarantee that "fmt" is immediately followed by "data".
  58.        // There can be other chunks present!
  59.  
  60.        do
  61.        {
  62.            file.read(reinterpret_cast<char*>(&chunk_hdr), sizeof(chunk_hdr));
  63.            if (!file) return false;
  64.  
  65.            padded_size = ((chunk_hdr.size + 1) & ~1);
  66.  
  67.            cout << "id=" << string(chunk_hdr.id, 4) << endl;
  68.            cout << "size=" << chunk_hdr.size << endl;
  69.            cout << "padded size=" << padded_size << endl;
  70.  
  71.            if (memcmp(chunk_hdr.id, "fmt ", 4) == 0) 
  72.            {
  73.                if (chunk_hdr.size < sizeof(s_wavefmt)) return false;
  74.  
  75.                fmt_data.resize(padded_size);
  76.  
  77.                file.read(reinterpret_cast<char*>(&fmt_data[0]), padded_size);
  78.                if (!file) return false;
  79.  
  80.                fmt = reinterpret_cast<s_wavefmt*>(&fmt_data[0]);
  81.  
  82.                cout << "format_tag=" << fmt->format_tag << endl;
  83.                cout << "channels=" << fmt->channels << endl;
  84.                cout << "sample_rate=" << fmt->sample_rate << endl;
  85.                cout << "avg_bytes_sec=" << fmt->avg_bytes_sec << endl;
  86.                cout << "block_align=" << fmt->block_align << endl;
  87.  
  88.                if (fmt->format_tag == 1) // PCM
  89.                {
  90.                   if (chunk_hdr.size < sizeof(s_pcmwavefmt)) return false;
  91.  
  92.                   s_pcmwavefmt *pcm_fmt = reinterpret_cast<s_pcmwavefmt*>(fmt);
  93.  
  94.                   cout << "bits_per_sample=" << pcm_fmt->bits_per_sample << endl;
  95.                }
  96.                else
  97.                {
  98.                   if (chunk_hdr.size < sizeof(s_wavefmtex)) return false;
  99.  
  100.                   s_wavefmtex *fmt_ex = reinterpret_cast<s_wavefmtex*>(fmt);
  101.  
  102.                   cout << "bits_per_sample=" << fmt_ex->bits_per_sample << endl;
  103.                   cout << "extra_size=" << fmt_ex->extra_size << endl;
  104.  
  105.                   if (fmt_ex->extra_size != 0)
  106.                   {
  107.                       if (chunk_hdr.size < (sizeof(s_wavefmtex) + fmt_ex->extra_size)) return   
  108.                          false;
  109.  
  110.                       uint8_t *extra_data = reinterpret_cast<uint8_t*>(fmt_ex + 1);
  111.                     // use extra_data, up to extra_size bytes, as needed...
  112.                  }
  113.               }
  114.           }
  115.           else if (memcmp(chunk_hdr.id, "data", 4) == 0)
  116.           {
  117.               // process chunk data, according to fmt, as needed...
  118.             //cout << padded_size;
  119.             file.ignore(padded_size);
  120.             if (!file) return false;
  121.         }
  122.         else
  123.         {
  124.  
  125.             // process other chunks as needed...
  126.             file.ignore(padded_size);
  127.             if (!file) return false;
  128.         }
  129.     }
  130.     while (!file.eof());
  131.  
  132.     return true;
  133.     }
  134.  
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.
Sep 13 '13 #1
0 1176

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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
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);...
3
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...
2
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...
3
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...
7
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
1
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)...
1
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...
5
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"); ...
4
nicebasic
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.