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