Playing MP3 files

A little background. Several years ago I developed a player for MP3 files. This was to accommodate a large collection of CDs and I designed it to catalog the files and play them. (this was programmed in Pascale on Windows) Today I am looking to rebuild the program in C++ with GTK so it will run on UBUNTU. I am needing a library that will play the MP3 files. I have looked but can not find a straight forward answer. What is available? Thanks
C++ isn't designed to do sound/music or graphics, that is done by using the OS API. There are 3rd party libraries you can use which will do the grunt work.

From 13 years ago: https://stackoverflow.com/questions/772208/best-audio-playback-api-for-c-c-under-linux

I'd bet there are newer and better libraries now. Maybe https://www.libsdl.org/?
Thank you so much! I had started looking that direction. I just need to figure how to use the API.
I think you need two things in order to build an "mp3 player", in addition to the GUI stuff:
1. A library to decompress the MP3 data into "raw" PCM samples
2. An API to send the PCM samples to the audio hardware for playback

As far as (1) is concerned, you should have a look libmpg123 or libavcodec:
- http://mpg123.de/
- https://en.wikipedia.org/wiki/Libavcodec

And for (2), I think that PulseAudio is the de facto standard on Linux these days:
https://en.wikipedia.org/wiki/PulseAudio

Maybe the PulseAudio output module of mpg123 is worth a look:
http://mpg123.de/cgi-bin/scm/mpg123/trunk/src/libout123/modules/pulse.c?revision=5031&view=markup

(I think SDL is more a "high-level" library/framework that does many more things than just audio playback. And, under the hood, at least on Linux, SDL uses a "native" API like PulseAudio to implement audio output)
Last edited on
https://github.com/lieff/minimp3

PulseAudio +1

SDL is for graphics, not sound.

[edit] I was wrong.
Last edited on
Duthomhas wrote:
SDL is for graphics, not sound.

The documentation for SDL sure had me fooled about audio support.
http://wiki.libsdl.org/Introduction

Audio
Set audio playback of 8-bit and 16-bit audio, mono stereo or 5.1 surround sound, with optional conversion if the format is not supported by the hardware
Audio runs independently in a separate thread, filled via a user callback mechanism
Designed for custom software audio mixers, but SDL_mixer provides a complete audio/music output library

My knowledge of the *nix system capabilities is limited, admittedly I've never heard of PulseAudio before.
Even though audio support in Linux is provided by ALSA (Advanced Linux Sound Architecture) on the lowest level, PulseAudio is the way how applications usually interface with the audio subsystem. For example, with "pure" ALSA only a single application could use an audio device at a time! PulseAudio sits on top of ALSA and provides things like "mixing" and stuff. PulseAudio is the de facto standard on most Linux distributions. So, if you have ever used "sound" on a somewhat recent Linux machine, probably PulseAudio was involved ;-)

In broad terms ALSA is a kernel subsystem that provides the sound hardware driver, and PulseAudio is the interface engine between applications and ALSA. [...] PulseAudio acts as a sound server, where a background process accepting sound input from one or more sources (processes, capture devices, etc.) is created. The background process then redirects these sound sources to one or more sinks (sound cards, remote network PulseAudio servers, or other processes).


To my understanding, SDL provides access to the graphics and audio hardware (and much more!) in a cross-platform way. Under the hood, it uses platform-specific interfaces on each platform, of course. On Linux, it would use, for example, X11/OpenGL and PluseAudio, whereas on Windows it would probably use DirectX. This means using SDL may be a great idea for developing cross-platform applications, but it probably is "killing a fly with a sledge hammer" for a simple Linux-only tool that just needs to do basic audio output.
Last edited on
Registered users can post here. Sign in or register to post.