Created
January 12, 2015 17:11
-
-
Save caprica/1a6d0468675af33f7d36 to your computer and use it in GitHub Desktop.
How to use the audio equalizer in LibVLC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A simple program to test the audio equalizer API in LibVLC. | |
* | |
* An example build command: | |
* | |
* $gcc -std=c99 -I/home/linux/vlc/vlc/include -o equalizer equalizer.c `pkg-config --cflags --libs libvlc` | |
* | |
* You may also need to set PKG_CONFIG_PATH first, for example: | |
* | |
* $export PKG_CONFIG_PATH=/home/linux/vlc/install/lib/pkgconfig | |
* | |
* When running this test, you must pass via the command-line two separate audio filenames | |
* to be played during the test - for example: | |
* | |
* ./equalizer /home/music/1.mp3 /home/music/2.mp3 | |
* | |
* You may also need to set LD_LIBARRY_PATH, for example: | |
* | |
* $export LD_LIBRARY_PATH=/home/linux/vlc/install/lib | |
* | |
* mark dot lee at capricasofware.co.uk | |
*/ | |
#include <unistd.h> | |
#include <vlc/vlc.h> | |
#include <vlc/libvlc.h> | |
void dump(libvlc_equalizer_t *p_equalizer); | |
int main(int argc, char *argv[]) { | |
if(argc != 3) { | |
printf("Specify two audio file names\n"); | |
return -1; | |
} | |
char *psz_file1 = argv[1]; | |
char *psz_file2 = argv[2]; | |
libvlc_instance_t *p_instance = libvlc_new(0, NULL); | |
printf("Equalizer Tests\n\n"); | |
// Dump out the individual frequency bands | |
printf("Bands...\n"); | |
unsigned u_bands = libvlc_audio_equalizer_get_band_count(); | |
for(int i = 0; i < u_bands; i++) { | |
printf(" Band %2d -> %.1fHz\n", i, libvlc_audio_equalizer_get_band_frequency(i)); | |
} | |
printf("\n"); | |
// Dump out each preset name | |
printf("Presets...\n"); | |
unsigned u_presets = libvlc_audio_equalizer_get_preset_count(); | |
for(int i = 0; i < u_presets; i++) { | |
printf(" Preset %2d -> %s\n", i, libvlc_audio_equalizer_get_preset_name(i)); | |
} | |
printf("\n"); | |
// Dump out the settings for each preset | |
for(int i = 0; i < u_presets; i++) { | |
const char* psz_name = libvlc_audio_equalizer_get_preset_name(i); | |
printf("%d %s\n", i, psz_name); | |
libvlc_equalizer_t *p_equalizer = libvlc_audio_equalizer_new_from_preset(i); | |
dump(p_equalizer); | |
libvlc_audio_equalizer_release(p_equalizer); | |
} | |
// Now some tests with the equalizer and a media player... | |
printf("Starting tests...\n\n"); | |
libvlc_media_player_t *p_media_player = libvlc_media_player_new(p_instance); | |
libvlc_media_t *p_media = libvlc_media_new_path(p_instance, psz_file1); | |
libvlc_media_player_set_media(p_media_player, p_media); | |
libvlc_media_release(p_media); | |
// sleep(i_delay) is used just because it is convenient for this test, of course you wouldn't do that normally | |
int i_delay = 5; // seconds | |
// Presets... | |
libvlc_equalizer_t *p_equalizer_full_treble = libvlc_audio_equalizer_new_from_preset(6); | |
libvlc_equalizer_t *p_equalizer_full_bass = libvlc_audio_equalizer_new_from_preset(4); | |
// It is possible to set an equalizer before playing media | |
printf("Set full treble\n"); | |
libvlc_media_player_set_equalizer(p_media_player, p_equalizer_full_treble); | |
// Now play... | |
printf("Play\n"); | |
libvlc_media_player_play(p_media_player); | |
sleep(i_delay); | |
// Change equalizer while playing | |
printf("Set full bass\n"); | |
libvlc_media_player_set_equalizer(p_media_player, p_equalizer_full_bass); | |
sleep(i_delay); | |
printf("Set full treble\n"); | |
libvlc_media_player_set_equalizer(p_media_player, p_equalizer_full_treble); | |
sleep(i_delay); | |
// Stop playback | |
printf("Stop\n"); | |
libvlc_media_player_stop(p_media_player); | |
sleep(1); | |
// Start playback, settings should be remembered | |
printf("Play full treble\n"); | |
libvlc_media_player_play(p_media_player); | |
sleep(i_delay); | |
// Change equalizer while playing | |
printf("Set full bass\n"); | |
libvlc_media_player_set_equalizer(p_media_player, p_equalizer_full_bass); | |
sleep(i_delay); | |
printf("Set full treble\n"); | |
libvlc_media_player_set_equalizer(p_media_player, p_equalizer_full_treble); | |
sleep(i_delay); | |
// Change media | |
printf("Change media\n"); | |
p_media = libvlc_media_new_path(p_instance, psz_file2); | |
libvlc_media_player_set_media(p_media_player, p_media); | |
libvlc_media_release(p_media); | |
// Play new media, settings should be remembered | |
libvlc_media_player_play(p_media_player); | |
sleep(i_delay); | |
// Change equalizer while playing | |
printf("Set full bass\n"); | |
libvlc_media_player_set_equalizer(p_media_player, p_equalizer_full_bass); | |
sleep(i_delay); | |
printf("Set full treble\n"); | |
libvlc_media_player_set_equalizer(p_media_player, p_equalizer_full_treble); | |
sleep(i_delay); | |
printf("Disable equalizer\n"); | |
libvlc_media_player_set_equalizer(p_media_player, NULL); | |
sleep(i_delay); | |
printf("Set full treble\n"); | |
libvlc_media_player_set_equalizer(p_media_player, p_equalizer_full_treble); | |
sleep(i_delay); | |
printf("Disable equalizer\n"); | |
libvlc_media_player_set_equalizer(p_media_player, NULL); | |
sleep(i_delay); | |
// We're finished with the presets, they are no longer associated with any | |
// media player, so release them | |
libvlc_audio_equalizer_release(p_equalizer_full_bass); | |
libvlc_audio_equalizer_release(p_equalizer_full_treble); | |
libvlc_media_player_stop(p_media_player); | |
// Custom... | |
libvlc_equalizer_t *p_equalizer = libvlc_audio_equalizer_new(); | |
libvlc_audio_equalizer_set_preamp(p_equalizer, 6.5f); | |
libvlc_audio_equalizer_set_amp_at_index(p_equalizer, 20.0f, 1); | |
libvlc_audio_equalizer_set_amp_at_index(p_equalizer, 15.3f, 7); | |
libvlc_audio_equalizer_set_amp_at_index(p_equalizer, 16.2f, 8); | |
libvlc_audio_equalizer_set_amp_at_index(p_equalizer, 17.1f, 9); | |
// Set a new custom equalizer before playback starts | |
printf("Set custom\n"); | |
libvlc_media_player_set_equalizer(p_media_player, p_equalizer); | |
printf("Play\n"); | |
libvlc_media_player_play(p_media_player); | |
sleep(i_delay); | |
// Change the custom equalizer while playing | |
printf("Change custom\n"); | |
libvlc_audio_equalizer_set_preamp(p_equalizer, 8.75f); | |
libvlc_audio_equalizer_set_amp_at_index(p_equalizer, 20.0f, 4); | |
libvlc_audio_equalizer_set_amp_at_index(p_equalizer, 20.0f, 5); | |
libvlc_media_player_set_equalizer(p_media_player, p_equalizer); | |
sleep(i_delay); | |
printf("Disable equalizer\n"); | |
libvlc_media_player_set_equalizer(p_media_player, NULL); | |
sleep(i_delay); | |
printf("End of test\n"); | |
libvlc_audio_equalizer_release(p_equalizer); | |
libvlc_media_player_stop(p_media_player); | |
libvlc_media_player_release(p_media_player); | |
libvlc_release(p_instance); | |
return 0; | |
} | |
void dump(libvlc_equalizer_t *p_equalizer) { | |
if(p_equalizer) { | |
unsigned u_bands = libvlc_audio_equalizer_get_band_count(); | |
printf(" Preamp: %18.2fdB\n", libvlc_audio_equalizer_get_preamp(p_equalizer)); | |
for(unsigned i = 0; i < u_bands; i++) { | |
printf(" Band: %7.1fHz -> %5.2fdB\n", libvlc_audio_equalizer_get_band_frequency(i), libvlc_audio_equalizer_get_amp_at_index(p_equalizer, i)); | |
} | |
} | |
else { | |
printf("No equalizer.\n"); | |
} | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist doesn't really relate to your question.
What you're asking about can be done using various LibVLC language bindings that give you the raw audio samples via a callback. However, if you use those audio callbacks to do your frequency analysis you also have to play those samples yourself somehow - in this case VLC does not send the audio to any output.