Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mp3 support #782

Merged
merged 14 commits into from
Jun 8, 2023
2 changes: 1 addition & 1 deletion rts/Lua/LuaUnsyncedCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ int LuaUnsyncedCtrl::PlaySoundFile(lua_State* L)
}


/*** Allows to play an Ogg Vorbis (.OGG) compressed sound file.
/*** Allows to play an Ogg Vorbis (.OGG) and mp3 compressed sound file.
*
* @function Spring.PlaySoundStream
*
Expand Down
4 changes: 3 additions & 1 deletion rts/System/Sound/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ if (NOT NO_SOUND)
OpenAL/EFXfuncs.cpp
OpenAL/EFXPresets.cpp
OpenAL/AudioChannel.cpp
OpenAL/OggStream.cpp
OpenAL/OggDecoder.cpp
OpenAL/Mp3Decoder.cpp
OpenAL/MusicStream.cpp
OpenAL/Sound.cpp
OpenAL/SoundChannels.cpp
OpenAL/SoundBuffer.cpp
Expand Down
41 changes: 41 additions & 0 deletions rts/System/Sound/OpenAL/Mp3Decoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#define DR_MP3_IMPLEMENTATION
#define DR_MP3_NO_STDIO
#include "lib/dr_mp3/dr_mp3.h"

#include "System/Sound/OpenAL/Mp3Decoder.h"
#include "System/Sound/SoundLog.h"


int Mp3Decoder::GetChannels() const
{
return data.channels;
}

long Mp3Decoder::GetRate() const
{
return data.sampleRate;
}

long Mp3Decoder::Read(uint8_t *buffer,int length, int bigendianp,int word,int sgned,int *bitstream)
{
auto bytesToFrames = sizeof(drmp3_int16) / sizeof(*buffer);
return drmp3_read_pcm_frames_s16(&data, length / bytesToFrames / data.channels,
reinterpret_cast<drmp3_int16*>(buffer)) * bytesToFrames * data.channels;
}

float Mp3Decoder::GetTotalTime()
{
return 1.0f * drmp3_get_pcm_frame_count(&data) / data.sampleRate; // linear complexity
dzosz marked this conversation as resolved.
Show resolved Hide resolved
}

bool Mp3Decoder::LoadData(const uint8_t* mem, size_t len)
{
if (!drmp3_init_memory(&data, mem, len, NULL)) {
dzosz marked this conversation as resolved.
Show resolved Hide resolved
LOG_L(L_ERROR, "[Mp3Decoder::LoadFile] Failed to load");
return false;
}
return true;
}

22 changes: 22 additions & 0 deletions rts/System/Sound/OpenAL/Mp3Decoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef MP3DECODER_H
#define MP3DECODER_H

#include "lib/dr_mp3/dr_mp3.h"
#include <cstdint>

class Mp3Decoder
{
public:
long Read(uint8_t *buffer, int length, int , int , int , int *);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider naming the arguments, especially since it's not obvious what they are from context

Suggested change
long Read(uint8_t *buffer, int length, int , int , int , int *);
long Read(uint8_t *buffer, int length, int bigendianp, int word, int sgned, int *bitstream);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the common interface for all decoders. mp3 decoder does not use unnamed variables.

bool LoadData(const uint8_t* mem, size_t len);
int GetChannels() const;
long GetRate() const;
float GetTotalTime();

private:
drmp3 data;
};

#endif // MP3DECODER_H
Loading