Giter VIP home page Giter VIP logo

libjukebox's Introduction

libjukebox

Self-contained (i.e., no external dependencies) and platform independent (windows/linux) C++ audio playback library (uses DirectSound under Windows and Alsa under Linux).

Supported file formats:

Effects and features

  • Fade in/out;
  • Fade on stop;
  • Simple reverberation (multiple delay lines with decay parameter);
  • Distortion (tanh);
  • File writer driver output;
  • One shot timed events (with seconds resolution) and on stop event stack;
  • Extensible architecture allows implementation of custom effects and audio drivers.

Building

apt install libasound2-dev liblua5.3-dev make -f makefile.linux or make -f makefile.mingw

This will compile the library (.so or .dll) and examples in 'bin/' directory. All you'll need to use is the header libjukebox.h and the library to link against your own project.

To provide MIDI support we are using FluidSynth, but you don't need to install nor build another library prior to building libjukebox because FluidSynth already comes embedded into it (i.e., there is no external dependency with it).

Simple Usage

auto sound = jukebox::factory::makeSound("audio.mp3"); // create sound
sound.play(); // start playing
std::cout << "hit enter to stop..." << std::endl;
std::cin.get();

sound.stop(); // stop playing
std::cout << "hit enter to exit..." << std::endl;
std::cin.get();

Combining effects

auto sound = jukebox::factory::makeSound("audio.mp3"); // create sound

sound
	.fade(2, 2) // 2 seconds fade in & out
	.loop(true) // looping
	.setVolume(100) // 100% volume
	.distortion(50) // gain = 50
	.reverb(0.01, 0.8, 3) // robot voice (delay, decay, # delays)
	.play(); // start playing
	
std::cout << "hit enter to exit..." << std::endl;
std::cin.get();

Example

#include <iostream>
#include <algorithm>
#include <exception>
#include "libjukebox.h"

// forward declarations
void printFileInfo(const jukebox::SoundFile &file);

int main(int argc, char **argv) {
	if( argc < 2 ) {
		std::cout << "usage: " << argv[0] << " filename.[wav|ogg|mp3|mid|flac]" << std::endl;
		return 1;
	}

	std::string filename(argv[1]);

	try {
		// load a sound file
		auto soundFile = jukebox::factory::loadFile(filename);

		// print file info
		printFileInfo(soundFile);

		auto sound = jukebox::factory::makeSound(soundFile);

		jukebox::Mixer mixer;
		mixer.setVolume(100); // max global volume

		sound
			.fadeOnStop(3) // 3 seconds fade out on stop
			.loop(true) // looping
			.setVolume(100) // 100% volume 
			.play(); // start playing

		std::cout << "hit enter to fade out..." << std::endl;
		std::cin.get();
		sound.stop(); // fade out the sound before stopping it

		std::cout << "hit enter to exit..." << std::endl;
		std::cin.get();
	} catch (std::exception &e) {
		std::cerr << "error loading " << filename << ": " << e.what() << std::endl;
	}
	return 0;
}

std::string formatDuration(double duration) {
	int hr = duration/3600;
	duration -= hr*3600;
	int min = duration/60;
	duration -= min*60;
	int secs = duration;

	return
		std::to_string(hr) + ":" +
		(min<10?"0":"") + std::to_string(min) + ":" +
		(secs<10?"0":"") + std::to_string(secs);
};

void printFileInfo(const jukebox::SoundFile &file) {
	static std::array<std::string, 2> channels = {"Mono", "Stereo"};

	std::cout << file.getFilename() << " attributes: " << std::endl;
	std::cout << file.getBitsPerSample() << " bits" << std::endl;
	std::cout << channels[file.getNumChannels() - 1] << std::endl;
	std::cout << file.getSampleRate() << " Hz" << std::endl;
	std::cout << file.getDataSize() << " bytes" << std::endl;
	std::cout << formatDuration(file.getDuration()) << " sec(s)" << std::endl << std::endl;
}

Credits

Collaborators

libjukebox's People

Contributors

rpvelloso avatar thinlizzy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

libjukebox's Issues

refactor decoder

expose only the decoder to the soundimpl, remove soundfile from it. This will ease decoration at the decoder for resampling, resolution change, etc.

Sound files should support loading by chunks

Now they are reading and decoding the entire files, which can drain the application memory if there are too many objects and start causing memory trashing!

Both wav and ogg files have this behavior.

We need a way to specify a maximum chunk size and design some sort of streaming when playing Sound objects.

TODO: refactor sound states

  • move state transitions outside the states, make transitions explicit;
  • do not detach playing thread and join it upon state destruction/transition.

prevent more than one decoder when loading SoundFile from stream

When a soundfile is loaded from a stream, chunk by chunk, it can't be played simultaneously by multiple sound objects (i.e., it can't be shared) because the stream pointer can't be shared (it could be shared using mutex and alike, but we want to avoid this). So a stream soundfile should be allowed to instantiate only one decoder. If sharing is needed then the soundfile should be loaded to memory so it can be played simultaneously by several sound objects (or, if the file is too big, multiples soundfile should be created for the same disk file).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.