Skip to content

Commit 65944b1

Browse files
pmprogSiegeLord
authored andcommitted
MiniMP3 support (Sample mode only for now)
1 parent 78ba7cc commit 65944b1

File tree

6 files changed

+188
-0
lines changed

6 files changed

+188
-0
lines changed

addons/acodec/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ option(WANT_TREMOR "Enable Ogg Vorbis support using Tremor" off)
99
option(WANT_OPUS "Enable Opus support using libopus" on)
1010
option(WANT_MODAUDIO "Enable MOD Audio support" on)
1111
option(WANT_ACODEC_DYNAMIC_LOAD "Enable DLL loading in acodec (Windows)" off)
12+
option(WANT_MP3 "Enable MP3 support" on)
1213

1314
#-----------------------------------------------------------------------------#
1415

@@ -332,6 +333,23 @@ if(SUPPORT_OPUS)
332333
endif()
333334
endif(SUPPORT_OPUS)
334335

336+
337+
#
338+
# MP3
339+
#
340+
if(WANT_MP3)
341+
find_package(MiniMP3)
342+
if(MINIMP3_FOUND)
343+
include_directories(SYSTEM ${MINIMP3_INCLUDE_DIRS})
344+
set(ALLEGRO_CFG_ACODEC_MP3 1)
345+
list(APPEND ACODEC_SOURCES mp3.c)
346+
endif(MINIMP3_FOUND)
347+
if(NOT MINIMP3_FOUND)
348+
message("WARNING: minimp3 was not found")
349+
endif(NOT MINIMP3_FOUND)
350+
endif()
351+
352+
335353
configure_file(
336354
allegro5/internal/aintern_acodec_cfg.h.cmake
337355
${PROJECT_BINARY_DIR}/include/allegro5/internal/aintern_acodec_cfg.h

addons/acodec/acodec.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ bool al_init_acodec_addon(void)
5454
ret &= al_register_audio_stream_loader_f(".opus", _al_load_ogg_opus_audio_stream_f);
5555
#endif
5656

57+
#ifdef ALLEGRO_CFG_ACODEC_MP3
58+
ret &= al_register_sample_loader(".mp3", _al_load_mp3);
59+
ret &= al_register_audio_stream_loader(".mp3", _al_load_mp3_audio_stream);
60+
ret &= al_register_sample_loader_f(".mp3", _al_load_mp3_f);
61+
ret &= al_register_audio_stream_loader_f(".mp3", _al_load_mp3_audio_stream_f);
62+
#endif
63+
5764
return ret;
5865
}
5966

addons/acodec/acodec.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,13 @@ ALLEGRO_AUDIO_STREAM *_al_load_ogg_opus_audio_stream_f(ALLEGRO_FILE* file,
5252
size_t buffer_count, unsigned int samples);
5353
#endif
5454

55+
#ifdef ALLEGRO_CFG_ACODEC_MP3
56+
ALLEGRO_SAMPLE *_al_load_mp3(const char *filename);
57+
ALLEGRO_SAMPLE *_al_load_mp3_f(ALLEGRO_FILE *f);
58+
ALLEGRO_AUDIO_STREAM *_al_load_mp3_audio_stream(const char *filename,
59+
size_t buffer_count, unsigned int samples);
60+
ALLEGRO_AUDIO_STREAM *_al_load_mp3_audio_stream_f(ALLEGRO_FILE* f,
61+
size_t buffer_count, unsigned int samples);
62+
#endif
5563

5664
#endif

addons/acodec/allegro5/internal/aintern_acodec_cfg.h.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#cmakedefine ALLEGRO_CFG_ACODEC_VORBIS
44
#cmakedefine ALLEGRO_CFG_ACODEC_TREMOR
55
#cmakedefine ALLEGRO_CFG_ACODEC_OPUS
6+
#cmakedefine ALLEGRO_CFG_ACODEC_MP3
67

78

89
/* Define if the library should be loaded dynamically. */

