1#ifndef ESD_H
2#define ESD_H
3#include <audiofile.h>
4
5#ifdef __cplusplus
6extern "C" {
7#endif
8
9/* path and name of the default EsounD domain socket */
10#define ESD_UNIX_SOCKET_DIR	esd_get_socket_dirname()
11#define ESD_UNIX_SOCKET_NAME	esd_get_socket_name()
12
13/* size of the audio buffer */
14#define ESD_BUF_SIZE (4 * 1024)
15/* maximum size we can write().  Otherwise we might overflow */
16#define ESD_MAX_WRITE_SIZE (21 * 4096)
17
18/* length of the authorization key, octets */
19#define ESD_KEY_LEN (16)
20
21/* default port for the EsounD server */
22#define ESD_DEFAULT_PORT (16001)
23
24/* default sample rate for the EsounD server */
25#define ESD_DEFAULT_RATE (44100)
26
27/* maximum length of a stream/sample name */
28#define ESD_NAME_MAX (128)
29
30/* a magic number to identify the relative endianness of a client */
31#define ESD_ENDIAN_KEY \
32	( (unsigned int) ( ('E' << 24) + ('N' << 16) + ('D' << 8) + ('N') ) )
33
34#define ESD_VOLUME_BASE (256)
35
36/*************************************/
37/* what can we do to/with the EsounD */
38enum esd_proto {
39    ESD_PROTO_CONNECT,      /* implied on inital client connection */
40
41    /* pseudo "security" functionality */
42    ESD_PROTO_LOCK,	    /* disable "foreign" client connections */
43    ESD_PROTO_UNLOCK,	    /* enable "foreign" client connections */
44
45    /* stream functionality: play, record, monitor */
46    ESD_PROTO_STREAM_PLAY,  /* play all following data as a stream */
47    ESD_PROTO_STREAM_REC,   /* record data from card as a stream */
48    ESD_PROTO_STREAM_MON,   /* send mixed buffer output as a stream */
49
50    /* sample functionality: cache, free, play, loop, EOL, kill */
51    ESD_PROTO_SAMPLE_CACHE, /* cache a sample in the server */
52    ESD_PROTO_SAMPLE_FREE,  /* release a sample in the server */
53    ESD_PROTO_SAMPLE_PLAY,  /* play a cached sample */
54    ESD_PROTO_SAMPLE_LOOP,  /* loop a cached sample, til eoloop */
55    ESD_PROTO_SAMPLE_STOP,  /* stop a looping sample when done */
56    ESD_PROTO_SAMPLE_KILL,  /* stop the looping sample immed. */
57
58    /* free and reclaim /dev/dsp functionality */
59    ESD_PROTO_STANDBY,	    /* release /dev/dsp and ignore all data */
60    ESD_PROTO_RESUME,	    /* reclaim /dev/dsp and play sounds again */
61
62    /* TODO: move these to a more logical place. NOTE: will break the protocol */
63    ESD_PROTO_SAMPLE_GETID, /* get the ID for an already-cached sample */
64    ESD_PROTO_STREAM_FILT,  /* filter mixed buffer output as a stream */
65
66    /* esd remote management */
67    ESD_PROTO_SERVER_INFO,  /* get server info (ver, sample rate, format) */
68    ESD_PROTO_ALL_INFO,     /* get all info (server info, players, samples) */
69    ESD_PROTO_SUBSCRIBE,    /* track new and removed players and samples */
70    ESD_PROTO_UNSUBSCRIBE,  /* stop tracking updates */
71
72    /* esd remote control */
73    ESD_PROTO_STREAM_PAN,   /* set stream panning */
74    ESD_PROTO_SAMPLE_PAN,   /* set default sample panning */
75
76    /* esd status */
77    ESD_PROTO_STANDBY_MODE, /* see if server is in standby, autostandby, etc */
78
79    /* esd latency */
80    ESD_PROTO_LATENCY,      /* retrieve latency between write()'s and output */
81
82    ESD_PROTO_MAX           /* for bounds checking */
83};
84
85
86/******************/
87/* The EsounD api */
88
89/* the properties of a sound buffer are logically or'd */
90
91/* bits of stream/sample data */
92#define ESD_MASK_BITS	( 0x000F )
93#define ESD_BITS8 	( 0x0000 )
94#define ESD_BITS16	( 0x0001 )
95
96/* how many interleaved channels of data */
97#define ESD_MASK_CHAN	( 0x00F0 )
98#define ESD_MONO	( 0x0010 )
99#define ESD_STEREO	( 0x0020 )
100
101/* whether it's a stream or a sample */
102#define ESD_MASK_MODE	( 0x0F00 )
103#define ESD_STREAM	( 0x0000 )
104#define ESD_SAMPLE	( 0x0100 )
105#define ESD_ADPCM	( 0x0200 )	/* TODO: anyone up for this? =P */
106
107/* the function of the stream/sample, and common functions */
108#define ESD_MASK_FUNC	( 0xF000 )
109#define ESD_PLAY	( 0x1000 )
110/* functions for streams only */
111#define ESD_MONITOR	( 0x0000 )
112#define ESD_RECORD	( 0x2000 )
113/* functions for samples only */
114#define ESD_STOP	( 0x0000 )
115#define ESD_LOOP	( 0x2000 )
116
117typedef int esd_format_t;
118typedef int esd_proto_t;
119
120/*******************************************************************/
121/* client side API for playing sounds */
122
123typedef unsigned char octet;
124
125/*******************************************************************/
126/* esdlib.c - basic esd client interface functions */
127
128/* opens channel, authenticates connection, and prefares for protos */
129/* returns EsounD socket for communication, result < 0 = error */
130/* server = listen socket (localhost:5001, 192.168.168.0:9999 */
131/* rate, format = (bits | channels | stream | func) */
132int esd_open_sound( const char *host );
133
134/* send the authorization cookie, create one if needed */
135int esd_send_auth( int sock );
136
137/* lock/unlock will disable/enable foreign clients from connecting */
138int esd_lock( int esd );
139int esd_unlock( int esd );
140
141/* standby/resume will free/reclaim audio device so others may use it */
142int esd_standby( int esd );
143int esd_resume( int esd );
144
145/* open a socket for playing, monitoring, or recording as a stream */
146/* the *_fallback functions try to open /dev/dsp if there's no EsounD */
147int esd_play_stream( esd_format_t format, int rate,
148		     const char *host, const char *name );
149int esd_play_stream_fallback( esd_format_t format, int rate,
150			      const char *host, const char *name );
151int esd_monitor_stream( esd_format_t format, int rate,
152			const char *host, const char *name );
153/* int esd_monitor_stream_fallback( esd_format_t format, int rate ); */
154int esd_record_stream( esd_format_t format, int rate,
155		       const char *host, const char *name );
156int esd_record_stream_fallback( esd_format_t format, int rate,
157				const char *host, const char *name );
158int esd_filter_stream( esd_format_t format, int rate,
159		       const char *host, const char *name );
160
161/* cache a sample in the server returns sample id, < 0 = error */
162int esd_sample_cache( int esd, esd_format_t format, const int rate,
163		      const int length, const char *name );
164int esd_confirm_sample_cache( int esd );
165
166/* get the sample id for an already-cached sample */
167int esd_sample_getid( int esd, const char *name);
168
169/* uncache a sample in the server */
170int esd_sample_free( int esd, int sample );
171
172/* play a cached sample once */
173int esd_sample_play( int esd, int sample );
174/* make a cached sample loop */
175int esd_sample_loop( int esd, int sample );
176
177/* stop the looping sample at end */
178int esd_sample_stop( int esd, int sample );
179/* stop a playing sample immed. */
180int esd_sample_kill( int esd, int sample );
181
182/* closes fd, previously obtained by esd_open */
183int esd_close( int esd );
184
185/* get the stream latency to esound (latency is number of samples  */
186/* at 44.1khz stereo 16 bit - you'll have to adjust if oyur input  */
187/* sampling rate is less (in bytes per second)                     */
188/* so if you're at 44.1khz stereo 16bit in your stream - your lag  */
189/* in bytes woudl be lag * 2 * 2 bytes (2 for stereo, 2 for 16bit) */
190/* if your stream is at 22.05 Khz it'll be double this - in mono   */
191/* double again ... etc.                                           */
192int esd_get_latency(int esd);
193
194
195/*******************************************************************/
196/* esdmgr.c - functions to implement a "sound manager" for esd */
197
198/* structures to retrieve information about streams/samples from the server */
199typedef struct esd_server_info {
200
201    int version; 		/* server version encoded as an int */
202    esd_format_t format;	/* magic int with the format info */
203    int rate;			/* sample rate */
204
205} esd_server_info_t;
206
207typedef struct esd_player_info {
208
209    struct esd_player_info *next; /* point to next entry in list */
210    esd_server_info_t *server;	/* the server that contains this stream */
211
212    int source_id;		/* either a stream fd or sample id */
213    char name[ ESD_NAME_MAX ];	/* name of stream for remote control */
214    int rate;			/* sample rate */
215    int left_vol_scale;		/* volume scaling */
216    int right_vol_scale;
217
218    esd_format_t format;	/* magic int with the format info */
219
220} esd_player_info_t;
221
222typedef struct esd_sample_info {
223
224    struct esd_sample_info *next; /* point to next entry in list */
225    esd_server_info_t *server;	/* the server that contains this sample */
226
227    int sample_id;		/* either a stream fd or sample id */
228    char name[ ESD_NAME_MAX ];	/* name of stream for remote control */
229    int rate;			/* sample rate */
230    int left_vol_scale;		/* volume scaling */
231    int right_vol_scale;
232
233    esd_format_t format;	/* magic int with the format info */
234    int length;			/* total buffer length */
235
236} esd_sample_info_t;
237
238typedef struct esd_info {
239
240    esd_server_info_t *server;
241    esd_player_info_t *player_list;
242    esd_sample_info_t *sample_list;
243
244} esd_info_t;
245
246enum esd_standby_mode {
247    ESM_ERROR, ESM_ON_STANDBY, ESM_ON_AUTOSTANDBY, ESM_RUNNING
248};
249typedef int esd_standby_mode_t;
250
251/* define callbacks for esd_update_info() */
252/* what to do when a stream connects, or sample is played */
253typedef int esd_new_player_callback_t( esd_player_info_t * );
254/* what to do when a stream disconnects, or sample stops playing */
255typedef int esd_old_player_callback_t( esd_player_info_t * );
256/* what to do when a sample is cached */
257typedef int esd_new_sample_callback_t( esd_sample_info_t * );
258/* what to do when a sample is uncached */
259typedef int esd_old_sample_callback_t( esd_sample_info_t * );
260
261typedef struct esd_update_info_callbacks {
262    esd_new_player_callback_t *esd_new_player_callback;
263    esd_old_player_callback_t *esd_old_player_callback;
264    esd_new_sample_callback_t *esd_new_sample_callback;
265    esd_old_sample_callback_t *esd_old_sample_callback;
266} esd_update_info_callbacks_t;
267
268/* print server into to stdout */
269void esd_print_server_info( esd_server_info_t *server_info );
270void esd_print_player_info( esd_player_info_t *player_info );
271void esd_print_sample_info( esd_sample_info_t *sample_info );
272/* print all info to stdout */
273void esd_print_all_info( esd_info_t *all_info );
274
275/* retrieve server properties (sample rate, format, version number) */
276esd_server_info_t *esd_get_server_info( int esd );
277/* release all memory allocated for the server properties structure */
278void esd_free_server_info( esd_server_info_t *server_info );
279
280/* retrieve all information from server */
281esd_info_t *esd_get_all_info( int esd );
282
283/* retrieve all information from server, and update until unsubsribed or closed */
284esd_info_t *esd_subscribe_all_info( int esd );
285
286/* call to update the info structure with new information, and call callbacks */
287esd_info_t *esd_update_info( int esd, esd_info_t *info,
288			     esd_update_info_callbacks_t *callbacks );
289esd_info_t *esd_unsubscribe_info( int esd );
290
291/* release all memory allocated for the esd info structure */
292void esd_free_all_info( esd_info_t *info );
293
294
295/* reset the volume panning for a stream */
296int esd_set_stream_pan( int esd, int stream_id,
297			int left_scale, int right_scale );
298
299/* reset the default volume panning for a sample */
300int esd_set_default_sample_pan( int esd, int sample_id,
301				int left_scale, int right_scale );
302
303/* see if the server is in stnaby, autostandby, etc */
304esd_standby_mode_t esd_get_standby_mode( int esd );
305
306
307/*******************************************************************/
308/* esdfile.c - audiofile wrappers for sane handling of files */
309
310int esd_send_file( int esd, AFfilehandle au_file, int frame_length );
311int esd_play_file( const char *name_prefix, const char *filename, int fallback );
312int esd_file_cache( int esd, const char *name_prefix, const char *filename );
313
314
315/*******************************************************************/
316/* audio.c - abstract the sound hardware for cross platform usage */
317extern esd_format_t esd_audio_format;
318extern int esd_audio_rate;
319extern char *esd_audio_device;
320
321const char *esd_audio_devices( void );
322int esd_audio_open( void );
323void esd_audio_close( void );
324void esd_audio_pause( void );
325int esd_audio_write( void *buffer, int buf_size );
326int esd_audio_read( void *buffer, int buf_size );
327void esd_audio_flush( void );
328
329/******************************************************************/
330/* util.c utilities						  */
331
332const char *esd_get_socket_dirname( void );
333const char *esd_get_socket_name( void );
334
335int have_ipv6( void );
336
337#ifdef __cplusplus
338}
339#endif
340
341
342#endif /* #ifndef ESD_H */
343