mirror of
https://github.com/parasyte/alt64
synced 2025-02-28 09:21:46 -05:00
Fix some whitespace horrors!
This commit is contained in:
parent
174d9d96e6
commit
050db8b445
412
mp3.c
412
mp3.c
@ -14,8 +14,8 @@ static struct mad_synth Synth;
|
||||
static mad_timer_t Timer;
|
||||
|
||||
typedef struct {
|
||||
short left;
|
||||
short right;
|
||||
short left;
|
||||
short right;
|
||||
} Sample;
|
||||
|
||||
static int eos;
|
||||
@ -39,88 +39,71 @@ extern char path[1024];
|
||||
extern void c2wstrcpy(void *dst, void *src);
|
||||
extern void c2wstrcat(void *dst, void *src);
|
||||
|
||||
static int mp3_seek(char* fd, int offset, int whence) {
|
||||
//todo filesize and mp3File_fptr;
|
||||
long offs = 0;
|
||||
// libff routine
|
||||
switch (whence)
|
||||
{
|
||||
case SEEK_SET:
|
||||
offs = offset;
|
||||
break;
|
||||
case SEEK_CUR:
|
||||
offs = mp3File_fptr + offset;
|
||||
break;
|
||||
case SEEK_END:
|
||||
offs = mp3File_fsize + offset;
|
||||
break;
|
||||
}
|
||||
//f_lseek(&mp3File, offs);
|
||||
mp3File_fptr=offs;
|
||||
|
||||
static int mp3_seek(char* fd, int offset, int whence)
|
||||
{
|
||||
|
||||
//todo filesize and mp3File_fptr;
|
||||
|
||||
|
||||
long offs = 0;
|
||||
// libff routine
|
||||
switch (whence)
|
||||
{
|
||||
case SEEK_SET:
|
||||
offs = offset;
|
||||
break;
|
||||
case SEEK_CUR:
|
||||
offs = mp3File_fptr + offset;
|
||||
break;
|
||||
case SEEK_END:
|
||||
offs = mp3File_fsize + offset;
|
||||
break;
|
||||
}
|
||||
//f_lseek(&mp3File, offs);
|
||||
mp3File_fptr=offs;
|
||||
|
||||
return offs;
|
||||
|
||||
return offs;
|
||||
}
|
||||
|
||||
static int mp3_size(char* fd)
|
||||
{
|
||||
FatRecord rec_tmpf;
|
||||
u8 resp=0;
|
||||
resp = fatOpenFileByeName(fd, 0); //err if not found ^^
|
||||
|
||||
int fsize = file.sec_available*512; //fsize in bytes
|
||||
mp3File_fsize = fsize;
|
||||
//todo filesize
|
||||
return mp3File_fsize;
|
||||
static int mp3_size(char* fd) {
|
||||
FatRecord rec_tmpf;
|
||||
u8 resp=0;
|
||||
resp = fatOpenFileByeName(fd, 0); //err if not found ^^
|
||||
|
||||
int fsize = file.sec_available*512; //fsize in bytes
|
||||
mp3File_fsize = fsize;
|
||||
//todo filesize
|
||||
return mp3File_fsize;
|
||||
}
|
||||
|
||||
static void _f_read(char* fname, unsigned char *readBuffer, int size){
|
||||
|
||||
|
||||
/*
|
||||
FatRecord rec_tmpf;
|
||||
u8 resp=0;
|
||||
resp = fatOpenFileByeName(fname, 0); //err if not found ^^
|
||||
FatRecord rec_tmpf;
|
||||
u8 resp=0;
|
||||
resp = fatOpenFileByeName(fname, 0); //err if not found ^^
|
||||
|
||||
int fsize = file.sec_available*512; //fsize in bytes
|
||||
mp3File_fsize = fsize;
|
||||
|
||||
int fsize = file.sec_available*512; //fsize in bytes
|
||||
mp3File_fsize = fsize;
|
||||
|
||||
//injecting in buffer... slow but working :/
|
||||
if(file.sec_available*512>=size){
|
||||
resp = fatReadPartialFile(readBuffer, size/512, mp3File_fptr);
|
||||
//resp = fatReadFile(readBuffer+mp3File_fptr, size/512);//file.sec_available);
|
||||
mp3File_fptr+=size;
|
||||
|
||||
//injecting in buffer... slow but working :/
|
||||
if(file.sec_available*512>=size){
|
||||
resp = fatReadPartialFile(readBuffer, size/512, mp3File_fptr);
|
||||
//resp = fatReadFile(readBuffer+mp3File_fptr, size/512);//file.sec_available);
|
||||
mp3File_fptr+=size;
|
||||
|
||||
}
|
||||
//dma_write_s(buffer, 0xb0000000, fsize);
|
||||
|
||||
|
||||
}
|
||||
//dma_write_s(buffer, 0xb0000000, fsize);
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
static int mp3_read(char* fd, unsigned char *ptr, int size)
|
||||
{
|
||||
int ts=size;
|
||||
_f_read(fd, ptr, size);
|
||||
return ts;
|
||||
|
||||
int ts=size;
|
||||
_f_read(fd, ptr, size);
|
||||
return ts;
|
||||
}
|
||||
|
||||
static int id3_tag_size(unsigned char const *buf, int remaining)
|
||||
{
|
||||
int size;
|
||||
static int id3_tag_size(unsigned char const *buf, int remaining) {
|
||||
int size;
|
||||
|
||||
if (remaining < 10)
|
||||
return 0;
|
||||
if (remaining < 10)
|
||||
return 0;
|
||||
|
||||
if (!strncmp((char*)buf, "ID3", 3) || !strncmp((char*)buf, "ea3", 3)) //skip past id3v2 header, which can cause a false sync to be found
|
||||
{
|
||||
@ -135,7 +118,7 @@ static int id3_tag_size(unsigned char const *buf, int remaining)
|
||||
if (buf[5] & 0x10) //has footer
|
||||
size += 10;
|
||||
}
|
||||
return size;
|
||||
return size;
|
||||
}
|
||||
|
||||
//Seek next valid frame after ID3/EA3 header
|
||||
@ -164,13 +147,12 @@ static int MP3_SkipHdr(char* fd)
|
||||
if (buf[5] & 0x10) //has footer
|
||||
size += 10;
|
||||
|
||||
offset += size;
|
||||
offset += size;
|
||||
}
|
||||
mp3_seek(fd, offset, SEEK_SET);
|
||||
|
||||
//now seek for a sync
|
||||
while(1)
|
||||
{
|
||||
//now seek for a sync
|
||||
while(1) {
|
||||
offset = mp3_seek(fd, 0, SEEK_CUR);
|
||||
size = mp3_read(fd, buf, sizeof(buf));
|
||||
|
||||
@ -184,125 +166,108 @@ static int MP3_SkipHdr(char* fd)
|
||||
}
|
||||
|
||||
pBuffer = buf;
|
||||
for( i = 0; i < size; i++)
|
||||
{
|
||||
for( i = 0; i < size; i++) {
|
||||
//if this is a valid frame sync (0xe0 is for mpeg version 2.5,2+1)
|
||||
if ( (pBuffer[i] == 0xff) && ((pBuffer[i+1] & 0xE0) == 0xE0) )
|
||||
{
|
||||
if ( (pBuffer[i] == 0xff) && ((pBuffer[i+1] & 0xE0) == 0xE0) ) {
|
||||
offset += i;
|
||||
mp3_seek(fd, offset, SEEK_SET);
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
//go back two bytes to catch any syncs that on the boundary
|
||||
//go back two bytes to catch any syncs that on the boundary
|
||||
mp3_seek(fd, -2, SEEK_CUR);
|
||||
}
|
||||
}
|
||||
|
||||
static short convertSample(mad_fixed_t Fixed)
|
||||
{
|
||||
static short convertSample(mad_fixed_t Fixed) {
|
||||
/* Clipping */
|
||||
if (Fixed >= MAD_F_ONE)
|
||||
return (32767);
|
||||
return (32767);
|
||||
if (Fixed <= -MAD_F_ONE)
|
||||
return (-32768);
|
||||
return (-32768);
|
||||
|
||||
/* Conversion. */
|
||||
Fixed = Fixed >> (MAD_F_FRACBITS - 15);
|
||||
return ((short)Fixed);
|
||||
|
||||
return ((short)Fixed);
|
||||
}
|
||||
|
||||
static int fillFileBuffer()
|
||||
{
|
||||
int leftOver = Stream.bufend - Stream.next_frame;
|
||||
int want = INPUT_BUFFER_SIZE - leftOver;
|
||||
static int fillFileBuffer() {
|
||||
int leftOver = Stream.bufend - Stream.next_frame;
|
||||
int want = INPUT_BUFFER_SIZE - leftOver;
|
||||
|
||||
// move left-over bytes
|
||||
if (leftOver > 0)
|
||||
memmove(fileBuffer, fileBuffer + want, leftOver);
|
||||
// move left-over bytes
|
||||
if (leftOver > 0)
|
||||
memmove(fileBuffer, fileBuffer + want, leftOver);
|
||||
|
||||
// fill remainder of buffer
|
||||
unsigned char* bufferPos = fileBuffer + leftOver;
|
||||
while (want > 0)
|
||||
{
|
||||
int got = mp3_read(mp3Fd, bufferPos, want);
|
||||
if (got <= 0)
|
||||
return 1; // EOF
|
||||
// fill remainder of buffer
|
||||
unsigned char* bufferPos = fileBuffer + leftOver;
|
||||
while (want > 0) {
|
||||
int got = mp3_read(mp3Fd, bufferPos, want);
|
||||
if (got <= 0)
|
||||
return 1; // EOF
|
||||
|
||||
want -= got;
|
||||
bufferPos += got;
|
||||
}
|
||||
return 0;
|
||||
want -= got;
|
||||
bufferPos += got;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void decode()
|
||||
{
|
||||
while (mad_frame_decode(&Frame, &Stream) == -1)
|
||||
{
|
||||
if ((Stream.error == MAD_ERROR_BUFLEN) || (Stream.error == MAD_ERROR_BUFPTR))
|
||||
{
|
||||
if (fillFileBuffer())
|
||||
{
|
||||
eos = 1;
|
||||
break;
|
||||
}
|
||||
mad_stream_buffer(&Stream, fileBuffer, INPUT_BUFFER_SIZE);
|
||||
}
|
||||
else if (Stream.error == MAD_ERROR_LOSTSYNC)
|
||||
{
|
||||
/* LOSTSYNC - due to ID3 tags? */
|
||||
static void decode() {
|
||||
while (mad_frame_decode(&Frame, &Stream) == -1) {
|
||||
if ((Stream.error == MAD_ERROR_BUFLEN) || (Stream.error == MAD_ERROR_BUFPTR)) {
|
||||
if (fillFileBuffer()) {
|
||||
eos = 1;
|
||||
break;
|
||||
}
|
||||
mad_stream_buffer(&Stream, fileBuffer, INPUT_BUFFER_SIZE);
|
||||
}
|
||||
else if (Stream.error == MAD_ERROR_LOSTSYNC) {
|
||||
/* LOSTSYNC - due to ID3 tags? */
|
||||
int tagsize = id3_tag_size(Stream.this_frame, Stream.bufend - Stream.this_frame);
|
||||
if (tagsize > 0)
|
||||
{
|
||||
mad_stream_skip (&Stream, tagsize);
|
||||
if (tagsize > 0) {
|
||||
mad_stream_skip (&Stream, tagsize);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mad_timer_add(&Timer, Frame.header.duration);
|
||||
mad_synth_frame(&Synth, &Frame);
|
||||
mad_synth_frame(&Synth, &Frame);
|
||||
}
|
||||
|
||||
static void convertLeftSamples(Sample* first, Sample* last, const mad_fixed_t* src)
|
||||
{
|
||||
for (Sample *dst = first; dst != last; ++dst)
|
||||
dst->left = convertSample(*src++);
|
||||
static void convertLeftSamples(Sample* first, Sample* last, const mad_fixed_t* src) {
|
||||
for (Sample *dst = first; dst != last; ++dst)
|
||||
dst->left = convertSample(*src++);
|
||||
}
|
||||
|
||||
static void convertRightSamples(Sample* first, Sample* last, const mad_fixed_t* src)
|
||||
{
|
||||
for (Sample *dst = first; dst != last; ++dst)
|
||||
dst->right = convertSample(*src++);
|
||||
static void convertRightSamples(Sample* first, Sample* last, const mad_fixed_t* src) {
|
||||
for (Sample *dst = first; dst != last; ++dst)
|
||||
dst->right = convertSample(*src++);
|
||||
}
|
||||
|
||||
static void MP3_Callback(void *buffer, unsigned int samplesToWrite)
|
||||
{
|
||||
static void MP3_Callback(void *buffer, unsigned int samplesToWrite) {
|
||||
Sample *destination = (Sample*)buffer;
|
||||
|
||||
while (samplesToWrite > 0)
|
||||
{
|
||||
while (!eos && (Synth.pcm.length == 0))
|
||||
decode();
|
||||
while (samplesToWrite > 0) {
|
||||
while (!eos && (Synth.pcm.length == 0))
|
||||
decode();
|
||||
|
||||
if (eos)
|
||||
{
|
||||
// done
|
||||
memset(destination, 0, samplesToWrite*4);
|
||||
break;
|
||||
}
|
||||
if (eos) {
|
||||
// done
|
||||
memset(destination, 0, samplesToWrite*4);
|
||||
break;
|
||||
}
|
||||
|
||||
unsigned int samplesAvailable = Synth.pcm.length - samplesRead;
|
||||
if (samplesAvailable > samplesToWrite)
|
||||
{
|
||||
if (samplesAvailable > samplesToWrite) {
|
||||
convertLeftSamples(destination, destination + samplesToWrite, &Synth.pcm.samples[0][samplesRead]);
|
||||
convertRightSamples(destination, destination + samplesToWrite, &Synth.pcm.samples[1][samplesRead]);
|
||||
|
||||
samplesRead += samplesToWrite;
|
||||
samplesToWrite = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else {
|
||||
convertLeftSamples(destination, destination + samplesAvailable, &Synth.pcm.samples[0][samplesRead]);
|
||||
convertRightSamples(destination, destination + samplesAvailable, &Synth.pcm.samples[1][samplesRead]);
|
||||
|
||||
@ -310,137 +275,124 @@ static void MP3_Callback(void *buffer, unsigned int samplesToWrite)
|
||||
samplesToWrite -= samplesAvailable;
|
||||
|
||||
samplesRead = 0;
|
||||
decode();
|
||||
decode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void MP3_Init()
|
||||
{
|
||||
static void MP3_Init() {
|
||||
/* First the structures used by libmad must be initialized. */
|
||||
mad_stream_init(&Stream);
|
||||
mad_header_init(&Header);
|
||||
mad_header_init(&Header);
|
||||
mad_frame_init(&Frame);
|
||||
mad_synth_init(&Synth);
|
||||
mad_timer_reset(&Timer);
|
||||
}
|
||||
|
||||
static void MP3_Exit()
|
||||
{
|
||||
static void MP3_Exit() {
|
||||
mad_synth_finish(&Synth);
|
||||
mad_header_finish(&Header);
|
||||
mad_frame_finish(&Frame);
|
||||
mad_stream_finish(&Stream);
|
||||
}
|
||||
|
||||
static void MP3_GetInfo(long long *samples, int *rate)
|
||||
{
|
||||
unsigned long FrameCount = 0;
|
||||
static void MP3_GetInfo(long long *samples, int *rate) {
|
||||
unsigned long FrameCount = 0;
|
||||
int bufferSize = 1024*512;
|
||||
unsigned char *localBuffer;
|
||||
long red = 0;
|
||||
double totalBitrate = 0.0;
|
||||
double mediumBitrate = 0.0;
|
||||
struct mad_stream stream;
|
||||
struct mad_header header;
|
||||
int size = mp3_size(mp3Fd);
|
||||
long count = size;
|
||||
struct mad_stream stream;
|
||||
struct mad_header header;
|
||||
int size = mp3_size(mp3Fd);
|
||||
long count = size;
|
||||
|
||||
mad_stream_init (&stream);
|
||||
mad_header_init (&header);
|
||||
mad_stream_init (&stream);
|
||||
mad_header_init (&header);
|
||||
|
||||
localBuffer = (unsigned char *)malloc(bufferSize);
|
||||
|
||||
for (int i=0; i<3; i++)
|
||||
{
|
||||
for (int i=0; i<3; i++) {
|
||||
memset(localBuffer, 0, bufferSize);
|
||||
|
||||
if (count > bufferSize)
|
||||
red = mp3_read(mp3Fd, localBuffer, bufferSize);
|
||||
else
|
||||
red = mp3_read(mp3Fd, localBuffer, count);
|
||||
count -= red;
|
||||
if (!red)
|
||||
break; // ran out of data
|
||||
if (count > bufferSize)
|
||||
red = mp3_read(mp3Fd, localBuffer, bufferSize);
|
||||
else
|
||||
red = mp3_read(mp3Fd, localBuffer, count);
|
||||
count -= red;
|
||||
if (!red)
|
||||
break; // ran out of data
|
||||
|
||||
mad_stream_buffer (&stream, localBuffer, red);
|
||||
mad_stream_buffer (&stream, localBuffer, red);
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (mad_header_decode(&header, &stream) == -1)
|
||||
{
|
||||
if (stream.buffer == NULL || stream.error == MAD_ERROR_BUFLEN)
|
||||
while (1) {
|
||||
if (mad_header_decode(&header, &stream) == -1) {
|
||||
if (stream.buffer == NULL || stream.error == MAD_ERROR_BUFLEN) {
|
||||
break;
|
||||
else if (MAD_RECOVERABLE(stream.error))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (FrameCount++ == 0)
|
||||
*rate = header.samplerate;
|
||||
totalBitrate += header.bitrate;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (MAD_RECOVERABLE(stream.error)) {
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (FrameCount++ == 0)
|
||||
*rate = header.samplerate;
|
||||
totalBitrate += header.bitrate;
|
||||
}
|
||||
}
|
||||
|
||||
mediumBitrate = totalBitrate / (double)FrameCount;
|
||||
int secs = size * 8 / mediumBitrate;
|
||||
*samples = *rate * secs;
|
||||
*samples = *rate * secs;
|
||||
|
||||
mad_header_finish (&header);
|
||||
mad_stream_finish (&stream);
|
||||
mad_header_finish (&header);
|
||||
mad_stream_finish (&stream);
|
||||
|
||||
if (localBuffer)
|
||||
free(localBuffer);
|
||||
free(localBuffer);
|
||||
|
||||
mp3_seek(mp3Fd, 0, SEEK_SET);
|
||||
mp3_seek(mp3Fd, 0, SEEK_SET);
|
||||
}
|
||||
|
||||
|
||||
void start_mp3(char *fname, long long *samples, int *rate, int *channels)
|
||||
{
|
||||
void start_mp3(char *fname, long long *samples, int *rate, int *channels) {
|
||||
sprintf(mp3Fd, "%s", fname);
|
||||
|
||||
sprintf(mp3Fd, "%s", fname);
|
||||
|
||||
//if (mp3Fd[0]!=0)
|
||||
//{
|
||||
useReadBuffer = 0;
|
||||
MP3_GetInfo(samples, rate);
|
||||
*channels = 2;
|
||||
|
||||
//if (mp3Fd[0]!=0)
|
||||
//{
|
||||
useReadBuffer = 0;
|
||||
MP3_GetInfo(samples, rate);
|
||||
*channels = 2;
|
||||
MP3_Init();
|
||||
MP3_SkipHdr(mp3Fd);
|
||||
eos = readLen = readPos = 0;
|
||||
useReadBuffer = 1;
|
||||
return;
|
||||
//}
|
||||
|
||||
MP3_Init();
|
||||
MP3_SkipHdr(mp3Fd);
|
||||
eos = readLen = readPos = 0;
|
||||
useReadBuffer = 1;
|
||||
return;
|
||||
//}
|
||||
|
||||
//*samples = 0;
|
||||
//return;
|
||||
//*samples = 0;
|
||||
}
|
||||
|
||||
void stop_mp3(void)
|
||||
{
|
||||
MP3_Exit();
|
||||
mp3File_fptr=0;
|
||||
void stop_mp3(void) {
|
||||
MP3_Exit();
|
||||
mp3File_fptr=0;
|
||||
/*
|
||||
if (mp3Fd > 0)
|
||||
{
|
||||
if (gBrowser)
|
||||
f_close(&mp3File);
|
||||
else
|
||||
dfs_close(mp3Fd);
|
||||
}
|
||||
mp3Fd = -1;
|
||||
*/
|
||||
if (mp3Fd > 0)
|
||||
{
|
||||
if (gBrowser)
|
||||
f_close(&mp3File);
|
||||
else
|
||||
dfs_close(mp3Fd);
|
||||
}
|
||||
mp3Fd = -1;
|
||||
*/
|
||||
}
|
||||
|
||||
int update_mp3(char *buf, int bytes)
|
||||
{
|
||||
MP3_Callback(buf, bytes/4);
|
||||
return eos ? 0 : 1;
|
||||
int update_mp3(char *buf, int bytes) {
|
||||
MP3_Callback(buf, bytes/4);
|
||||
return eos ? 0 : 1;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user