addons/acodec/mp3.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Allegro5 MP3 reader.
3+
* Requires MiniMP3 from https://github.com/lieff/minimp3
4+
* author: Mark Watkin (pmprog) 2019
5+
*/
6+
7+
8+
#include "allegro5/allegro.h"
9+
#include "allegro5/allegro_acodec.h"
10+
#include "allegro5/allegro_audio.h"
11+
#include "allegro5/internal/aintern.h"
12+
#include "allegro5/internal/aintern_audio.h"
13+
#include "allegro5/internal/aintern_exitfunc.h"
14+
#include "allegro5/internal/aintern_system.h"
15+
#include "acodec.h"
16+
#include "helper.h"
17+
18+
#define MINIMP3_IMPLEMENTATION
19+
#include <minimp3.h>
20+
#include <minimp3_ex.h>
21+
22+
ALLEGRO_DEBUG_CHANNEL("acodec")
23+
24+
static bool mp3_libinit = false;
25+
static mp3dec_t mp3d;
26+
27+
void mp3_initminimp3();
28+
29+
ALLEGRO_SAMPLE *_al_load_mp3(const char *filename)
30+
{
31+
ALLEGRO_FILE *f;
32+
ALLEGRO_SAMPLE *spl;
33+
ASSERT(filename);
34+
35+
f = al_fopen(filename, "rb");
36+
if (!f)
37+
return NULL;
38+
39+
spl = _al_load_mp3_f(f);
40+
41+
al_fclose(f);
42+
43+
return spl;
44+
}
45+
46+
ALLEGRO_SAMPLE *_al_load_mp3_f(ALLEGRO_FILE *f)
47+
{
48+
mp3_initminimp3(); // Make sure library is initialised
49+
50+
mp3dec_file_info_t info;
51+
ALLEGRO_SAMPLE *spl = NULL;
52+
53+
// Read our file size
54+
int64_t filesize = al_fsize(f);
55+
if (filesize == -1)
56+
return NULL;
57+
58+
// Allocate buffer and read all the file
59+
size_t fsize = (size_t)al_fsize(f);
60+
uint8_t* mp3data = (uint8_t*)al_malloc(fsize);
61+
size_t readbytes = al_fread(f, mp3data, fsize);
62+
if (readbytes != fsize)
63+
{
64+
al_free(mp3data);
65+
return NULL;
66+
}
67+
68+
// Decode the file contents, and copy to a new buffer
69+
mp3dec_load_buf(&mp3d, mp3data, filesize, &info, NULL, NULL);
70+
uint8_t* pcmdata = (uint8_t*)al_malloc(info.samples * sizeof(int16_t));
71+
memcpy(pcmdata, info.buffer, info.samples * sizeof(int16_t));
72+
73+
// Free file copy, and buffer (which is copied to pcmdata
74+
al_free(mp3data);
75+
al_free(info.buffer);
76+
77+
// Create sample from info variable
78+
spl = al_create_sample(pcmdata, info.samples, info.hz, _al_word_size_to_depth_conf(2), _al_count_to_channel_conf(info.channels), true);
79+
80+
return spl;
81+
}
82+
83+
ALLEGRO_AUDIO_STREAM *_al_load_mp3_audio_stream(const char *filename, size_t buffer_count, unsigned int samples)
84+
{
85+
ALLEGRO_FILE *f;
86+
ALLEGRO_AUDIO_STREAM *stream;
87+
ASSERT(filename);
88+
89+
f = al_fopen(filename, "rb");
90+
if (!f)
91+
return NULL;
92+
93+
stream = _al_load_mp3_audio_stream_f(f, buffer_count, samples);
94+
if (!stream) {
95+
al_fclose(f);
96+
}
97+
98+
return stream;
99+
}
100+
101+
ALLEGRO_AUDIO_STREAM *_al_load_mp3_audio_stream_f(ALLEGRO_FILE* f, size_t buffer_count, unsigned int samples)
102+
{
103+
mp3_initminimp3(); // Make sure library is initialised
104+
return NULL;
105+
}
106+
107+
void mp3_initminimp3()
108+
{
109+
if(!mp3_libinit)
110+
{
111+
mp3dec_init(&mp3d);
112+
mp3_libinit = true;
113+
}
114+
}

cmake/FindMiniMP3.cmake

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# - Try to find WebP.
2+
# Once done, this will define
3+
#
4+
# WEBP_FOUND - system has WebP.
5+
# WEBP_INCLUDE_DIRS - the WebP. include directories
6+
# WEBP_LIBRARIES - link these to use WebP.
7+
#
8+
# Copyright (C) 2012 Raphael Kubo da Costa <[email protected]>
9+
# Copyright (C) 2013 Igalia S.L.
10+
#
11+
# Redistribution and use in source and binary forms, with or without
12+
# modification, are permitted provided that the following conditions
13+
# are met:
14+
# 1. Redistributions of source code must retain the above copyright
15+
# notice, this list of conditions and the following disclaimer.
16+
# 2. Redistributions in binary form must reproduce the above copyright
17+
# notice, this list of conditions and the following disclaimer in the
18+
# documentation and/or other materials provided with the distribution.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND ITS CONTRIBUTORS ``AS
21+
# IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR ITS
24+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30+
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
# Look for the header file.
33+
find_path(MINIMP3_INCLUDE_DIRS
34+
NAMES minimp3.h minimp3_ex.h
35+
)
36+
mark_as_advanced(MINIMP3_INCLUDE_DIRS)
37+
38+
include(FindPackageHandleStandardArgs)
39+
find_package_handle_standard_args(MiniMP3 REQUIRED_VARS MINIMP3_INCLUDE_DIRS
40+
FOUND_VAR MINIMP3_FOUND)

0 commit comments

Comments
 (0)