1 | #include "esd.h" |
---|
2 | |
---|
3 | #include <stdio.h> |
---|
4 | #include <stdlib.h> |
---|
5 | #include <unistd.h> |
---|
6 | #include <dirent.h> |
---|
7 | #include <sys/stat.h> |
---|
8 | |
---|
9 | #include <signal.h> |
---|
10 | |
---|
11 | /* prototype(s) */ |
---|
12 | void clean_exit(int signum); |
---|
13 | |
---|
14 | volatile int terminate = 0; /* signal will set this for a clean exit */ |
---|
15 | |
---|
16 | void clean_exit(int signum) { |
---|
17 | fprintf( stderr, "received signal %d: terminating...\n", signum ); |
---|
18 | terminate = 1; |
---|
19 | return; |
---|
20 | } |
---|
21 | |
---|
22 | int main(int argc, char **argv) |
---|
23 | { |
---|
24 | char buf[ESD_BUF_SIZE]; |
---|
25 | int sock = -1, rate = ESD_DEFAULT_RATE; |
---|
26 | int arg = 0, length = 0, total = 0; |
---|
27 | |
---|
28 | int bits = ESD_BITS16, channels = ESD_STEREO; |
---|
29 | int mode = ESD_STREAM, func = ESD_PLAY ; |
---|
30 | esd_format_t format = 0; |
---|
31 | |
---|
32 | int sample_id = 0; |
---|
33 | FILE *source = NULL; |
---|
34 | struct stat source_stats; |
---|
35 | char *host = NULL; |
---|
36 | char *name = NULL; |
---|
37 | |
---|
38 | for ( arg = 1 ; arg < argc ; arg++) |
---|
39 | { |
---|
40 | if (!strcmp("-h",argv[arg])) |
---|
41 | { |
---|
42 | printf("usage:\n\t%s [-s server ] [-b] [-m] [-r freq] < file\n", |
---|
43 | argv[0]); |
---|
44 | exit(0); |
---|
45 | } |
---|
46 | else if ( !strcmp( "-s", argv[ arg ] ) ) |
---|
47 | host = argv[ ++arg ]; |
---|
48 | else if ( !strcmp( "-b", argv[ arg ] ) ) |
---|
49 | bits = ESD_BITS8; |
---|
50 | else if ( !strcmp( "-m", argv[ arg ] ) ) |
---|
51 | channels = ESD_MONO; |
---|
52 | else if ( !strcmp( "-r", argv[ arg ] ) ) |
---|
53 | { |
---|
54 | arg++; |
---|
55 | rate = atoi( argv[ arg ] ); |
---|
56 | } else { |
---|
57 | name = argv[ arg ]; |
---|
58 | source = fopen( name, "r" ); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | signal( SIGINT, clean_exit ); |
---|
63 | |
---|
64 | if ( source == NULL ) { |
---|
65 | fprintf( stderr, "%s, sample file not specified\n", argv[ 0 ] ); |
---|
66 | return -1; |
---|
67 | } |
---|
68 | |
---|
69 | format = bits | channels | mode | func; |
---|
70 | printf( "opening socket, format = 0x%08x at %d Hz\n", |
---|
71 | format, rate ); |
---|
72 | |
---|
73 | sock = esd_open_sound( host ); |
---|
74 | if ( sock <= 0 ) |
---|
75 | return 1; |
---|
76 | |
---|
77 | stat( name, &source_stats ); |
---|
78 | sample_id = esd_sample_cache( sock, format, rate, source_stats.st_size, name ); |
---|
79 | printf( "sample id is <%d>\n", sample_id ); |
---|
80 | |
---|
81 | while ( ( length = fread( buf, 1, ESD_BUF_SIZE, source ) ) > 0 ) |
---|
82 | { |
---|
83 | /* fprintf( stderr, "read %d\n", length ); */ |
---|
84 | if ( ( length = write( sock, buf, length) ) <= 0 ) |
---|
85 | return 1; |
---|
86 | else |
---|
87 | total += length; |
---|
88 | } |
---|
89 | |
---|
90 | printf( "sample uploaded, %d bytes\n", total ); |
---|
91 | esd_sample_loop( sock, sample_id ); |
---|
92 | |
---|
93 | printf( "press <enter> to quit.\n" ); |
---|
94 | getchar(); |
---|
95 | |
---|
96 | /* TODO: make sample_free clean out all playing samples */ |
---|
97 | esd_sample_stop( sock, sample_id ); |
---|
98 | esd_sample_free( sock, sample_id ); |
---|
99 | |
---|
100 | printf( "closing down\n" ); |
---|
101 | close( sock ); |
---|
102 | |
---|
103 | return 0; |
---|
104 | } |
---|