changeset 2300:fca4a63e086e

totally converted sndfile plugin to use VFS
author mf0102 <0102@gmx.at>
date Sat, 12 Jan 2008 16:26:05 +0100
parents 1bc5ccdc47d5
children 227de9a5467d
files src/wav/wav-sndfile.c
diffstat 1 files changed, 31 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/src/wav/wav-sndfile.c	Sat Jan 12 22:52:04 2008 +0900
+++ b/src/wav/wav-sndfile.c	Sat Jan 12 16:26:05 2008 +0100
@@ -86,6 +86,25 @@
     sf_tell
 };
 
+static SNDFILE *
+open_sndfile_from_uri(gchar *filename, VFSFile *vfsfile, SF_INFO *tmp_sfinfo)
+{
+    vfsfile = aud_vfs_fopen(filename, "rb");
+
+    if (vfsfile == NULL)
+        return NULL;
+
+    return sf_open_virtual (&sf_virtual_io, SFM_READ, tmp_sfinfo, vfsfile);
+}
+
+static void
+close_sndfile(SNDFILE *snd_file, VFSFile *vfsfile)
+{
+    sf_close(snd_file);
+    aud_vfs_fclose(vfsfile);
+}
+
+
 
 static void
 plugin_init (void)
@@ -103,43 +122,19 @@
     g_mutex_free(decode_mutex);
 }
 
-static int
-get_song_length (char *filename)
-{
-    SNDFILE *tmp_sndfile;
-    SF_INFO tmp_sfinfo;
-    gchar *realfn = NULL;
-
-    realfn = g_filename_from_uri(filename, NULL, NULL);
-    tmp_sndfile = sf_open (realfn ? realfn : filename, SFM_READ, &tmp_sfinfo);
-    g_free(realfn); realfn = NULL;
-
-    if (!tmp_sndfile) {
-        return 0;
-    }
-
-    sf_close (tmp_sndfile);
-    tmp_sndfile = NULL;
-
-    if (tmp_sfinfo.samplerate <= 0)
-        return 0;
-
-    return (int) ceil (1000.0 * tmp_sfinfo.frames / tmp_sfinfo.samplerate);
-}
-
 static void
 fill_song_tuple (char *filename, Tuple *ti)
 {
+    VFSFile *vfsfile = NULL;
     SNDFILE *tmp_sndfile;
     SF_INFO tmp_sfinfo;
     unsigned int lossy = 0;
-    gchar *realfn = NULL, *codec = NULL, *format, *subformat = NULL;
+    gchar *codec = NULL, *format, *subformat = NULL;
     GString *codec_gs = NULL;
 
-    realfn = g_filename_from_uri(filename, NULL, NULL);
-    tmp_sndfile = sf_open (realfn ? realfn : filename, SFM_READ, &tmp_sfinfo);
+    tmp_sndfile = open_sndfile_from_uri(filename, vfsfile, &tmp_sfinfo);
     if ( sf_get_string(tmp_sndfile, SF_STR_TITLE) == NULL)
-        aud_tuple_associate_string(ti, FIELD_TITLE, NULL, g_path_get_basename(realfn ? realfn : filename));
+        aud_tuple_associate_string(ti, FIELD_TITLE, NULL, g_path_get_basename(filename));
     else
         aud_tuple_associate_string(ti, FIELD_TITLE, NULL, sf_get_string(tmp_sndfile, SF_STR_TITLE));
 
@@ -148,12 +143,10 @@
     aud_tuple_associate_string(ti, FIELD_DATE, NULL, sf_get_string(tmp_sndfile, SF_STR_DATE));
     aud_tuple_associate_string(ti, -1, "software", sf_get_string(tmp_sndfile, SF_STR_SOFTWARE));
 
-    g_free(realfn); realfn = NULL;
-
     if (!tmp_sndfile)
         return;
 
-    sf_close (tmp_sndfile);
+    close_sndfile (tmp_sndfile, vfsfile);
     tmp_sndfile = NULL;
 
     if (tmp_sfinfo.samplerate > 0)
@@ -343,22 +336,19 @@
 static int
 is_our_file (char *filename)
 {
+    VFSFile *vfsfile = NULL;
     SNDFILE *tmp_sndfile;
     SF_INFO tmp_sfinfo;
-    gchar *realfn = NULL; 
-
-    realfn = g_filename_from_uri(filename, NULL, NULL);
 
     /* Have to open the file to see if libsndfile can handle it. */
-    tmp_sndfile = sf_open (realfn ? realfn : filename, SFM_READ, &tmp_sfinfo);
-    g_free(realfn); realfn = NULL;
+    tmp_sndfile = open_sndfile_from_uri(filename, vfsfile, &tmp_sfinfo);
 
     if (!tmp_sndfile) {
         return FALSE;
     }
 
     /* It can so close file and return TRUE. */
-    sf_close (tmp_sndfile);
+    close_sndfile (tmp_sndfile, vfsfile);
     tmp_sndfile = NULL;
 
     return TRUE;
@@ -437,7 +427,7 @@
 static void
 play_start (InputPlayback *playback)
 {
-    gchar *realfn = NULL;
+    VFSFile *vfsfile = NULL;
     int pcmbitwidth;
     gchar *song_title;
 
@@ -447,9 +437,7 @@
     pcmbitwidth = 32;
     song_title = get_title(playback->filename);
 
-    realfn = g_filename_from_uri(playback->filename, NULL, NULL);
-    sndfile = sf_open (realfn ? realfn : playback->filename, SFM_READ, &sfinfo);
-    g_free(realfn); realfn = NULL;
+    sndfile = open_sndfile_from_uri(playback->filename, vfsfile, &sfinfo);
 
     if (!sndfile)
         return;
@@ -463,7 +451,7 @@
 
     if (! playback->output->open_audio (FMT_S16_NE, sfinfo.samplerate, sfinfo.channels))
     {
-        sf_close (sndfile);
+        close_sndfile (sndfile, vfsfile);
         sndfile = NULL;
         return;
     }
@@ -521,13 +509,6 @@
     file_mseek(playback, millisecond);
 }
 
-static void
-get_song_info (gchar *filename, gchar **title, gint *length)
-{
-    (*length) = get_song_length(filename);
-    (*title) = get_title(filename);
-}
-
 static Tuple*
 get_song_tuple (gchar *filename)
 {
@@ -594,9 +575,8 @@
     .pause = play_pause,
     .seek = file_seek,
     .cleanup = plugin_cleanup,
-    .get_song_info = get_song_info,
     .get_song_tuple = get_song_tuple,
-    //.is_our_file_from_vfs = is_our_file_from_vfs,
+    .is_our_file_from_vfs = is_our_file_from_vfs,
     .vfs_extensions = fmts,
     .mseek = file_mseek,
 };