Add MP3 Support
Signed-off-by: Mahyar Koshkouei <deltabeard@users.noreply.github.com>
This commit is contained in:
2
Makefile
2
Makefile
@@ -49,7 +49,7 @@ SOURCE_DIRS := source
|
|||||||
EXTRA_OUTPUT_FILES :=
|
EXTRA_OUTPUT_FILES :=
|
||||||
|
|
||||||
LIBRARY_DIRS := $(DEVKITPRO)/libctru $(DEVKITPRO)/portlibs/armv6k
|
LIBRARY_DIRS := $(DEVKITPRO)/libctru $(DEVKITPRO)/portlibs/armv6k
|
||||||
LIBRARIES := opusfile opus ogg ctru m
|
LIBRARIES := mpg123 opusfile opus ogg ctru m
|
||||||
|
|
||||||
BUILD_FLAGS :=
|
BUILD_FLAGS :=
|
||||||
RUN_FLAGS :=
|
RUN_FLAGS :=
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#define CHANNEL 0x08
|
#define CHANNEL 0x08
|
||||||
|
|
||||||
/* Adds extra debugging text */
|
/* Adds extra debugging text */
|
||||||
#define DEBUG 0
|
#define DEBUG
|
||||||
|
|
||||||
/* Prints more error information */
|
/* Prints more error information */
|
||||||
#define err_print(err) \
|
#define err_print(err) \
|
||||||
|
|||||||
@@ -25,8 +25,10 @@ int playFlac(const char* in)
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
printf("\nRate: %lu\tChan: %d\n", pFlac->sampleRate,
|
printf("\nRate: %lu\tChan: %d\n", pFlac->sampleRate,
|
||||||
pFlac->channels);
|
pFlac->channels);
|
||||||
|
#endif
|
||||||
|
|
||||||
ndspChnReset(CHANNEL);
|
ndspChnReset(CHANNEL);
|
||||||
ndspChnWaveBufClear(CHANNEL);
|
ndspChnWaveBufClear(CHANNEL);
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
#include "all.h"
|
#include "all.h"
|
||||||
#include "flac.h"
|
#include "flac.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
#include "mp3.h"
|
||||||
#include "opus.h"
|
#include "opus.h"
|
||||||
#include "wav.h"
|
#include "wav.h"
|
||||||
|
|
||||||
@@ -31,7 +32,8 @@ enum file_types {
|
|||||||
FILE_TYPE_WAV,
|
FILE_TYPE_WAV,
|
||||||
FILE_TYPE_FLAC,
|
FILE_TYPE_FLAC,
|
||||||
FILE_TYPE_OGG,
|
FILE_TYPE_OGG,
|
||||||
FILE_TYPE_OPUS
|
FILE_TYPE_OPUS,
|
||||||
|
FILE_TYPE_MP3
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
@@ -85,9 +87,11 @@ int main(int argc, char **argv)
|
|||||||
if(kDown & KEY_START)
|
if(kDown & KEY_START)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
consoleSelect(&topScreen);
|
consoleSelect(&topScreen);
|
||||||
printf("\rNum: %d, Max: %d, from: %d ", fileNum, fileMax, from);
|
printf("\rNum: %d, Max: %d, from: %d ", fileNum, fileMax, from);
|
||||||
consoleSelect(&bottomScreen);
|
consoleSelect(&bottomScreen);
|
||||||
|
#endif
|
||||||
|
|
||||||
if((kHeld & KEY_UP) && fileNum > 0)
|
if((kHeld & KEY_UP) && fileNum > 0)
|
||||||
{
|
{
|
||||||
@@ -211,6 +215,10 @@ int main(int argc, char **argv)
|
|||||||
playOpus(file);
|
playOpus(file);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case FILE_TYPE_MP3:
|
||||||
|
playMp3(file);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
consoleSelect(&bottomScreen);
|
consoleSelect(&bottomScreen);
|
||||||
printf("Unsupported File type.\n");
|
printf("Unsupported File type.\n");
|
||||||
@@ -413,6 +421,20 @@ int getFileType(const char *file)
|
|||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
/*
|
||||||
|
* MP3 without ID3 tag, ID3v1 tag is at the end of file, or MP3
|
||||||
|
* with ID3 tag at the beginning of the file.
|
||||||
|
*/
|
||||||
|
if((fileSig << 16) == 0xFBFF0000 || (fileSig << 8) == 0x33444900)
|
||||||
|
{
|
||||||
|
puts("File type is MP3.");
|
||||||
|
file_type = FILE_TYPE_MP3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Unknown magic number: %#010x\n.", fileSig);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(ftest);
|
fclose(ftest);
|
||||||
|
|||||||
159
source/mp3.c
Normal file
159
source/mp3.c
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
#include <3ds.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "all.h"
|
||||||
|
#include "mp3.h"
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
3
source/mp3.h
Normal file
3
source/mp3.h
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#include <mpg123.h>
|
||||||
|
|
||||||
|
int playMp3(const char* in);
|
||||||
@@ -38,8 +38,10 @@ int playOpus(const char* in)
|
|||||||
}
|
}
|
||||||
|
|
||||||
opusHead = op_head(opusFile, link);
|
opusHead = op_head(opusFile, link);
|
||||||
|
#ifdef DEBUG
|
||||||
printf("\nRate: %lu\tChan: %d\n", opusHead->input_sample_rate,
|
printf("\nRate: %lu\tChan: %d\n", opusHead->input_sample_rate,
|
||||||
opusHead->channel_count);
|
opusHead->channel_count);
|
||||||
|
#endif
|
||||||
|
|
||||||
ndspChnReset(CHANNEL);
|
ndspChnReset(CHANNEL);
|
||||||
ndspChnWaveBufClear(CHANNEL);
|
ndspChnWaveBufClear(CHANNEL);
|
||||||
|
|||||||
Reference in New Issue
Block a user