Last active
January 6, 2025 16:08
-
-
Save ommos61/1acc2665af2214f25c8d008b5948aab2 to your computer and use it in GitHub Desktop.
Simple example of how to use basic functions of minimp3 header only library to decode an mp3 file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#define MINIMP3_ONLY_MP3 | |
#define MINIMP3_NO_SIMD | |
#define MINIMP3_IMPLEMENTATION | |
#include "minimp3.h" | |
void readFile(const char *filename, uint64_t *size, uint8_t **data) { | |
*size = 0; | |
*data = NULL; | |
FILE *fin = fopen(filename, "rb"); | |
if (fin != NULL) { | |
fseek(fin, 0, SEEK_END); | |
*size = ftell(fin); | |
fseek(fin, 0, SEEK_SET); | |
*data = malloc(*size); | |
if (*data != NULL) { | |
uint64_t nread = fread(*data, 1, *size, fin); | |
if (nread != *size) { | |
*size = 0; | |
*data = NULL; | |
} | |
fclose(fin); | |
} else { | |
*size = 0; | |
} | |
} else { | |
perror("fopen"); | |
} | |
} | |
int main() { | |
static mp3dec_t mp3d; | |
mp3dec_init(&mp3d); | |
uint64_t buf_size = 0; | |
uint8_t *input_buf = NULL; | |
readFile("test1.mp3", &buf_size, &input_buf); | |
while (buf_size != 0) { | |
/*typedef struct | |
{ | |
int frame_bytes; | |
int channels; | |
int hz; | |
int layer; | |
int bitrate_kbps; | |
} mp3dec_frame_info_t;*/ | |
mp3dec_frame_info_t info; | |
short pcm[MINIMP3_MAX_SAMPLES_PER_FRAME]; | |
/*unsigned char *input_buf; - input byte stream*/ | |
int samples = mp3dec_decode_frame(&mp3d, input_buf, buf_size, pcm, &info); | |
printf("samples = %d\n", samples); | |
buf_size -= info.frame_bytes; | |
input_buf += info.frame_bytes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an example of how to use the minimp3 header only library, as the original repo doesn't seem to show a complete simple example to use it.