File: timidity.c

package info (click to toggle)
moc 1%3A2.6.0~svn-r3005-7
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,904 kB
  • sloc: ansic: 31,748; sh: 929; cpp: 487; makefile: 240
file content (239 lines) | stat: -rw-r--r-- 5,542 bytes parent folder | download | duplicates (5)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * MOC - music on console
 * Copyright (C) 2004 Damian Pietras <daper@daper.net>
 *
 * libTiMidity-plugin Copyright (C) 2007 Hendrik Iben <hiben@tzi.de>
 * The hard work is done by the libTiMidity-Library written by
 * Konstantin Korikov (http://libtimidity.sourceforge.net).
 * I have merely hacked together a wrapper...
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <strings.h>
#include <assert.h>
#include <timidity.h>

#define DEBUG

#include "common.h"
#include "io.h"
#include "decoder.h"
#include "log.h"
#include "files.h"
#include "options.h"

MidSongOptions midioptions;

struct timidity_data
{
  MidSong *midisong;
  int length;
  struct decoder_error error;
};

static struct timidity_data *make_timidity_data(const char *file) {
  struct timidity_data *data;

  data = (struct timidity_data *)xmalloc (sizeof(struct timidity_data));

  data->midisong = NULL;
  decoder_error_init (&data->error);

  MidIStream *midistream = mid_istream_open_file(file);

  if(midistream==NULL) {
    decoder_error(&data->error, ERROR_FATAL, 0,
                  "Can't open midifile: %s", file);
    return data;
  }

  data->midisong = mid_song_load(midistream, &midioptions);
  mid_istream_close(midistream);

  if(data->midisong==NULL) {
    decoder_error(&data->error, ERROR_FATAL, 0,
                  "Can't load midifile: %s", file);
    return data;
  }

  return data;
}

static void *timidity_open (const char *file)
{
  struct timidity_data *data = make_timidity_data(file);

  if(data->midisong) {
    data->length = mid_song_get_total_time(data->midisong);
  }


  if(data->midisong) {
    debug ("Opened file %s", file);

    mid_song_set_volume(data->midisong, options_get_int("TiMidity_Volume"));
    mid_song_start(data->midisong);
  }

  return data;
}

static void timidity_close (void *void_data)
{
  struct timidity_data *data = (struct timidity_data *)void_data;

  if (data->midisong) {
    mid_song_free(data->midisong);
  }

  decoder_error_clear (&data->error);
  free (data);
}

static void timidity_info (const char *file_name, struct file_tags *info,
		const int tags_sel)
{
  struct timidity_data *data = make_timidity_data(file_name);

  if(data->midisong==NULL) {
    free (data);
    return;
  }

  if(tags_sel & TAGS_TIME) {
    info->time = mid_song_get_total_time(data->midisong) / 1000;
    info->filled |= TAGS_TIME;
  }

  timidity_close(data);
}

static int timidity_seek (void *void_data, int sec)
{
  struct timidity_data *data = (struct timidity_data *)void_data;

  assert (sec >= 0);

  int ms = sec*1000;

  ms = MIN(ms,data->length);

  mid_song_seek(data->midisong, ms);

  return ms/1000;
}

static int timidity_decode (void *void_data, char *buf, int buf_len,
		struct sound_params *sound_params)
{
  struct timidity_data *data = (struct timidity_data *)void_data;

  sound_params->channels = midioptions.channels;
  sound_params->rate = midioptions.rate;
  sound_params->fmt = (midioptions.format==MID_AUDIO_S16LSB)?(SFMT_S16 | SFMT_LE):SFMT_S8;

  return mid_song_read_wave(data->midisong, (void *)buf, buf_len);
}

static int timidity_get_bitrate (void *unused ATTR_UNUSED)
{
  return -1;
}

static int timidity_get_duration (void *void_data)
{
  struct timidity_data *data = (struct timidity_data *)void_data;
  return data->length/1000;
}

static void timidity_get_name (const char *unused ATTR_UNUSED, char buf[4])
{
  strcpy (buf, "MID");
}

static int timidity_our_format_ext(const char *ext)
{
  return !strcasecmp (ext, "MID");
}

static int timidity_our_format_mime (const char *mime)
{
  return !strcasecmp(mime, "audio/midi")
      || !strncasecmp(mime, "audio/midi;", 10);
}

static void timidity_get_error (void *prv_data, struct decoder_error *error)
{
  struct timidity_data *data = (struct timidity_data *)prv_data;

  decoder_error_copy (error, &data->error);
}

static void timidity_destroy()
{
  mid_exit();
}

static struct decoder timidity_decoder =
{
  DECODER_API_VERSION,
  NULL,
  timidity_destroy,
  timidity_open,
  NULL,
  NULL,
  timidity_close,
  timidity_decode,
  timidity_seek,
  timidity_info,
  timidity_get_bitrate,
  timidity_get_duration,
  timidity_get_error,
  timidity_our_format_ext,
  timidity_our_format_mime,
  timidity_get_name,
  NULL,
  NULL,
  NULL
};

struct decoder *plugin_init ()
{
  char *config;
  int initresult;

  config = options_get_str("TiMidity_Config");
  if (config == NULL || strcasecmp(config, "yes") == 0)
    initresult = mid_init(NULL);
  else if (strcasecmp(config, "no") == 0)
    initresult = mid_init_no_config();
  else
    initresult = mid_init(config);

  // Is there a better way to signal failed init?
  // The decoder-init-function may not return errors AFAIK...
  if(initresult < 0)
  {
    if (config == NULL || strcasecmp(config, "yes") == 0)
      config = "<default>";
    fatal("TiMidity-Plugin: Error processing TiMidity-Configuration!\n"
          "                              Configuration file is: %s", config);
  }

  midioptions.rate = options_get_int("TiMidity_Rate");
  midioptions.format = (options_get_int("TiMidity_Bits")==16)?MID_AUDIO_S16LSB:MID_AUDIO_S8;
  midioptions.channels = options_get_int("TiMidity_Channels");
  midioptions.buffer_size = midioptions.rate;

  return &timidity_decoder;
}