leimao / Console-Player

Audio Player in Console Implemented Using C/C++

Repository from Github https://github.comleimao/Console-PlayerRepository from Github https://github.comleimao/Console-Player

Console Player

Lei Mao

Introduction

The Console Player (CPlayer) was implemented using C++, and the library libsndfile and libasound. libsndfile was used to decode audio files and libasound was used to play audio in the hardware.

The commonly supported audio formats in CPlayer includes wav, ogg, and flac. More effort of supporting mp3 in libsndfile is coming from the community. Additionally, the CPlayer library libcplayer could be used for other C++ projects, such as games, to play audio in the background asynchronously.

Dependencies

Files

.
├── bgm
│   ├── bgm.cpp
│   └── CMakeLists.txt
├── CMakeLists.txt
├── demo
│   ├── BIS1536-001-flac-16.flac
│   ├── punch-deck-elegance-in-simplicity.wav
│   └── spring-village.ogg
├── LICENSE.md
├── README.md
└── src
    ├── CMakeLists.txt
    ├── cplayer.cpp
    ├── cplayer.h
    └── main.cpp

Installation

Build Docker Image

$ docker build -f docker/clayer.Dockerfile --no-cache --tag=clayer:0.0.1 .

Run Docker Container

$ docker run -it --rm --device /dev/snd -v $(pwd):/mnt clayer:0.0.1

Install CMake

Check out the installation guide from Kitware.

Install Dependencies

$ apt-get install libsndfile-dev libasound2-dev

Install to Custom Directory (Recommended)

$ mkdir -p build
$ cd build
$ cmake -DCMAKE_INSTALL_PREFIX=[custom_dir] ..
$ make
$ make install

Install to Default Directory

$ mkdir -p build
$ cd build
$ cmake ..
$ make
$ # This requires writing permissions to `usr` directory
$ sudo make install

Usages

Play Audio in Console

./console_player audio_file [loop]

Add optional argument loop if you want to repeatedly playing the audio file.

Use CPlayer Library

The libcplayer library supports playing the audio in the background asynchronously. Please check the header file cplayer.h and the demo code bgm.cpp for details.

Demo

Run CPlayer in Console

$ mkdir -p build
$ cd build
$ cmake -DCMAKE_INSTALL_PREFIX=. ..
$ make
$ make install
$ cd bin/
$ # Play the audio file once.
$ ./console_player ../../demo/punch-deck-elegance-in-simplicity.wav
$ # Loop playing the audio file.
$ ./console_player ../../demo/spring-village.ogg loop

Run BGM Demo

$ ./bgm ../../demo/spring-village.ogg

The music would be played 6 times. In the first 3 times, the full audio file would be played. In the last 3 times, the player would receive kill signal from the program, and the audio play would be terminated before its end.

References

About

Audio Player in Console Implemented Using C/C++

License:MIT License


Languages

Language:C++ 84.5%Language:CMake 11.8%Language:Dockerfile 3.7%