Chris@0
|
1 /*
|
Chris@0
|
2 ** Copyright (C) 2001 Marcus Overhagen <marcus@overhagen.de>
|
Chris@0
|
3 **
|
Chris@0
|
4 ** This program is free software; you can redistribute it and/or modify
|
Chris@0
|
5 ** it under the terms of the GNU General Public License as published by
|
Chris@0
|
6 ** the Free Software Foundation; either version 2 of the License, or
|
Chris@0
|
7 ** (at your option) any later version.
|
Chris@0
|
8 **
|
Chris@0
|
9 ** This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 ** GNU General Public License for more details.
|
Chris@0
|
13 **
|
Chris@0
|
14 ** You should have received a copy of the GNU General Public License
|
Chris@0
|
15 ** along with this program; if not, write to the Free Software
|
Chris@0
|
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
Chris@0
|
17 */
|
Chris@0
|
18
|
Chris@0
|
19 #include <stdio.h>
|
Chris@0
|
20
|
Chris@0
|
21 #include <Application.h>
|
Chris@0
|
22 #include <SoundPlayer.h>
|
Chris@0
|
23 #include <string.h>
|
Chris@0
|
24
|
Chris@0
|
25 #include <sndfile.h>
|
Chris@0
|
26
|
Chris@0
|
27 #define BUFFER_LEN 1024
|
Chris@0
|
28
|
Chris@0
|
29 /*------------------------------------------------------------------------------
|
Chris@0
|
30 ** BeOS functions for playing a sound.
|
Chris@0
|
31 */
|
Chris@0
|
32
|
Chris@0
|
33 #if defined (__BEOS__)
|
Chris@0
|
34
|
Chris@0
|
35 struct shared_data
|
Chris@0
|
36 {
|
Chris@0
|
37 BSoundPlayer *player;
|
Chris@0
|
38 SNDFILE *sndfile;
|
Chris@0
|
39 SF_INFO sfinfo;
|
Chris@0
|
40 sem_id finished;
|
Chris@0
|
41 };
|
Chris@0
|
42
|
Chris@0
|
43 static void
|
Chris@0
|
44 buffer_callback(void *theCookie, void *buf, size_t size, const media_raw_audio_format &format)
|
Chris@0
|
45 {
|
Chris@0
|
46 shared_data *data = (shared_data *)theCookie;
|
Chris@0
|
47 short *buffer = (short *)buf;
|
Chris@0
|
48 int count = size / sizeof(short);
|
Chris@0
|
49 int m, readcount;
|
Chris@0
|
50
|
Chris@0
|
51 if (!data->player->HasData())
|
Chris@0
|
52 return;
|
Chris@0
|
53
|
Chris@0
|
54 readcount = sf_read_short(data->sndfile, buffer, count);
|
Chris@0
|
55 if (readcount == 0)
|
Chris@0
|
56 { data->player->SetHasData(false);
|
Chris@0
|
57 release_sem(data->finished);
|
Chris@0
|
58 }
|
Chris@0
|
59 if (readcount < count)
|
Chris@0
|
60 { for (m = readcount ; m < count ; m++)
|
Chris@0
|
61 buffer [m] = 0 ;
|
Chris@0
|
62 }
|
Chris@0
|
63 if (data->sfinfo.pcmbitwidth < 16)
|
Chris@0
|
64 { for (m = 0 ; m < count ; m++)
|
Chris@0
|
65 buffer [m] *= 256 ;
|
Chris@0
|
66 }
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 static void
|
Chris@0
|
70 beos_play (int argc, char *argv [])
|
Chris@0
|
71 {
|
Chris@0
|
72 shared_data data;
|
Chris@0
|
73 status_t status;
|
Chris@0
|
74 int k;
|
Chris@0
|
75
|
Chris@0
|
76 /* BSoundPlayer requires a BApplication object */
|
Chris@0
|
77 BApplication app("application/x-vnd.MarcusOverhagen-sfplay");
|
Chris@0
|
78
|
Chris@0
|
79 for (k = 1 ; k < argc ; k++)
|
Chris@0
|
80 { printf ("Playing %s\n", argv [k]) ;
|
Chris@0
|
81 if (! (data.sndfile = sf_open_read (argv [k], &data.sfinfo)))
|
Chris@0
|
82 { sf_perror (NULL) ;
|
Chris@0
|
83 continue ;
|
Chris@0
|
84 } ;
|
Chris@0
|
85
|
Chris@0
|
86 if (data.sfinfo.channels < 1 || data.sfinfo.channels > 2)
|
Chris@0
|
87 { printf ("Error : channels = %d.\n", data.sfinfo.channels) ;
|
Chris@0
|
88 sf_close (data.sndfile) ;
|
Chris@0
|
89 continue ;
|
Chris@0
|
90 } ;
|
Chris@0
|
91
|
Chris@0
|
92 data.finished = create_sem(0,"finished");
|
Chris@0
|
93
|
Chris@0
|
94 media_raw_audio_format format =
|
Chris@0
|
95 { data.sfinfo.samplerate,
|
Chris@0
|
96 data.sfinfo.channels,
|
Chris@0
|
97 media_raw_audio_format::B_AUDIO_SHORT,
|
Chris@0
|
98 B_HOST_IS_LENDIAN ? B_MEDIA_LITTLE_ENDIAN : B_MEDIA_BIG_ENDIAN,
|
Chris@0
|
99 BUFFER_LEN * sizeof(short)
|
Chris@0
|
100 };
|
Chris@0
|
101
|
Chris@0
|
102 BSoundPlayer player(&format,"player",buffer_callback,NULL,&data);
|
Chris@0
|
103 data.player = &player;
|
Chris@0
|
104
|
Chris@0
|
105 if ((status = player.InitCheck()) != B_OK)
|
Chris@0
|
106 {
|
Chris@0
|
107 printf ("Error : BSoundPlayer init failed, %s.\n", strerror(status)) ;
|
Chris@0
|
108 delete_sem(data.finished);
|
Chris@0
|
109 sf_close (data.sndfile) ;
|
Chris@0
|
110 continue ;
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 player.SetVolume(1.0);
|
Chris@0
|
114 player.Start();
|
Chris@0
|
115 player.SetHasData(true);
|
Chris@0
|
116 acquire_sem(data.finished);
|
Chris@0
|
117 player.Stop();
|
Chris@0
|
118 delete_sem(data.finished);
|
Chris@0
|
119
|
Chris@0
|
120 sf_close (data.sndfile) ;
|
Chris@0
|
121
|
Chris@0
|
122 } ;
|
Chris@0
|
123
|
Chris@0
|
124 } /* beos_play */
|
Chris@0
|
125
|
Chris@0
|
126 #endif
|
Chris@0
|
127
|
Chris@0
|
128 /*==============================================================================
|
Chris@0
|
129 ** Main function.
|
Chris@0
|
130 */
|
Chris@0
|
131
|
Chris@0
|
132 int
|
Chris@0
|
133 main (int argc, char *argv [])
|
Chris@0
|
134 {
|
Chris@0
|
135 if (argc < 2)
|
Chris@0
|
136 { printf ("Usage : %s <input sound file>\n\n", argv [0]) ;
|
Chris@0
|
137 return 1 ;
|
Chris@0
|
138 } ;
|
Chris@0
|
139
|
Chris@0
|
140 beos_play (argc, argv) ;
|
Chris@0
|
141
|
Chris@0
|
142 return 0 ;
|
Chris@0
|
143 } /* main */
|
Chris@0
|
144
|