summaryrefslogtreecommitdiffstats
path: root/flow/gsl/gslloader-oggvorbis.c
blob: f1491d7edced41a1920de0b967b6ee509fd6f284 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* GSL - Generic Sound Layer
 * Copyright (C) 1998, 2000, 2001 Tim Janik
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "gsl/gslloader.h"

#if GSL_HAVE_OGGVORBIS
#include <gsl/gsldatahandle.h>
#include "gsldatahandle-vorbis.h"

#include <vorbis/vorbisfile.h>
#include <string.h>
#include <unistd.h>


/* --- structures --- */
typedef struct
{
  GslWaveFileInfo wfi;
  OggVorbis_File  ofile;
} FileInfo;


/* --- functions --- */
static GslWaveFileInfo*
oggv_load_file_info (gpointer      data,
		     const gchar  *file_name,
		     GslErrorType *error_p)
{
  FileInfo *fi = gsl_new_struct0 (FileInfo, 1);
  FILE *file;
  gint err, i;

  file = fopen (file_name, "r");
  if (!file)
    {
      *error_p = GSL_ERROR_OPEN_FAILED;
      return NULL;
    }

  fi = gsl_new_struct0 (FileInfo, 1);
  err = ov_open (file, &fi->ofile, NULL, 0);
  if (err)
    {
      fclose (file);
      gsl_delete_struct (FileInfo, fi);
      *error_p = GSL_ERROR_CODEC_FAILURE;
      return NULL;
    }

  fi->wfi.n_waves = ov_streams (&fi->ofile);
  fi->wfi.waves = g_malloc0 (sizeof (fi->wfi.waves[0]) * fi->wfi.n_waves);
  for (i = 0; i < fi->wfi.n_waves; i++)
    {
      vorbis_comment *vc = ov_comment (&fi->ofile, i);
      guint n;

      for (n = 0; n < vc->comments; n++)
	if (strcmp (vc->user_comments[n], "title=") == 0)
	  break;
      if (n < vc->comments)
	fi->wfi.waves[i].name = g_strdup (vc->user_comments[n] + 6);
      else
	fi->wfi.waves[i].name = g_strdup_printf ("Unnamed-%u", i);
    }

  return &fi->wfi;
}

static void
oggv_free_file_info (gpointer         data,
		     GslWaveFileInfo *file_info)
{
  FileInfo *fi = (FileInfo*) file_info;
  guint i;

  for (i = 0; i < fi->wfi.n_waves; i++)
    g_free (fi->wfi.waves[i].name);
  g_free (fi->wfi.waves);
  ov_clear (&fi->ofile);
  gsl_delete_struct (FileInfo, fi);
}

static GslWaveDsc*
oggv_load_wave_dsc (gpointer         data,
		    GslWaveFileInfo *file_info,
		    guint            nth_wave,
		    GslErrorType    *error_p)
{
  FileInfo *fi = (FileInfo*) file_info;
  GslWaveDsc *wdsc = gsl_new_struct0 (GslWaveDsc, 1);
  vorbis_info *vi = ov_info (&fi->ofile, nth_wave);

  wdsc->name = g_strdup (fi->wfi.waves[nth_wave].name);
  wdsc->n_channels = vi->channels;
  wdsc->n_chunks = 1;
  wdsc->chunks = g_new0 (GslWaveChunkDsc, 1);
  wdsc->chunks[0].osc_freq = 440.0; /* FIXME */
  wdsc->chunks[0].mix_freq = vi->rate;
  wdsc->chunks[0].loader_offset = nth_wave;	/* lbitstream */

  return wdsc;
}

static void
oggv_free_wave_dsc (gpointer    data,
		    GslWaveDsc *wdsc)
{
  g_free (wdsc->name);
  g_free (wdsc->chunks);
  gsl_delete_struct (GslWaveDsc, wdsc);
}

static GslDataHandle*
oggv_create_chunk_handle (gpointer      data,
			  GslWaveDsc   *wdsc,
			  guint         nth_chunk,
			  GslErrorType *error_p)
{
  FileInfo *fi = (FileInfo*) wdsc->file_info;
  GslDataHandle *dhandle;

  g_return_val_if_fail (nth_chunk == 0, NULL);

  dhandle = gsl_data_handle_new_ogg_vorbis (fi->wfi.file_name,
					    wdsc->chunks[0].loader_offset); /* lbitstream */
  if (!dhandle)
    *error_p = GSL_ERROR_OPEN_FAILED;
  return dhandle;
}

void
_gsl_init_loader_oggvorbis (void)
{
  static const gchar *file_exts[] = { "ogg", NULL, };
  static const gchar *mime_types[] = { "application/x-ogg", "audio/x-vorbis", "audio/x-ogg", NULL, };
  static const gchar *magics[] = { "0 string OggS\n" "29 string vorbis", NULL, };
  static GslLoader loader = {
    "Ogg/Vorbis",
    file_exts,
    mime_types,
    magics,
    0,  /* priority */
    NULL,
    oggv_load_file_info,
    oggv_free_file_info,
    oggv_load_wave_dsc,
    oggv_free_wave_dsc,
    oggv_create_chunk_handle,
  };
  static gboolean initialized = FALSE;

  g_assert (initialized == FALSE);
  initialized = TRUE;

  gsl_loader_register (&loader);
}
#else
void
_gsl_init_loader_oggvorbis (void)
{
  /* dummy */
}
#endif /* GSL_HAVE_OGGVORBIS */