Skip to content

Commit 845314b

Browse files
author
dashodanger
committed
Fix MP3 music playback
1 parent 7bb7461 commit 845314b

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

libraries/dr_libs/dr_mp3.h

+24-1
Original file line numberDiff line numberDiff line change
@@ -3655,7 +3655,30 @@ static drmp3_uint64 drmp3_read_pcm_frames_raw(drmp3* pMP3, drmp3_uint64 framesTo
36553655
/* s16 */
36563656
drmp3_int16* pFramesOutS16 = (drmp3_int16*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(drmp3_int16) * totalFramesRead * pMP3->channels);
36573657
drmp3_int16* pFramesInS16 = (drmp3_int16*)DRMP3_OFFSET_PTR(&pMP3->pcmFrames[0], sizeof(drmp3_int16) * pMP3->pcmFramesConsumedInMP3Frame * pMP3->mp3FrameChannels);
3658-
DRMP3_COPY_MEMORY(pFramesOutS16, pFramesInS16, sizeof(drmp3_int16) * framesToConsume * pMP3->channels);
3658+
if (pMP3->mp3FrameChannels == pMP3->channels) {
3659+
/* Fast path. */
3660+
DRMP3_COPY_MEMORY(pFramesOutS16, pFramesInS16, sizeof(drmp3_int16) * framesToConsume * pMP3->channels);
3661+
} else {
3662+
/* Slow path. Channel conversion required. */
3663+
drmp3_uint32 iFrame;
3664+
if (pMP3->mp3FrameChannels == 1) {
3665+
/* Mono -> Stereo */
3666+
DRMP3_ASSERT(pMP3->channels == 2);
3667+
for (iFrame = 0; iFrame < framesToConsume; iFrame += 1) {
3668+
pFramesOutS16[iFrame*2 + 0] = pFramesInS16[iFrame];
3669+
pFramesOutS16[iFrame*2 + 1] = pFramesInS16[iFrame];
3670+
}
3671+
} else {
3672+
/* Stereo -> Mono */
3673+
DRMP3_ASSERT(pMP3->channels == 1);
3674+
for (iFrame = 0; iFrame < framesToConsume; iFrame += 1) {
3675+
drmp3_int32 sample = 0;
3676+
sample += pFramesInS16[iFrame*pMP3->mp3FrameChannels + 0];
3677+
sample += pFramesInS16[iFrame*pMP3->mp3FrameChannels + 1];
3678+
pFramesOutS16[iFrame] = (drmp3_int16)(sample / 2);
3679+
}
3680+
}
3681+
}
36593682
#endif
36603683
}
36613684

source_files/edge/s_mp3.cc

+2-10
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class MP3Player : public AbstractMusicPlayer
4848
int status_;
4949

5050
bool looping_;
51-
bool is_stereo_;
5251

5352
uint8_t *mp3_data_ = nullptr;
5453
drmp3 *mp3_decoder_ = nullptr;
@@ -85,15 +84,6 @@ MP3Player::~MP3Player()
8584

8685
void MP3Player::PostOpen()
8786
{
88-
if (mp3_decoder_->channels == 1)
89-
{
90-
is_stereo_ = false;
91-
}
92-
else
93-
{
94-
is_stereo_ = true;
95-
}
96-
9787
// Loaded, but not playing
9888
status_ = kStopped;
9989
}
@@ -142,6 +132,8 @@ bool MP3Player::OpenMemory(uint8_t *data, int length)
142132
return false;
143133
}
144134

135+
// Force stereo for MP3 music if not already the case
136+
mp3_decoder_->channels = 2;
145137
mp3_data_ = data;
146138
PostOpen();
147139
return true;

0 commit comments

Comments
 (0)