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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
/*
TiMidity++ -- MIDI to WAVE converter and player
Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
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.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
wave_audio.c
Functions to output RIFF WAVE format data to a file or stdout.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#ifdef __W32__
#include <io.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef STDC_HEADERS
#include <string.h>
#include <stdlib.h>
#elif HAVE_STRINGS_H
#include <strings.h>
#endif
#include <fcntl.h>
#ifdef __FreeBSD__
#include <stdio.h>
#endif
#include <ctype.h>
#include "timidity.h"
#include "common.h"
#include "output.h"
#include "controls.h"
#include "instrum.h"
#include "playmidi.h"
#include "readmidi.h"
static int open_output(void); /* 0=success, 1=warning, -1=fatal error */
static void close_output(void);
static int output_data(char *buf, int32 bytes);
static int acntl(int request, void *arg);
/* export the playback mode */
#define dpm wave_play_mode
PlayMode dpm = {
DEFAULT_RATE,
#ifdef LITTLE_ENDIAN
PE_16BIT|PE_SIGNED,
#else
PE_16BIT|PE_SIGNED|PE_BYTESWAP,
#endif
PF_PCM_STREAM,
-1,
{0,0,0,0,0},
"RIFF WAVE file", 'w',
NULL,
open_output,
close_output,
output_data,
acntl
};
#define UPDATE_HEADER_STEP (128*1024) /* 128KB */
static uint32 bytes_output, next_bytes;
static int already_warning_lseek;
#ifdef __W32G__
extern char *w32g_output_dir;
extern int w32g_auto_output_mode;
#endif
/*************************************************************************/
static char *orig_RIFFheader=
"RIFF" "\377\377\377\377"
"WAVE" "fmt " "\020\000\000\000" "\001\000"
/* 22: channels */ "\001\000"
/* 24: frequency */ "xxxx"
/* 28: bytes/second */ "xxxx"
/* 32: bytes/sample */ "\004\000"
/* 34: bits/sample */ "\020\000"
"data" "\377\377\377\377"
;
/* We support follows WAVE format:
* 8 bit unsigned pcm
* 16 bit signed pcm (little endian)
* A-law
* U-law
*/
/* Windows WAVE File Encoding Tags */
#define WAVE_FORMAT_PCM 0x01
#define WAVE_FORMAT_ALAW 0x06
#define WAVE_FORMAT_MULAW 0x07
static int wav_output_open(const char *fname)
{
int t;
char RIFFheader[44];
int fd;
if(strcmp(fname, "-") == 0)
fd = 1; /* data to stdout */
else {
/* Open the audio file */
fd = open(fname, FILE_OUTPUT_MODE);
if(fd < 0) {
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s",
fname, strerror(errno));
return -1;
}
}
/* Generate a (rather non-standard) RIFF header. We don't know yet
what the block lengths will be. We'll fix that at close if this
is a seekable file. */
memcpy(RIFFheader, orig_RIFFheader, 44);
if(dpm.encoding & PE_ALAW)
RIFFheader[20] = WAVE_FORMAT_ALAW;
else if(dpm.encoding & PE_ULAW)
RIFFheader[20] = WAVE_FORMAT_MULAW;
else
RIFFheader[20] = WAVE_FORMAT_PCM;
if(dpm.encoding & PE_MONO)
RIFFheader[22] = 1;
else
RIFFheader[22] = 2;
*((int *)(RIFFheader+24)) = LE_LONG(dpm.rate);
t = dpm.rate;
if(!(dpm.encoding & PE_MONO)) t *= 2;
if(dpm.encoding & PE_24BIT) t *= 3;
else if(dpm.encoding & PE_16BIT) t *= 2;
*((int *)(RIFFheader+28)) = LE_LONG(t);
if(dpm.encoding & PE_16BIT)
t = 2;
else if(dpm.encoding & PE_24BIT)
t = 3;
else
t = 1;
RIFFheader[34] = t * 8;
if(!(dpm.encoding & PE_MONO))
t *= 2;
RIFFheader[32] = t;
if(write(fd, RIFFheader, 44) == -1) {
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: write: %s",
dpm.name, strerror(errno));
close_output();
return -1;
}
/* Reset the length counter */
bytes_output = 0;
next_bytes = bytes_output + UPDATE_HEADER_STEP;
already_warning_lseek = 0;
return fd;
}
static int auto_wav_output_open(const char *input_filename)
{
char *output_filename;
#ifndef __W32G__
output_filename = create_auto_output_name(input_filename,"wav",NULL,0);
#else
output_filename = create_auto_output_name(input_filename,"wav",w32g_output_dir,w32g_auto_output_mode);
#endif
if(output_filename==NULL){
return -1;
}
if((dpm.fd = wav_output_open(output_filename)) == -1) {
free(output_filename);
return -1;
}
if(dpm.name != NULL)
free(dpm.name);
dpm.name = output_filename;
ctl->cmsg(CMSG_INFO, VERB_NORMAL, "Output %s", dpm.name);
return 0;
}
static int open_output(void)
{
int include_enc, exclude_enc;
include_enc = exclude_enc = 0;
if(dpm.encoding & (PE_16BIT | PE_24BIT))
{
#ifdef LITTLE_ENDIAN
exclude_enc = PE_BYTESWAP;
#else
include_enc = PE_BYTESWAP;
#endif /* LITTLE_ENDIAN */
include_enc |= PE_SIGNED;
}
else if(!(dpm.encoding & (PE_ULAW|PE_ALAW)))
{
exclude_enc = PE_SIGNED;
}
dpm.encoding = validate_encoding(dpm.encoding, include_enc, exclude_enc);
#ifndef __W32G__
if(dpm.name == NULL) {
dpm.flag |= PF_AUTO_SPLIT_FILE;
dpm.name = NULL;
} else {
dpm.flag &= ~PF_AUTO_SPLIT_FILE;
if((dpm.fd = wav_output_open(dpm.name)) == -1)
return -1;
}
#else
if(w32g_auto_output_mode>0){
dpm.flag |= PF_AUTO_SPLIT_FILE;
dpm.name = NULL;
} else {
dpm.flag &= ~PF_AUTO_SPLIT_FILE;
if((dpm.fd = wav_output_open(dpm.name)) == -1)
return -1;
}
#endif
return 0;
}
static int update_header(void)
{
off_t save_point;
int32 tmp;
if(already_warning_lseek)
return 0;
save_point = lseek(dpm.fd, 0, SEEK_CUR);
if(save_point == -1 || lseek(dpm.fd, 4, SEEK_SET) == -1)
{
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
"Warning: %s: %s: Can't make valid header",
dpm.name, strerror(errno));
already_warning_lseek = 1;
return 0;
}
tmp = LE_LONG(bytes_output + 44 - 8);
if(write(dpm.fd, &tmp, 4) == -1)
{
lseek(dpm.fd, save_point, SEEK_SET);
return -1;
}
lseek(dpm.fd, 40, SEEK_SET);
tmp = LE_LONG(bytes_output);
ssize_t dummy = write(dpm.fd, &tmp, 4); ++dummy;
lseek(dpm.fd, save_point, SEEK_SET);
ctl->cmsg(CMSG_INFO, VERB_DEBUG,
"%s: Update RIFF WAVE header (size=%d)", dpm.name, bytes_output);
return 0;
}
static int output_data(char *buf, int32 bytes)
{
int n;
if(dpm.fd == -1)
return -1;
while(((n = write(dpm.fd, buf, bytes)) == -1) && errno == EINTR)
;
if(n == -1)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s",
dpm.name, strerror(errno));
return -1;
}
bytes_output += bytes;
#if UPDATE_HEADER_STEP > 0
if(bytes_output >= next_bytes)
{
if(update_header() == -1)
return -1;
next_bytes = bytes_output + UPDATE_HEADER_STEP;
}
#endif /* UPDATE_HEADER_STEP */
return n;
}
static void close_output(void)
{
if(dpm.fd != 1 && /* We don't close stdout */
dpm.fd != -1)
{
update_header();
close(dpm.fd);
dpm.fd = -1;
}
}
static int acntl(int request, void *arg)
{
switch(request) {
case PM_REQ_PLAY_START:
if(dpm.flag & PF_AUTO_SPLIT_FILE)
return auto_wav_output_open(current_file_info->filename);
break;
case PM_REQ_PLAY_END:
if(dpm.flag & PF_AUTO_SPLIT_FILE) {
close_output();
return 0;
}
break;
case PM_REQ_DISCARD:
return 0;
}
return -1;
}
|