Use dynamic buffer size for mp3 again
Additionally removed the old MP3 code, saving a massive 1KB from the binary! Signed-off-by: Mahyar Koshkouei <deltabeard@users.noreply.github.com>
This commit is contained in:
181
source/mp3.c
181
source/mp3.c
@@ -1,4 +1,5 @@
|
|||||||
#include <3ds.h>
|
#include <3ds.h>
|
||||||
|
#include <mpg123.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -6,10 +7,10 @@
|
|||||||
#include "all.h"
|
#include "all.h"
|
||||||
#include "mp3.h"
|
#include "mp3.h"
|
||||||
|
|
||||||
static int buffSize = 64 * 1024;
|
static int* buffSize;
|
||||||
static mpg123_handle *mh = NULL;
|
static mpg123_handle *mh = NULL;
|
||||||
static uint32_t rate = 0;
|
static uint32_t rate;
|
||||||
static uint8_t channels = 0;
|
static uint8_t channels;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set decoder parameters for MP3.
|
* Set decoder parameters for MP3.
|
||||||
@@ -21,7 +22,11 @@ void setMp3(struct decoder_fn* decoder)
|
|||||||
decoder->init = &initMp3;
|
decoder->init = &initMp3;
|
||||||
decoder->rate = &rateMp3;
|
decoder->rate = &rateMp3;
|
||||||
decoder->channels = &channelMp3;
|
decoder->channels = &channelMp3;
|
||||||
decoder->buffSize = buffSize;
|
/*
|
||||||
|
* buffSize changes depending on input file. So we set buffSize later when
|
||||||
|
* decoder is initialised.
|
||||||
|
*/
|
||||||
|
buffSize = &(decoder->buffSize);
|
||||||
decoder->decode = &decodeMp3;
|
decoder->decode = &decodeMp3;
|
||||||
decoder->exit = &exitMp3;
|
decoder->exit = &exitMp3;
|
||||||
}
|
}
|
||||||
@@ -53,14 +58,18 @@ int initMp3(const char* file)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Ensure that this output format will not change
|
/*
|
||||||
(it might, when we allow it). */
|
* Ensure that this output format will not change (it might, when we allow
|
||||||
|
* it).
|
||||||
|
*/
|
||||||
mpg123_format_none(mh);
|
mpg123_format_none(mh);
|
||||||
mpg123_format(mh, rate, channels, encoding);
|
mpg123_format(mh, rate, channels, encoding);
|
||||||
|
|
||||||
/* Buffer could be almost any size here, mpg123_outblock() is just some
|
/*
|
||||||
recommendation. The size should be a multiple of the PCM frame size. */
|
* Buffer could be almost any size here, mpg123_outblock() is just some
|
||||||
//buffSize = mpg123_outblock(mh) * 16;
|
* recommendation. The size should be a multiple of the PCM frame size.
|
||||||
|
*/
|
||||||
|
*buffSize = mpg123_outblock(mh) * 16;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -94,7 +103,7 @@ uint8_t channelMp3(void)
|
|||||||
uint64_t decodeMp3(void* buffer)
|
uint64_t decodeMp3(void* buffer)
|
||||||
{
|
{
|
||||||
int done = 0;
|
int done = 0;
|
||||||
mpg123_read(mh, buffer, buffSize, &done);
|
mpg123_read(mh, buffer, *buffSize, &done);
|
||||||
return done / (sizeof(int16_t));
|
return done / (sizeof(int16_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,155 +116,3 @@ void exitMp3(void)
|
|||||||
mpg123_delete(mh);
|
mpg123_delete(mh);
|
||||||
mpg123_exit();
|
mpg123_exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
int playMp3(const char* in)
|
|
||||||
{
|
|
||||||
int err = 0;
|
|
||||||
mpg123_handle *mh = NULL;
|
|
||||||
size_t buffer_size = 0;
|
|
||||||
size_t done = 0;
|
|
||||||
long rate = 0;
|
|
||||||
int channels = 0;
|
|
||||||
int encoding = 0;
|
|
||||||
unsigned char* buffer1 = NULL;
|
|
||||||
unsigned char* buffer2 = NULL;
|
|
||||||
int ret = 0;
|
|
||||||
ndspWaveBuf waveBuf[2];
|
|
||||||
bool playing = true;
|
|
||||||
bool lastbuf = false;
|
|
||||||
|
|
||||||
if(R_FAILED(ndspInit()))
|
|
||||||
{
|
|
||||||
printf("Initialising ndsp failed.");
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
if((err = mpg123_init()) != MPG123_OK)
|
|
||||||
return err;
|
|
||||||
|
|
||||||
if((mh = mpg123_new(NULL, &err)) == NULL)
|
|
||||||
{
|
|
||||||
printf("Error: %s\n", mpg123_plain_strerror(err));
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(mpg123_open(mh, in) != MPG123_OK ||
|
|
||||||
mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK)
|
|
||||||
{
|
|
||||||
printf("Trouble with mpg123: %s\n", mpg123_strerror(mh));
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Ensure that this output format will not change
|
|
||||||
(it might, when we allow it). */
|
|
||||||
mpg123_format_none(mh);
|
|
||||||
mpg123_format(mh, rate, channels, encoding);
|
|
||||||
|
|
||||||
/* Buffer could be almost any size here, mpg123_outblock() is just some
|
|
||||||
recommendation. The size should be a multiple of the PCM frame size. */
|
|
||||||
buffer_size = mpg123_outblock(mh) * 16;
|
|
||||||
buffer1 = linearAlloc(buffer_size);
|
|
||||||
buffer2 = linearAlloc(buffer_size);
|
|
||||||
|
|
||||||
/* I'm not sure if this error will ever occur. */
|
|
||||||
if(channels > 2)
|
|
||||||
{
|
|
||||||
printf("Invalid number of channels %d\n", channels);
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
ndspChnReset(CHANNEL);
|
|
||||||
ndspChnWaveBufClear(CHANNEL);
|
|
||||||
ndspSetOutputMode(NDSP_OUTPUT_STEREO);
|
|
||||||
ndspChnSetInterp(CHANNEL, NDSP_INTERP_POLYPHASE);
|
|
||||||
ndspChnSetRate(CHANNEL, rate);
|
|
||||||
ndspChnSetFormat(CHANNEL,
|
|
||||||
channels == 2 ? NDSP_FORMAT_STEREO_PCM16 : NDSP_FORMAT_MONO_PCM16);
|
|
||||||
|
|
||||||
memset(waveBuf, 0, sizeof(waveBuf));
|
|
||||||
|
|
||||||
mpg123_read(mh, buffer1, buffer_size, &done);
|
|
||||||
waveBuf[0].nsamples = done / (sizeof(s16) * channels);
|
|
||||||
waveBuf[0].data_vaddr = &buffer1[0];
|
|
||||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
|
||||||
|
|
||||||
mpg123_read(mh, buffer2, buffer_size, &done);
|
|
||||||
waveBuf[1].nsamples = done / (sizeof(s16) * channels);
|
|
||||||
waveBuf[1].data_vaddr = &buffer2[0];
|
|
||||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
|
||||||
|
|
||||||
printf("Playing %s\n", in);
|
|
||||||
/**
|
|
||||||
* There may be a chance that the music has not started by the time we get
|
|
||||||
* to the while loop. So we ensure that music has started here.
|
|
||||||
*/
|
|
||||||
while(ndspChnIsPlaying(CHANNEL) == false);
|
|
||||||
|
|
||||||
while(playing == false || ndspChnIsPlaying(CHANNEL) == true)
|
|
||||||
{
|
|
||||||
u32 kDown;
|
|
||||||
|
|
||||||
gfxSwapBuffers();
|
|
||||||
gfxFlushBuffers();
|
|
||||||
gspWaitForVBlank();
|
|
||||||
|
|
||||||
hidScanInput();
|
|
||||||
kDown = hidKeysDown();
|
|
||||||
|
|
||||||
if(kDown & KEY_B)
|
|
||||||
break;
|
|
||||||
|
|
||||||
if(kDown & (KEY_A | KEY_R))
|
|
||||||
{
|
|
||||||
playing = !playing;
|
|
||||||
printf("\33[2K\r%s", playing == false ? "Paused" : "");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(playing == false || lastbuf == true)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if(waveBuf[0].status == NDSP_WBUF_DONE)
|
|
||||||
{
|
|
||||||
err = mpg123_read(mh, buffer1, buffer_size, &done);
|
|
||||||
|
|
||||||
if(err != MPG123_OK)
|
|
||||||
{
|
|
||||||
lastbuf = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(waveBuf[1].status == NDSP_WBUF_DONE)
|
|
||||||
{
|
|
||||||
err = mpg123_read(mh, buffer2, buffer_size, &done);
|
|
||||||
|
|
||||||
if(err != MPG123_OK)
|
|
||||||
{
|
|
||||||
lastbuf = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ndspChnWaveBufAdd(CHANNEL, &waveBuf[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
DSP_FlushDataCache(buffer1, buffer_size);
|
|
||||||
DSP_FlushDataCache(buffer2, buffer_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
out:
|
|
||||||
linearFree(buffer1);
|
|
||||||
linearFree(buffer2);
|
|
||||||
ndspChnWaveBufClear(CHANNEL);
|
|
||||||
ndspExit();
|
|
||||||
mpg123_close(mh);
|
|
||||||
mpg123_delete(mh);
|
|
||||||
mpg123_exit();
|
|
||||||
printf("\nStopping MP3 playback.\n");
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
err:
|
|
||||||
ret = -1;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
#include <mpg123.h>
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set decoder parameters for MP3.
|
* Set decoder parameters for MP3.
|
||||||
*
|
*
|
||||||
@@ -41,5 +39,3 @@ uint64_t decodeMp3(void* buffer);
|
|||||||
* Free MP3 decoder.
|
* Free MP3 decoder.
|
||||||
*/
|
*/
|
||||||
void exitMp3(void);
|
void exitMp3(void);
|
||||||
|
|
||||||
int playMp3(const char* in);
|
|
||||||
|
|||||||
@@ -38,8 +38,6 @@ int playFile(const char* file)
|
|||||||
case FILE_TYPE_MP3:
|
case FILE_TYPE_MP3:
|
||||||
setMp3(&decoder);
|
setMp3(&decoder);
|
||||||
break;
|
break;
|
||||||
//playMp3(file);
|
|
||||||
//return 0;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
printf("Unsupported File type.\n");
|
printf("Unsupported File type.\n");
|
||||||
@@ -52,10 +50,6 @@ int playFile(const char* file)
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Here: %d\n", __LINE__);
|
|
||||||
buffer1 = linearAlloc(decoder.buffSize * sizeof(int16_t));
|
|
||||||
buffer2 = linearAlloc(decoder.buffSize * sizeof(int16_t));
|
|
||||||
|
|
||||||
printf("Here: %d\n", __LINE__);
|
printf("Here: %d\n", __LINE__);
|
||||||
if((ret = (*decoder.init)(file)) != 0)
|
if((ret = (*decoder.init)(file)) != 0)
|
||||||
{
|
{
|
||||||
@@ -64,6 +58,10 @@ int playFile(const char* file)
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("Here: %d\n", __LINE__);
|
printf("Here: %d\n", __LINE__);
|
||||||
|
buffer1 = linearAlloc(decoder.buffSize * sizeof(int16_t));
|
||||||
|
buffer2 = linearAlloc(decoder.buffSize * sizeof(int16_t));
|
||||||
|
|
||||||
|
printf("Here: %d\n", __LINE__);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("\nRate: %lu\tChan: %d\n", (*decoder.rate)(), (*decoder.channels)());
|
printf("\nRate: %lu\tChan: %d\n", (*decoder.rate)(), (*decoder.channels)());
|
||||||
|
|||||||
Reference in New Issue
Block a user