commit 90e861a77aed6c9353e95dd123842219d24104b4
parent 1ea744502a318883c4d1616add3ae44a2195ab03
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date: Wed, 30 Jun 2010 12:39:06 +1000
Implement spek_audio_open()
Diffstat:
8 files changed, 164 insertions(+), 49 deletions(-)
diff --git a/configure.ac b/configure.ac
@@ -24,7 +24,7 @@ AM_PROG_VALAC([0.7.0])
AC_PROG_INSTALL
AC_PROG_INTLTOOL([0.35])
-pkg_modules="gtk+-2.0 >= 2.14.0 gstreamer-0.10 >= 0.10.17"
+pkg_modules="gtk+-2.0 >= 2.14.0 gstreamer-0.10 >= 0.10.17 libavformat libavcodec"
SPEK_PACKAGES="--pkg gtk+-2.0 --pkg gstreamer-0.10"
PKG_CHECK_MODULES(SPEK, [$pkg_modules])
AC_SUBST(SPEK_CFLAGS)
diff --git a/src/Makefile.am b/src/Makefile.am
@@ -26,3 +26,6 @@ VALAFLAGS = \
spek_LDADD = \
$(SPEK_LIBS) \
$(IGE_MAC_LIBS)
+
+EXTRA_DIST = \
+ spek-audio.h
diff --git a/src/spek-audio.c b/src/spek-audio.c
@@ -18,5 +18,67 @@
#include "spek-audio.h"
-void spek_audio_open (const char *file_name) {
+void spek_audio_init () {
+ av_register_all ();
+}
+
+SpekAudioContext * spek_audio_open (const char *file_name) {
+ SpekAudioContext *cx;
+ int i;
+
+ cx = g_new0 (SpekAudioContext, 1);
+ cx->file_name = g_strdup (file_name);
+
+ if (av_open_input_file (&cx->format_context, file_name, NULL, 0, NULL) != 0) {
+ /* TODO
+ */
+ return cx;
+ }
+ if (av_find_stream_info (cx->format_context)) {
+ /* TODO
+ */
+ return cx;
+ }
+ cx->audio_stream = -1;
+ for (i = 0; i < cx->format_context->nb_streams; i++) {
+ if (cx->format_context->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
+ cx->audio_stream = i;
+ break;
+ }
+ }
+ if (cx->audio_stream == -1) {
+ /* TODO
+ */
+ 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
+ */
+ return cx;
+ }
+ if (avcodec_open (cx->codec_context, cx->codec) < 0) {
+ /* TODO
+ */
+ return cx;
+ }
+
+ return cx;
+}
+
+void spek_audio_close (SpekAudioContext *cx) {
+ if (cx->file_name != NULL) {
+ g_free (cx->file_name);
+ }
+ if (cx->codec_context != NULL) {
+ avcodec_close (cx->codec_context);
+ }
+ if (cx->format_context != NULL) {
+ av_close_input_file (cx->format_context);
+ }
+ g_free (cx);
}
diff --git a/src/spek-audio.h b/src/spek-audio.h
@@ -19,6 +19,26 @@
#ifndef __SPEK_AUDIO_H__
#define __SPEK_AUDIO_H__
-void spek_audio_open (const char *file_name);
+#include <glib.h>
+#include <libavformat/avformat.h>
+#include <libavcodec/avcodec.h>
+
+typedef struct {
+ AVFormatContext *format_context;
+ gint audio_stream;
+ AVCodecContext *codec_context;
+ AVCodec *codec;
+
+ /* Exposed properties
+ */
+ gchar *file_name;
+ gint bit_rate;
+ gint sample_rate;
+ gint channels;
+} SpekAudioContext;
+
+void spek_audio_init ();
+SpekAudioContext * spek_audio_open (const char *file_name);
+void spek_audio_close (SpekAudioContext *cx);
#endif
diff --git a/src/spek-pipeline.vala b/src/spek-pipeline.vala
@@ -18,15 +18,27 @@
namespace Spek {
public class Pipeline {
- public string file_name { get; private set; }
- public string audio_codec { get; private set; }
+ private Audio.Context cx;
public Pipeline (string file_name) {
- this.file_name = file_name;
+ cx = new Audio.Context (file_name);
+ // TODO: check for errors
}
- public void open () {
- Audio.open (file_name);
+ public string file_name {
+ get { return cx.file_name; }
+ }
+
+ public int bit_rate {
+ get { return cx.bit_rate; }
+ }
+
+ public int sample_rate {
+ get { return cx.sample_rate; }
+ }
+
+ public int channels {
+ get { return cx.channels; }
}
}
}
diff --git a/src/spek-spectrogram.vala b/src/spek-spectrogram.vala
@@ -62,6 +62,11 @@ namespace Spek {
public void open (string file_name) {
this.file_name = file_name;
this.info = "";
+
+ // 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);
+
start ();
}
diff --git a/src/spek.vala b/src/spek.vala
@@ -16,45 +16,46 @@
* along with Spek. If not, see <http://www.gnu.org/licenses/>.
*/
-using Spek;
-
-bool version = false;
-[CCode (array_length = false, array_null_terminated = true)]
-string[] files = null;
-
-const OptionEntry[] options = {
- { "version", 'V', 0, OptionArg.NONE, ref version, N_("Display the version and exit"), null },
- { "", 0, 0, OptionArg.FILENAME_ARRAY, ref files, null, null },
- { null }
-};
-
-int main (string[] args) {
- Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
- Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
- Intl.textdomain (Config.GETTEXT_PACKAGE);
-
- try {
- Gtk.init_with_args (ref args, _("[FILE]"), (OptionEntry[]) options, Config.GETTEXT_PACKAGE);
- } catch (Error e) {
- print (e.message);
- print ("\n");
- print (_("Run `%s --help` to see a full list of available command line options.\n"), args[0]);
- return 1;
- }
-
- if (version) {
- print (_("%s version %s\n"), Config.PACKAGE_NAME, Config.PACKAGE_VERSION);
+namespace Spek {
+ bool version = false;
+ [CCode (array_length = false, array_null_terminated = true)]
+ string[] files = null;
+
+ const OptionEntry[] options = {
+ { "version", 'V', 0, OptionArg.NONE, ref version, N_("Display the version and exit"), null },
+ { "", 0, 0, OptionArg.FILENAME_ARRAY, ref files, null, null },
+ { null }
+ };
+
+ int main (string[] args) {
+ Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
+ Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
+ Intl.textdomain (Config.GETTEXT_PACKAGE);
+
+ try {
+ Gtk.init_with_args (ref args, _("[FILE]"), (OptionEntry[]) options, Config.GETTEXT_PACKAGE);
+ } catch (Error e) {
+ print (e.message);
+ print ("\n");
+ print (_("Run `%s --help` to see a full list of available command line options.\n"), args[0]);
+ return 1;
+ }
+
+ if (version) {
+ print (_("%s version %s\n"), Config.PACKAGE_NAME, Config.PACKAGE_VERSION);
+ return 0;
+ }
+
+ if (files != null && files.length != 1) {
+ print (_("Specify a single file\n"));
+ return 1;
+ }
+
+ Audio.init ();
+ Gst.init (ref args);
+ var window = new Window (files == null ? null : files[0]);
+ Gtk.main ();
+ window.destroy ();
return 0;
}
-
- if (files != null && files.length != 1) {
- print (_("Specify a single file\n"));
- return 1;
- }
-
- Gst.init (ref args);
- var window = new Window (files == null ? null : files[0]);
- Gtk.main ();
- window.destroy ();
- return 0;
-}
+}
+\ No newline at end of file
diff --git a/vapi/spek-audio.vapi b/vapi/spek-audio.vapi
@@ -1,4 +1,15 @@
[CCode (cprefix = "SpekAudio", lower_case_cprefix = "spek_audio_", cheader_filename = "spek-audio.h")]
namespace Spek.Audio {
- public static void open (string file_name);
+ [Compact]
+ [CCode (free_function = "spek_audio_close")]
+ public class Context {
+ public string file_name;
+ public int bit_rate;
+ public int sample_rate;
+ public int channels;
+
+ [CCode (cname = "spek_audio_open")]
+ public Context (string file_name);
+ }
+ public static void init ();
}