spek

Acoustic spectrum analyser
git clone http://git.hanabi.in/repos/spek.git
Log | Files | Refs | README

commit fd0d80815d24ee8342d05956c82615dee820f33a
parent 90e861a77aed6c9353e95dd123842219d24104b4
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Wed, 30 Jun 2010 14:29:58 +1000

Build the audio description string using ffmpeg

Diffstat:
Msrc/spek-audio.c | 24+++++++++++++++++++-----
Msrc/spek-audio.h | 2++
Msrc/spek-pipeline.vala | 25++++++++++++++++++++++++-
Msrc/spek-spectrogram.vala | 2+-
Mvapi/spek-audio.vapi | 2++
5 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/src/spek-audio.c b/src/spek-audio.c @@ -19,6 +19,7 @@ #include "spek-audio.h" void spek_audio_init () { + avcodec_init (); av_register_all (); } @@ -32,11 +33,13 @@ SpekAudioContext * spek_audio_open (const char *file_name) { if (av_open_input_file (&cx->format_context, file_name, NULL, 0, NULL) != 0) { /* TODO */ + printf ("cannot open\n"); return cx; } - if (av_find_stream_info (cx->format_context)) { + if (av_find_stream_info (cx->format_context) < 0) { /* TODO */ + printf ("cannot find stream info\n"); return cx; } cx->audio_stream = -1; @@ -49,24 +52,32 @@ SpekAudioContext * spek_audio_open (const char *file_name) { if (cx->audio_stream == -1) { /* TODO */ + printf ("no audio streams\n"); return cx; } cx->codec_context = cx->format_context->streams[cx->audio_stream]->codec; - cx->bit_rate = cx->codec_context->bit_rate; - cx->sample_rate = cx->codec_context->sample_rate; - cx->channels = cx->codec_context->channels; cx->codec = avcodec_find_decoder (cx->codec_context->codec_id); if (cx->codec == NULL) { /* TODO */ + printf ("cannot find decoder\n"); return cx; } if (avcodec_open (cx->codec_context, cx->codec) < 0) { /* TODO */ + printf ("cannot open decoder\n"); return cx; } - + cx->codec_name = g_strdup (cx->codec->long_name); + cx->bit_rate = cx->codec_context->bit_rate; + cx->sample_rate = cx->codec_context->sample_rate; + cx->bits_per_sample = cx->codec_context->bits_per_raw_sample; + if (!cx->bits_per_sample) { + /* APE uses bpcs, FLAC uses bprs. */ + cx->bits_per_sample = cx->codec_context->bits_per_coded_sample; + } + cx->channels = cx->codec_context->channels; return cx; } @@ -74,6 +85,9 @@ void spek_audio_close (SpekAudioContext *cx) { if (cx->file_name != NULL) { g_free (cx->file_name); } + if (cx->codec_name != NULL) { + g_free (cx->codec_name); + } if (cx->codec_context != NULL) { avcodec_close (cx->codec_context); } diff --git a/src/spek-audio.h b/src/spek-audio.h @@ -32,8 +32,10 @@ typedef struct { /* Exposed properties */ gchar *file_name; + gchar *codec_name; gint bit_rate; gint sample_rate; + gint bits_per_sample; gint channels; } SpekAudioContext; diff --git a/src/spek-pipeline.vala b/src/spek-pipeline.vala @@ -19,10 +19,33 @@ namespace Spek { public class Pipeline { private Audio.Context cx; + public string description { get; private set; } public Pipeline (string file_name) { - cx = new Audio.Context (file_name); // TODO: check for errors + cx = new Audio.Context (file_name); + + // Build the description string. + string[] items = {}; + if (cx.codec_name != null) { + items += cx.codec_name; + } + if (cx.bit_rate != 0) { + items += _("%d kbps").printf (cx.bit_rate / 1000); + } + if (cx.sample_rate != 0) { + items += _("%d Hz").printf (cx.sample_rate); + } + // Show bits per sample only if there is no bitrate. + if (cx.bits_per_sample != 0 && cx.bit_rate == 0) { + items += ngettext ( + "%d bit", "%d bits", cx.bits_per_sample).printf (cx.bits_per_sample); + } + if (cx.channels != 0) { + items += ngettext ("%d channel", "%d channels", cx.channels). + printf (cx.channels); + } + description = items.length > 0 ? string.joinv (", ", items) : ""; } public string file_name { diff --git a/src/spek-spectrogram.vala b/src/spek-spectrogram.vala @@ -65,7 +65,7 @@ namespace Spek { // TODO var pipeline = new Pipeline (file_name); - print ("%s:\nbr=%i sr=%i ch=%i\n\n", pipeline.file_name, pipeline.bit_rate, pipeline.sample_rate, pipeline.channels); + print ("%s:\n%s\n", file_name, pipeline.description); start (); } diff --git a/vapi/spek-audio.vapi b/vapi/spek-audio.vapi @@ -4,8 +4,10 @@ namespace Spek.Audio { [CCode (free_function = "spek_audio_close")] public class Context { public string file_name; + public string codec_name; public int bit_rate; public int sample_rate; + public int bits_per_sample; public int channels; [CCode (cname = "spek_audio_open")]