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
|
#include <esd.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
/*******************************************************************/
/* esdfile.c - audiofile wrappers for sane handling of files */
int esd_send_file( int esd, AFfilehandle au_file, int frame_length )
{
/* data for transfer */
char buf[ ESD_BUF_SIZE ];
int frames_read;
int buf_frames = ESD_BUF_SIZE / frame_length;
while ( ( frames_read = afReadFrames( au_file, AF_DEFAULT_TRACK,
buf, buf_frames ) ) )
{
if ( write ( esd, buf, frames_read * frame_length ) <= 0)
return 1;
}
/* diagnostic info */
/*
if ( getenv( "ESDBG" ) )
printf( "esound sending file\n" );
*/
return 0;
}
int esd_play_file( const char *name_prefix, const char *filename, int fallback )
{
/* input from libaudiofile... */
AFfilehandle in_file;
int in_format, in_width, in_channels, frame_count;
int in_compression;
double in_rate;
int bytes_per_frame;
/* output to esound... */
int out_sock, out_bits, out_channels, out_rate;
int out_mode = ESD_STREAM, out_func = ESD_PLAY;
esd_format_t out_format;
char name[ ESD_NAME_MAX ] = "";
/* diagnostic info */
/*
if ( getenv( "ESDBG" ) )
printf( "esound playing file\n" );
*/
/* open the audio file */
in_file = afOpenFile( filename, "rb", NULL );
if ( !in_file )
return 0;
/* get audio file parameters */
frame_count = afGetFrameCount( in_file, AF_DEFAULT_TRACK );
in_channels = afGetChannels( in_file, AF_DEFAULT_TRACK );
in_rate = afGetRate( in_file, AF_DEFAULT_TRACK );
in_compression = afGetCompression ( in_file, AF_DEFAULT_TRACK );
afGetSampleFormat( in_file, AF_DEFAULT_TRACK, &in_format, &in_width );
if(getenv("ESDBG"))
printf ("frames: %i channels: %i rate: %f format: %i width: %i\n",
frame_count, in_channels, in_rate, in_format, in_width);
/* This check was added because audiofile doesn't tell us whether or
* not it will play compressed audio correctly. */
if ( in_compression != AF_COMPRESSION_NONE &&
in_compression != AF_COMPRESSION_G711_ULAW &&
in_compression != AF_COMPRESSION_G711_ALAW &&
in_compression != AF_COMPRESSION_IMA &&
in_compression != AF_COMPRESSION_MS_ADPCM)
{
/* fputs ("compressed audio not supported supported\n", stderr); */
return 0;
}
/* convert audiofile parameters to EsounD parameters */
if ( in_width == 8 )
out_bits = ESD_BITS8;
else if ( in_width == 16 )
out_bits = ESD_BITS16;
else
{
/* fputs ("only sample widths of 8 and 16 supported\n", stderr); */
return 0;
}
bytes_per_frame = ( in_width * in_channels ) / 8;
if ( in_channels == 1 )
out_channels = ESD_MONO;
else if ( in_channels == 2 )
out_channels = ESD_STEREO;
else
{
/* fputs ("only 1 or 2 channel samples supported\n", stderr); */
return 0;
}
out_format = out_bits | out_channels | out_mode | out_func;
out_rate = (int) in_rate;
/* construct name */
if ( name_prefix ) {
strncpy( name, name_prefix, ESD_NAME_MAX - 2 );
strcat( name, ":" );
}
strncpy( name + strlen( name ), filename, ESD_NAME_MAX - strlen( name ) );
/* connect to server and play stream */
if ( fallback )
out_sock = esd_play_stream_fallback( out_format, out_rate, NULL, name );
else
out_sock = esd_play_stream( out_format, out_rate, NULL, filename );
if ( out_sock <= 0 )
return 0;
/* play */
esd_send_file( out_sock, in_file, bytes_per_frame );
/* close up and go home */
close( out_sock );
if ( afCloseFile ( in_file ) )
return 0;
/* diagnostic info */
/*
if ( getenv( "ESDBG" ) )
printf( "esound played file\n" );
*/
return 1;
}
int esd_file_cache( int esd, const char *name_prefix, const char *filename )
{
/* input from libaudiofile... */
AFfilehandle in_file;
int in_format, in_width, in_channels, frame_count;
double in_rate;
int bytes_per_frame;
/* output to esound... */
int out_bits, out_channels, out_rate;
int out_mode = ESD_STREAM, out_func = ESD_PLAY;
esd_format_t out_format;
int length, sample_id, confirm_id;
char name[ ESD_NAME_MAX ];
/* diagnostic info */
/*
if ( getenv( "ESDBG" ) )
printf( "esound caching file\n" );
*/
/* open the audio file */
in_file = afOpenFile( filename, "rb", NULL );
if ( !in_file )
return -1;
/* get audio file parameters */
frame_count = afGetFrameCount( in_file, AF_DEFAULT_TRACK );
in_channels = afGetChannels( in_file, AF_DEFAULT_TRACK );
in_rate = afGetRate( in_file, AF_DEFAULT_TRACK );
length = afGetTrackBytes( in_file, AF_DEFAULT_TRACK );
afGetSampleFormat( in_file, AF_DEFAULT_TRACK, &in_format, &in_width );
/* printf ("frames: %i channels: %i rate: %f format: %i width: %i\n",
* frame_count, in_channels, in_rate, in_format, in_width);
*/
/* convert audiofile parameters to EsounD parameters */
if ( in_width == 8 )
out_bits = ESD_BITS8;
else if ( in_width == 16 )
out_bits = ESD_BITS16;
else
{
/* fputs ("only sample widths of 8 and 16 supported\n", stderr); */
return -1;
}
bytes_per_frame = ( in_width * in_channels ) / 8;
if ( in_channels == 1 )
out_channels = ESD_MONO;
else if ( in_channels == 2 )
out_channels = ESD_STEREO;
else
{
/* fputs ("only 1 or 2 channel samples supported\n", stderr); */
return -1;
}
out_format = out_bits | out_channels | out_mode | out_func;
out_rate = (int) in_rate;
/* construct name */
if ( name_prefix ) {
strncpy( name, name_prefix, ESD_NAME_MAX - 2 );
strcat( name, ":" );
}
strncpy( name + strlen( name ), filename, ESD_NAME_MAX - strlen( name ) );
/* connect to server and play stream */
sample_id = esd_sample_cache( esd, out_format, out_rate,
length, name );
/* play */
esd_send_file( esd, in_file, bytes_per_frame );
/* close up and go home */
if ( afCloseFile ( in_file ) )
return -1;
confirm_id = esd_confirm_sample_cache( esd );
if ( confirm_id != sample_id )
return -1;
/* diagnostic info */
/*
if ( getenv( "ESDBG" ) )
printf( "esound cached file\n" );
*/
return sample_id;
}
|