Skip to content

Commit 05009f7

Browse files
pmprogSiegeLord
authored andcommitted
Responding to change request from SiegeLord
1 parent b9fb1db commit 05009f7

File tree

1 file changed

+42
-43
lines changed

1 file changed

+42
-43
lines changed

addons/acodec/mp3.c

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ALLEGRO_DEBUG_CHANNEL("acodec")
2424
static bool mp3_libinit = false;
2525
static mp3dec_t mp3d;
2626

27-
void mp3_initminimp3();
27+
void mp3_initminimp3(void);
2828

2929
ALLEGRO_SAMPLE *_al_load_mp3(const char *filename)
3030
{
@@ -33,8 +33,10 @@ ALLEGRO_SAMPLE *_al_load_mp3(const char *filename)
3333
ASSERT(filename);
3434

3535
f = al_fopen(filename, "rb");
36-
if (!f)
36+
if (!f) {
37+
ALLEGRO_WARN("Could not open file '%s'.\n", filename);
3738
return NULL;
39+
}
3840

3941
spl = _al_load_mp3_f(f);
4042

@@ -45,38 +47,34 @@ ALLEGRO_SAMPLE *_al_load_mp3(const char *filename)
4547

4648
ALLEGRO_SAMPLE *_al_load_mp3_f(ALLEGRO_FILE *f)
4749
{
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-
50+
mp3_initminimp3(); // Make sure library is initialised
51+
52+
mp3dec_file_info_t info;
53+
ALLEGRO_SAMPLE *spl = NULL;
54+
55+
// Read our file size
56+
int64_t filesize = al_fsize(f);
57+
if (filesize == -1) {
58+
ALLEGRO_WARN("Could not determine file size.\n");
59+
return NULL;
60+
}
61+
62+
// Allocate buffer and read all the file
63+
uint8_t* mp3data = (uint8_t*)al_malloc(fsize);
64+
size_t readbytes = al_fread(f, mp3data, fsize);
65+
if (readbytes != fsize) {
66+
ALLEGRO_WARN("Failed to read file into memory.\n");
67+
al_free(mp3data);
68+
return NULL;
69+
}
70+
71+
// Decode the file contents, and copy to a new buffer
72+
mp3dec_load_buf(&mp3d, mp3data, filesize, &info, NULL, NULL);
73+
al_free(mp3data);
74+
75+
// Create sample from info variable
76+
spl = al_create_sample(info.buffer, info.samples, info.hz, _al_word_size_to_depth_conf(2), _al_count_to_channel_conf(info.channels), true);
77+
8078
return spl;
8179
}
8280

@@ -87,8 +85,10 @@ ALLEGRO_AUDIO_STREAM *_al_load_mp3_audio_stream(const char *filename, size_t buf
8785
ASSERT(filename);
8886

8987
f = al_fopen(filename, "rb");
90-
if (!f)
88+
if (!f) {
89+
ALLEGRO_WARN("Could not open file '%s'.\n", filename);
9190
return NULL;
91+
}
9292

9393
stream = _al_load_mp3_audio_stream_f(f, buffer_count, samples);
9494
if (!stream) {
@@ -100,15 +100,14 @@ ALLEGRO_AUDIO_STREAM *_al_load_mp3_audio_stream(const char *filename, size_t buf
100100

101101
ALLEGRO_AUDIO_STREAM *_al_load_mp3_audio_stream_f(ALLEGRO_FILE* f, size_t buffer_count, unsigned int samples)
102102
{
103-
mp3_initminimp3(); // Make sure library is initialised
104-
return NULL;
103+
mp3_initminimp3(); // Make sure library is initialised
104+
return NULL;
105105
}
106106

107107
void mp3_initminimp3()
108108
{
109-
if(!mp3_libinit)
110-
{
111-
mp3dec_init(&mp3d);
112-
mp3_libinit = true;
113-
}
114-
}
109+
if(!mp3_libinit) {
110+
mp3dec_init(&mp3d);
111+
mp3_libinit = true;
112+
}
113+
}

0 commit comments

Comments
 (0)