Home | History | Annotate | Download | only in include
      1 #ifndef ESD_H
      2 #define ESD_H
      3 #include <audiofile.h>
      4 
      5 #ifdef __cplusplus
      6 extern "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 */
     38 enum 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 
    117 typedef int esd_format_t;
    118 typedef int esd_proto_t;
    119 
    120 /*******************************************************************/
    121 /* client side API for playing sounds */
    122 
    123 typedef 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) */
    132 int esd_open_sound( const char *host );
    133 
    134 /* send the authorization cookie, create one if needed */
    135 int esd_send_auth( int sock );
    136 
    137 /* lock/unlock will disable/enable foreign clients from connecting */
    138 int esd_lock( int esd );
    139 int esd_unlock( int esd );
    140 
    141 /* standby/resume will free/reclaim audio device so others may use it */
    142 int esd_standby( int esd );
    143 int 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 */
    147 int esd_play_stream( esd_format_t format, int rate,
    148 		     const char *host, const char *name );
    149 int esd_play_stream_fallback( esd_format_t format, int rate,
    150 			      const char *host, const char *name );
    151 int 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 ); */
    154 int esd_record_stream( esd_format_t format, int rate,
    155 		       const char *host, const char *name );
    156 int esd_record_stream_fallback( esd_format_t format, int rate,
    157 				const char *host, const char *name );
    158 int 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 */
    162 int esd_sample_cache( int esd, esd_format_t format, const int rate,
    163 		      const int length, const char *name );
    164 int esd_confirm_sample_cache( int esd );
    165 
    166 /* get the sample id for an already-cached sample */
    167 int esd_sample_getid( int esd, const char *name);
    168 
    169 /* uncache a sample in the server */
    170 int esd_sample_free( int esd, int sample );
    171 
    172 /* play a cached sample once */
    173 int esd_sample_play( int esd, int sample );
    174 /* make a cached sample loop */
    175 int esd_sample_loop( int esd, int sample );
    176 
    177 /* stop the looping sample at end */
    178 int esd_sample_stop( int esd, int sample );
    179 /* stop a playing sample immed. */
    180 int esd_sample_kill( int esd, int sample );
    181 
    182 /* closes fd, previously obtained by esd_open */
    183 int 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.                                           */
    192 int 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 */
    199 typedef 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 
    207 typedef 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 
    222 typedef 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 
    238 typedef 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 
    246 enum esd_standby_mode {
    247     ESM_ERROR, ESM_ON_STANDBY, ESM_ON_AUTOSTANDBY, ESM_RUNNING
    248 };
    249 typedef 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 */
    253 typedef int esd_new_player_callback_t( esd_player_info_t * );
    254 /* what to do when a stream disconnects, or sample stops playing */
    255 typedef int esd_old_player_callback_t( esd_player_info_t * );
    256 /* what to do when a sample is cached */
    257 typedef int esd_new_sample_callback_t( esd_sample_info_t * );
    258 /* what to do when a sample is uncached */
    259 typedef int esd_old_sample_callback_t( esd_sample_info_t * );
    260 
    261 typedef 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 */
    269 void esd_print_server_info( esd_server_info_t *server_info );
    270 void esd_print_player_info( esd_player_info_t *player_info );
    271 void esd_print_sample_info( esd_sample_info_t *sample_info );
    272 /* print all info to stdout */
    273 void esd_print_all_info( esd_info_t *all_info );
    274 
    275 /* retrieve server properties (sample rate, format, version number) */
    276 esd_server_info_t *esd_get_server_info( int esd );
    277 /* release all memory allocated for the server properties structure */
    278 void esd_free_server_info( esd_server_info_t *server_info );
    279 
    280 /* retrieve all information from server */
    281 esd_info_t *esd_get_all_info( int esd );
    282 
    283 /* retrieve all information from server, and update until unsubsribed or closed */
    284 esd_info_t *esd_subscribe_all_info( int esd );
    285 
    286 /* call to update the info structure with new information, and call callbacks */
    287 esd_info_t *esd_update_info( int esd, esd_info_t *info,
    288 			     esd_update_info_callbacks_t *callbacks );
    289 esd_info_t *esd_unsubscribe_info( int esd );
    290 
    291 /* release all memory allocated for the esd info structure */
    292 void esd_free_all_info( esd_info_t *info );
    293 
    294 
    295 /* reset the volume panning for a stream */
    296 int 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 */
    300 int 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 */
    304 esd_standby_mode_t esd_get_standby_mode( int esd );
    305 
    306 
    307 /*******************************************************************/
    308 /* esdfile.c - audiofile wrappers for sane handling of files */
    309 
    310 int esd_send_file( int esd, AFfilehandle au_file, int frame_length );
    311 int esd_play_file( const char *name_prefix, const char *filename, int fallback );
    312 int 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 */
    317 extern esd_format_t esd_audio_format;
    318 extern int esd_audio_rate;
    319 extern char *esd_audio_device;
    320 
    321 const char *esd_audio_devices( void );
    322 int esd_audio_open( void );
    323 void esd_audio_close( void );
    324 void esd_audio_pause( void );
    325 int esd_audio_write( void *buffer, int buf_size );
    326 int esd_audio_read( void *buffer, int buf_size );
    327 void esd_audio_flush( void );
    328 
    329 /******************************************************************/
    330 /* util.c utilities						  */
    331 
    332 const char *esd_get_socket_dirname( void );
    333 const char *esd_get_socket_name( void );
    334 
    335 int have_ipv6( void );
    336 
    337 #ifdef __cplusplus
    338 }
    339 #endif
    340 
    341 
    342 #endif /* #ifndef ESD_H */
    343