old-bannertool/source/3ds/cwav.cpp

122 lines
3.2 KiB
C++
Raw Normal View History

#include "cwav.h"
#include <stdlib.h>
#include <string.h>
2015-01-26 03:04:27 +00:00
typedef enum {
PCM8,
PCM16,
TODO
} CWAVType;
typedef struct {
char magic[4] = {'C', 'W', 'A', 'V'};
u16 endianess = 0xFEFF;
u16 structLength = 0x40;
u32 unknown0 = 0;
u32 fileSize;
u32 numChunks = 2;
u32 infoChunkFlags = 0x7000;
u32 infoChunkOffset;
u32 infoChunkLength;
u32 dataChunkFlags = 0x7000;
u32 dataChunkOffset;
u32 dataChunkLength;
u8 reserved[0x14] = {0};
2015-01-26 03:04:27 +00:00
} CWAVHeader;
typedef struct {
char magic[4] = {'I', 'N', 'F', 'O'};
u32 length = 0xC0;
u32 type;
u32 sampleRate;
u32 unknown1 = 0;
u32 totalSamples;
u32 unknown2 = 0;
u32 totalChannels;
2015-01-26 03:04:27 +00:00
} CWAVInfoHeader;
typedef struct {
u32 flags = 0x7100;
u32 offset;
2015-01-26 03:04:27 +00:00
} CWAVChannelDataPointer;
typedef struct {
u32 flags = 0x1F00;
u32 offset;
u32 unknown3 = 0;
u32 unknown4 = 0;
u32 padding = 0;
2015-01-26 03:04:27 +00:00
} CWAVChannelData;
2015-01-25 18:32:01 +00:00
typedef struct {
char magic[4] = {'D', 'A', 'T', 'A'};
u32 length;
2015-01-26 03:04:27 +00:00
} CWAVDataHeader;
2015-01-25 18:32:01 +00:00
2015-01-26 03:04:27 +00:00
u8* cwav_build(CWAV cwav, u32* size) {
CWAVHeader header;
u32 offset = sizeof(CWAVHeader);
header.infoChunkOffset = offset;
2015-01-26 03:04:27 +00:00
header.infoChunkLength = sizeof(CWAVInfoHeader);
offset += header.infoChunkLength;
2015-01-26 03:04:27 +00:00
offset += (sizeof(CWAVChannelDataPointer) + sizeof(CWAVChannelData)) * cwav.channels;
header.dataChunkOffset = offset;
2015-01-26 03:04:27 +00:00
header.dataChunkLength = (u32) sizeof(CWAVDataHeader) + cwav.dataSize;
offset += header.dataChunkLength;
header.fileSize = offset;
u8* output = (u8*) malloc(offset);
u32 pos = 0;
2015-01-26 03:04:27 +00:00
memcpy(output + pos, &header, sizeof(CWAVHeader));
pos += sizeof(CWAVHeader);
u32 bytesPerSample = (u32) cwav.bitsPerSample / 8;
CWAVInfoHeader infoHeader;
infoHeader.type = cwav.bitsPerSample == 16 ? PCM16 : PCM8;
infoHeader.sampleRate = cwav.sampleRate;
infoHeader.totalSamples = cwav.dataSize / bytesPerSample / cwav.channels;
infoHeader.totalChannels = cwav.channels;
memcpy(output + pos, &infoHeader, sizeof(CWAVInfoHeader));
pos += sizeof(CWAVInfoHeader);
for(int i = 0; i < cwav.channels; i++) {
CWAVChannelDataPointer pointer;
pointer.offset = 0x4 + (cwav.channels * (u32) sizeof(CWAVChannelDataPointer)) + (i * (u32) sizeof(CWAVChannelData));
memcpy(output + pos, &pointer, sizeof(CWAVChannelDataPointer));
pos += sizeof(CWAVChannelDataPointer);
}
2015-01-26 03:04:27 +00:00
u32 bytesPerChannel = cwav.dataSize / cwav.channels;
for(int i = 0; i < cwav.channels; i++) {
CWAVChannelData data;
data.offset = i * bytesPerChannel;
memcpy(output + pos, &data, sizeof(CWAVChannelData));
pos += sizeof(CWAVChannelData);
}
2015-01-26 03:04:27 +00:00
CWAVDataHeader dataHeader;
dataHeader.length = (u32) sizeof(CWAVDataHeader) + cwav.dataSize;
memcpy(output + pos, &dataHeader, sizeof(CWAVDataHeader));
pos += sizeof(CWAVDataHeader);
2015-01-26 03:04:27 +00:00
for(int i = 0; i < cwav.dataSize; i += cwav.channels * bytesPerSample) {
for(int c = 0; c < cwav.channels; c++) {
memcpy(output + pos + (bytesPerChannel * c) + (i / cwav.channels), cwav.data + i + (c * bytesPerSample), bytesPerSample);
}
}
2015-01-26 03:04:27 +00:00
pos += cwav.dataSize;
if(size != NULL) {
*size = pos;
}
return output;
}