Initial MP3 refactor
Using mpg123_outblock doesn't work correctly. Also assigning variables to different types. Signed-off-by: Mahyar Koshkouei <deltabeard@users.noreply.github.com>
This commit is contained in:
102
source/mp3.c
102
source/mp3.c
@@ -6,6 +6,108 @@
|
||||
#include "all.h"
|
||||
#include "mp3.h"
|
||||
|
||||
static int buffSize = 64 * 1024;
|
||||
static mpg123_handle *mh = NULL;
|
||||
static uint32_t rate = 0;
|
||||
static uint8_t channels = 0;
|
||||
|
||||
/**
|
||||
* Set decoder parameters for MP3.
|
||||
*
|
||||
* \param decoder Structure to store parameters.
|
||||
*/
|
||||
void setMp3(struct decoder_fn* decoder)
|
||||
{
|
||||
decoder->init = &initMp3;
|
||||
decoder->rate = &rateMp3;
|
||||
decoder->channels = &channelMp3;
|
||||
decoder->buffSize = buffSize;
|
||||
decoder->decode = &decodeMp3;
|
||||
decoder->exit = &exitMp3;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise MP3 decoder.
|
||||
*
|
||||
* \param file Location of MP3 file to play.
|
||||
* \return 0 on success, else failure.
|
||||
*/
|
||||
int initMp3(const char* file)
|
||||
{
|
||||
int err = 0;
|
||||
int encoding = 0;
|
||||
|
||||
if((err = mpg123_init()) != MPG123_OK)
|
||||
return err;
|
||||
|
||||
if((mh = mpg123_new(NULL, &err)) == NULL)
|
||||
{
|
||||
//printf("Error: %s\n", mpg123_plain_strerror(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
if(mpg123_open(mh, file) != MPG123_OK ||
|
||||
mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK)
|
||||
{
|
||||
//printf("Trouble with mpg123: %s\n", mpg123_strerror(mh));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 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. */
|
||||
//buffSize = mpg123_outblock(mh) * 16;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sampling rate of MP3 file.
|
||||
*
|
||||
* \return Sampling rate.
|
||||
*/
|
||||
uint32_t rateMp3(void)
|
||||
{
|
||||
return rate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of channels of MP3 file.
|
||||
*
|
||||
* \return Number of channels for opened file.
|
||||
*/
|
||||
uint8_t channelMp3(void)
|
||||
{
|
||||
return channels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode part of open MP3 file.
|
||||
*
|
||||
* \param buffer Decoded output.
|
||||
* \return Samples read for each channel.
|
||||
*/
|
||||
uint64_t decodeMp3(void* buffer)
|
||||
{
|
||||
int done = 0;
|
||||
mpg123_read(mh, buffer, buffSize, &done);
|
||||
return done / (sizeof(int16_t));
|
||||
}
|
||||
|
||||
/**
|
||||
* Free MP3 decoder.
|
||||
*/
|
||||
void exitMp3(void)
|
||||
{
|
||||
mpg123_close(mh);
|
||||
mpg123_delete(mh);
|
||||
mpg123_exit();
|
||||
}
|
||||
|
||||
int playMp3(const char* in)
|
||||
{
|
||||
int err = 0;
|
||||
|
||||
42
source/mp3.h
42
source/mp3.h
@@ -1,3 +1,45 @@
|
||||
#include <mpg123.h>
|
||||
|
||||
/**
|
||||
* Set decoder parameters for MP3.
|
||||
*
|
||||
* \param decoder Structure to store parameters.
|
||||
*/
|
||||
void setMp3(struct decoder_fn* decoder);
|
||||
|
||||
/**
|
||||
* Initialise MP3 decoder.
|
||||
*
|
||||
* \param file Location of MP3 file to play.
|
||||
* \return 0 on success, else failure.
|
||||
*/
|
||||
int initMp3(const char* file);
|
||||
|
||||
/**
|
||||
* Get sampling rate of MP3 file.
|
||||
*
|
||||
* \return Sampling rate.
|
||||
*/
|
||||
uint32_t rateMp3(void);
|
||||
|
||||
/**
|
||||
* Get number of channels of MP3 file.
|
||||
*
|
||||
* \return Number of channels for opened file.
|
||||
*/
|
||||
uint8_t channelMp3(void);
|
||||
|
||||
/**
|
||||
* Decode part of open MP3 file.
|
||||
*
|
||||
* \param buffer Decoded output.
|
||||
* \return Samples read for each channel.
|
||||
*/
|
||||
uint64_t decodeMp3(void* buffer);
|
||||
|
||||
/**
|
||||
* Free MP3 decoder.
|
||||
*/
|
||||
void exitMp3(void);
|
||||
|
||||
int playMp3(const char* in);
|
||||
|
||||
@@ -36,8 +36,10 @@ int playFile(const char* file)
|
||||
break;
|
||||
|
||||
case FILE_TYPE_MP3:
|
||||
playMp3(file);
|
||||
return 0;
|
||||
setMp3(&decoder);
|
||||
break;
|
||||
//playMp3(file);
|
||||
//return 0;
|
||||
|
||||
default:
|
||||
printf("Unsupported File type.\n");
|
||||
|
||||
Reference in New Issue
Block a user