Permalink
Browse files

Even more cleanup of RIFFReader

  • Loading branch information...
hrydgard committed Dec 4, 2017
1 parent 210b70f commit b47251dbd4f243a5fd4501b19ce56ac2303fca45
Showing with 35 additions and 79 deletions.
  1. +17 −17 UI/BackgroundAudio.cpp
  2. +10 −46 ext/native/file/chunk_file.cpp
  3. +8 −16 ext/native/file/chunk_file.h
View
@@ -31,10 +31,10 @@ class AT3PlusReader {
u8 at3_extradata[16];
int num_channels, sample_rate, numFrames, samplesPerSec, avgBytesPerSec, Nothing;
if (file_.descend('RIFF')) {
file_.readInt(); //get past 'WAVE'
if (file_.descend('fmt ')) { //enter the format chunk
int temp = file_.readInt();
if (file_.Descend('RIFF')) {
file_.ReadInt(); //get past 'WAVE'
if (file_.Descend('fmt ')) { //enter the format chunk
int temp = file_.ReadInt();
int format = temp & 0xFFFF;
switch (format) {
case 0xFFFE:
@@ -50,10 +50,10 @@ class AT3PlusReader {
num_channels = temp >> 16;
samplesPerSec = file_.readInt();
avgBytesPerSec = file_.readInt();
samplesPerSec = file_.ReadInt();
avgBytesPerSec = file_.ReadInt();
temp = file_.readInt();
temp = file_.ReadInt();
raw_bytes_per_frame_ = temp & 0xFFFF;
Nothing = temp >> 16;
@@ -64,41 +64,41 @@ class AT3PlusReader {
if (codec == PSP_CODEC_AT3) {
// The first two bytes are actually not a useful part of the extradata.
// We already read 16 bytes, so make sure there's enough left.
if (file_.getCurrentChunkSize() >= 32) {
file_.readData(at3_extradata, 16);
if (file_.GetCurrentChunkSize() >= 32) {
file_.ReadData(at3_extradata, 16);
} else {
memset(at3_extradata, 0, sizeof(at3_extradata));
}
}
file_.ascend();
file_.Ascend();
// ILOG("got fmt data: %i", samplesPerSec);
} else {
ELOG("Error - no format chunk in wav");
file_.ascend();
file_.Ascend();
return;
}
if (file_.descend('data')) { //enter the data chunk
int numBytes = file_.getCurrentChunkSize();
if (file_.Descend('data')) { //enter the data chunk
int numBytes = file_.GetCurrentChunkSize();
numFrames = numBytes / raw_bytes_per_frame_; // numFrames
raw_data_ = (uint8_t *)malloc(numBytes);
raw_data_size_ = numBytes;
if (/*raw_bytes_per_frame_ == 280 && */ num_channels == 1 || num_channels == 2) {
file_.readData(raw_data_, numBytes);
file_.ReadData(raw_data_, numBytes);
} else {
ELOG("Error - bad blockalign or channels");
free(raw_data_);
raw_data_ = 0;
return;
}
file_.ascend();
file_.Ascend();
} else {
ELOG("Error - no data chunk in wav");
file_.ascend();
file_.Ascend();
return;
}
file_.ascend();
file_.Ascend();
} else {
ELOG("Could not descend into RIFF file. Data size=%d", (int32_t)data.size());
return;
@@ -3,8 +3,6 @@
#include "file/zip_read.h"
#include "file/file_util.h"
// #define CHUNKDEBUG
inline uint32_t flipID(uint32_t id) {
return ((id >> 24) & 0xFF) | ((id >> 8) & 0xFF00) | ((id << 8) & 0xFF0000) | ((id << 24) & 0xFF000000);
}
@@ -22,7 +20,7 @@ RIFFReader::~RIFFReader() {
delete[] data_;
}
int RIFFReader::readInt() {
int RIFFReader::ReadInt() {
if (data_ && pos_ < eof_) {
pos_ += 4;
return *(int *)(data_ + pos_ - 4);
@@ -31,7 +29,7 @@ int RIFFReader::readInt() {
}
// let's get into the business
bool RIFFReader::descend(uint32_t id) {
bool RIFFReader::Descend(uint32_t id) {
if (depth_ > 30)
return false;
@@ -47,59 +45,44 @@ bool RIFFReader::descend(uint32_t id) {
int firstID = 0;
// let's search through children..
while (pos_ < eof_) {
stack[depth_].ID = readInt();
stack[depth_].ID = ReadInt();
if (firstID == 0) firstID = stack[depth_].ID | 1;
stack[depth_].length = readInt();
stack[depth_].length = ReadInt();
stack[depth_].startLocation = pos_;
if (stack[depth_].ID == id) {
found = true;
break;
} else {
seekTo(pos_ + stack[depth_].length); // try next block
pos_ += stack[depth_].length; // try next block
}
}
// if we found nothing, return false so the caller can skip this
if (!found) {
#ifdef CHUNKDEBUG
ILOG("Couldn't find %c%c%c%c", id, id >> 8, id >> 16, id >> 24);
#endif
stack[depth_] = temp;
seekTo(stack[depth_].parentStartLocation);
pos_ = stack[depth_].parentStartLocation;
return false;
}
// descend into it
// pos was set inside the loop above
eof_ = stack[depth_].startLocation + stack[depth_].length;
depth_++;
#ifdef CHUNKDEBUG
ILOG("Descended into %c%c%c%c", id, id >> 8, id >> 16, id >> 24);
#endif
return true;
}
void RIFFReader::seekTo(int _pos) {
pos_ = _pos;
}
// let's ascend out
void RIFFReader::ascend() {
void RIFFReader::Ascend() {
// ascend, and restore information
depth_--;
seekTo(stack[depth_].parentStartLocation);
pos_ = stack[depth_].parentStartLocation;
eof_ = stack[depth_].parentEOF;
#ifdef CHUNKDEBUG
int id = stack[depth_].ID;
ILOG("Ascended out of %c%c%c%c", id, id >> 8, id >> 16, id >> 24);
#endif
}
// read a block
void RIFFReader::readData(void *what, int count) {
void RIFFReader::ReadData(void *what, int count) {
memcpy(what, data_ + pos_, count);
pos_ += count;
count &= 3;
if (count) {
@@ -108,26 +91,7 @@ void RIFFReader::readData(void *what, int count) {
}
}
static std::string fromUnicode(const uint16_t *src, int len) {
std::string str;
str.resize(len);
for (int i = 0; i < len; i++) {
str[i] = src[i] > 255 ? ' ' : src[i];
}
return str;
}
std::string RIFFReader::readWString() {
int len = readInt();
uint16_t *text = new uint16_t[len + 1];
readData((char *)text, len * sizeof(uint16_t));
text[len] = 0;
std::string temp = fromUnicode(text, len);
delete[] text;
return temp;
}
int RIFFReader::getCurrentChunkSize() {
int RIFFReader::GetCurrentChunkSize() {
if (depth_)
return stack[depth_ - 1].length;
else
@@ -11,42 +11,34 @@
#include <string>
#include <cstdio>
#include "base/basictypes.h"
#include <cstdint>
class RIFFReader {
public:
RIFFReader(const uint8_t *data, int dataSize);
~RIFFReader();
bool descend(uint32_t id);
void ascend();
bool Descend(uint32_t id);
void Ascend();
int readInt();
void readInt(int &i) {i = readInt();}
void readData(void *data, int count);
// String readWString();
std::string readWString();
int ReadInt();
void ReadData(void *data, int count);
int getCurrentChunkSize();
int GetCurrentChunkSize();
bool failed() const { return didFail_; }
private:
struct ChunkInfo {
int startLocation;
int parentStartLocation;
int parentEOF;
unsigned int ID;
uint32_t ID;
int length;
};
ChunkInfo stack[32];
int depth_ = 0;
uint8_t *data_;
int pos_ = 0;
int eof_ = 0;
int depth_ = 0;
bool didFail_ = false;
void seekTo(int _pos);
int getPos() const {return pos_;}
};

0 comments on commit b47251d

Please sign in to comment.