Bug 1130223 - Add an implementation to the big endian conditional. r=jesup

This commit is contained in:
Steve Singer 2015-02-15 09:36:00 +01:00
parent 2881077b96
commit 4f3952c197
2 changed files with 50 additions and 6 deletions

View File

@ -45,13 +45,18 @@ WavReader::~WavReader() {
}
size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) {
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Need to convert samples to big-endian when reading from WAV file"
#endif
const size_t read =
fread(samples, sizeof(*samples), num_samples, file_handle_);
// If we didn't read what was requested, ensure we've reached the EOF.
CHECK(read == num_samples || feof(file_handle_));
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
//convert to big-endian
for(size_t idx = 0; idx < num_samples; idx++) {
samples[idx] = (samples[idx]<<8) | (samples[idx]>>8);
}
#endif
return read;
}
@ -99,10 +104,17 @@ WavWriter::~WavWriter() {
void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) {
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Need to convert samples to little-endian when writing to WAV file"
#endif
int16_t * le_samples = new int16_t[num_samples];
for(size_t idx = 0; idx < num_samples; idx++) {
le_samples[idx] = (samples[idx]<<8) | (samples[idx]>>8);
}
const size_t written =
fwrite(le_samples, sizeof(*le_samples), num_samples, file_handle_);
delete []le_samples;
#else
const size_t written =
fwrite(samples, sizeof(*samples), num_samples, file_handle_);
#endif
CHECK_EQ(num_samples, written);
num_samples_ += static_cast<uint32_t>(written);
CHECK(written <= std::numeric_limits<uint32_t>::max() ||

View File

@ -128,7 +128,39 @@ static inline std::string ReadFourCC(uint32_t x) {
return std::string(reinterpret_cast<char*>(&x), 4);
}
#else
#error "Write be-to-le conversion functions"
static inline void WriteLE16(uint16_t* f, uint16_t x) {
*f = ((x << 8) & 0xff00) | ( ( x >> 8) & 0x00ff);
}
static inline void WriteLE32(uint32_t* f, uint32_t x) {
*f = ( (x & 0x000000ff) << 24 )
| ((x & 0x0000ff00) << 8)
| ((x & 0x00ff0000) >> 8)
| ((x & 0xff000000) >> 24 );
}
static inline void WriteFourCC(uint32_t* f, char a, char b, char c, char d) {
*f = (static_cast<uint32_t>(a) << 24 )
| (static_cast<uint32_t>(b) << 16)
| (static_cast<uint32_t>(c) << 8)
| (static_cast<uint32_t>(d) );
}
static inline uint16_t ReadLE16(uint16_t x) {
return (( x & 0x00ff) << 8 )| ((x & 0xff00)>>8);
}
static inline uint32_t ReadLE32(uint32_t x) {
return ( (x & 0x000000ff) << 24 )
| ( (x & 0x0000ff00) << 8 )
| ( (x & 0x00ff0000) >> 8)
| ( (x & 0xff000000) >> 24 );
}
static inline std::string ReadFourCC(uint32_t x) {
x = ReadLE32(x);
return std::string(reinterpret_cast<char*>(&x), 4);
}
#endif
static inline uint32_t RiffChunkSize(uint32_t bytes_in_payload) {