-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw_a.cpp
165 lines (137 loc) · 3.51 KB
/
raw_a.cpp
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
/*
TiMidity -- Experimental MIDI to WAVE converter
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.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.
raw_audio.c
Functions to output raw sound data to a file or stdout.
*/
#ifdef __WIN32__
#include <stdlib.h>
#include <io.h>
#include <string.h>
#else
#include <unistd.h>
#endif
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include "config.h"
#include "output.h"
#include "controls.h"
#if !defined(OPEN_MODE)
#ifdef __WIN32__
#define OPEN_MODE O_WRONLY | O_CREAT | O_TRUNC | O_BINARY
#else
#define OPEN_MODE O_WRONLY | O_CREAT | O_TRUNC
#endif
#endif
static int open_output(void); /* 0=success, 1=warning, -1=fatal error */
static void close_output(void);
static void output_data(int32 *buf, uint32 count);
static int driver_output_data(unsigned char *buf, uint32 count);
static void flush_output(void);
static void purge_output(void);
static int output_count(uint32 ct);
/* export the playback mode */
#undef dpm
#define dpm raw_play_mode
PlayMode dpm = {
DEFAULT_RATE, PE_16BIT|PE_SIGNED,
-1,
{0,0,0,0,0},
"raw waveform data", 'r',
"output.raw",
open_output,
close_output,
output_data,
driver_output_data,
flush_output,
purge_output,
output_count
};
/*************************************************************************/
static int open_output(void)
{
if (dpm.encoding & PE_ULAW)
dpm.encoding &= ~PE_16BIT;
if (!(dpm.encoding & PE_16BIT))
dpm.encoding &= ~PE_BYTESWAP;
if (dpm.name && dpm.name[0]=='-' && dpm.name[1]=='\0')
dpm.fd=1; /* data to stdout */
else
{
/* Open the audio file */
dpm.fd=open(dpm.name, OPEN_MODE, 0644);
if (dpm.fd<0)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s",
dpm.name, sys_errlist[errno]);
return -1;
}
}
return 0;
}
static int output_count(uint32 ct)
{
return (int)ct;
}
static int driver_output_data(unsigned char *buf, uint32 count)
{
return count;
}
static void output_data(int32 *buf, uint32 count)
{
if (!(dpm.encoding & PE_MONO)) count*=2; /* Stereo samples */
if (dpm.encoding & PE_16BIT)
{
if (dpm.encoding & PE_BYTESWAP)
{
if (dpm.encoding & PE_SIGNED)
s32tos16x(buf, count);
else
s32tou16x(buf, count);
}
else
{
if (dpm.encoding & PE_SIGNED)
s32tos16(buf, count);
else
s32tou16(buf, count);
}
while ((-1==write(dpm.fd, buf, count * 2)) && errno==EINTR)
;
}
else
{
if (dpm.encoding & PE_ULAW)
{
s32toulaw(buf, count);
}
else
{
if (dpm.encoding & PE_SIGNED)
s32tos8(buf, count);
else
s32tou8(buf, count);
}
while ((-1==write(dpm.fd, buf, count)) && errno==EINTR)
;
}
}
static void close_output(void)
{
if (dpm.fd != 1) /* We don't close stdout */
close(dpm.fd);
}
static void flush_output(void) { }
static void purge_output(void) { }