|
From: Alex Shulgin on 8 Jan 2008 22:49 On Jan 8, 4:20 pm, sidkdbl07 <sidkwak...(a)gmail.com> wrote: > > On Dec 15 2007, 3:56 pm, Alex Shulgin <alex.shul...(a)gmail.com> wrote: > > > On Dec 15, 6:12 am, sidkdbl07 <sidkwak...(a)gmail.com> wrote: > > > > I have a binary file with 16-bit samples and I'd like to down convert > > > to 1-bit > > > > Here's what I have but it doesn't work. any suggestions? > > > You cannot feed a binary file to your program and expect it to work. > > Try this instead: > > > $ cat input.txt > > 0000000000000000 > > 1111111111111111 > > $ ./a.out input.txt > > Found on > > $ cat output.bin > > 01 > > I don't understand why I can't load a binary file and expect it to > work. I have a binary file that was written with 16-bit samples. Ergo, > if I read in 16 bits, then I should have one sample, no? > > Your file called input.txt is an ASCII file, so it doesn't look like > it would really serve my purposes. Please correct me if I'm wrong. Sorry, I think I wasn't specific enough but hoped that you could figure out the rest by yourself. :) My point was that you cannot feed a binary file to _the_ program you posted, not that you can't do that with some other program. The problem here is that std::bitset<n> once read from the stream expects a series of characters representing 'zero' and 'one', not n-bit samples. So one of the ways to solve you problem is to read samples by hand, like this: std::ifstream input(infile, std::ios::binary); std::ofstream output(outfile, std::ios::binary); uint8_t out = 0; size_t bits = 0; while (input) { uint16_t sample; input.read(& sample, sizeof(sample)); if (input.eof()) break; // TODO: handle byte-order if (sample & 0x8000) // highest bit set // add up the sample to 'out' ++bits; if (bits == sizeof(out)) { output.write(& out, sizeof(out)); bits = 0; } } You have to decide on byte-order of the input samples and bit-order of the output samples in order for this to work across platforms. Good luck! -- Alex Shulgin PS: the code is out of the top of my head -- might not even compile. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Pages: 1 Prev: Can template instantiation be a POD? Next: Policy and Typedef |