summaryrefslogtreecommitdiff
path: root/programs/sndfile-play-beos.cpp
blob: fb3fde4cdceae8d24fd52ea090e021882f523c05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
** Copyright (C) 2001 Marcus Overhagen <marcus@overhagen.de>
**  
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** 
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software 
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include	<stdio.h>

#include 	<Application.h>
#include 	<SoundPlayer.h>
#include 	<string.h>

#include	<sndfile.h>

#define	BUFFER_LEN		1024

/*------------------------------------------------------------------------------
**	BeOS functions for playing a sound.
*/

#if defined (__BEOS__)

struct shared_data
{
	BSoundPlayer *player;
	SNDFILE *sndfile;
	SF_INFO sfinfo;	
	sem_id finished;
};

static void 
buffer_callback(void *theCookie, void *buf, size_t size, const media_raw_audio_format &format) 
{
	shared_data *data = (shared_data *)theCookie;
	short *buffer = (short *)buf;
	int count = size / sizeof(short);
	int m, readcount;

	if (!data->player->HasData())
		return;

	readcount = sf_read_short(data->sndfile, buffer, count);
	if (readcount == 0) 
	{	data->player->SetHasData(false);		
		release_sem(data->finished);
		}
	if (readcount < count) 
	{	for (m = readcount ; m < count ; m++)
			buffer [m] = 0 ;
		}
	if (data->sfinfo.pcmbitwidth < 16) 
	{	for (m = 0 ; m < count ; m++)
			buffer [m] *= 256 ;
		}
}

static void
beos_play (int argc, char *argv [])
{
	shared_data data;
	status_t status;
	int	k;

	/* BSoundPlayer requires a BApplication object */
	BApplication app("application/x-vnd.MarcusOverhagen-sfplay");

	for (k = 1 ; k < argc ; k++)
	{	printf ("Playing %s\n", argv [k]) ;	
		if (! (data.sndfile = sf_open_read (argv [k], &data.sfinfo)))
		{	sf_perror (NULL) ;
			continue ;
			} ;
			
		if (data.sfinfo.channels < 1 || data.sfinfo.channels > 2)
		{	printf ("Error : channels = %d.\n", data.sfinfo.channels) ;
			sf_close (data.sndfile) ;
			continue ;
			} ;		

		data.finished = create_sem(0,"finished");			

		media_raw_audio_format format = 
		{ 	data.sfinfo.samplerate,
			data.sfinfo.channels,
			media_raw_audio_format::B_AUDIO_SHORT,
			B_HOST_IS_LENDIAN ? B_MEDIA_LITTLE_ENDIAN : B_MEDIA_BIG_ENDIAN,
			BUFFER_LEN * sizeof(short)
			};

		BSoundPlayer player(&format,"player",buffer_callback,NULL,&data);
		data.player = &player;
		
		if ((status = player.InitCheck()) != B_OK) 
		{
			printf ("Error : BSoundPlayer init failed, %s.\n", strerror(status)) ;
			delete_sem(data.finished);
			sf_close (data.sndfile) ;
			continue ;
			}

		player.SetVolume(1.0);
		player.Start();
		player.SetHasData(true);
		acquire_sem(data.finished);
		player.Stop();
		delete_sem(data.finished);
		
		sf_close (data.sndfile) ;

		} ;

} /* beos_play */

#endif

/*==============================================================================
**	Main function.
*/

int 
main (int argc, char *argv [])
{
	if (argc < 2)
	{	printf ("Usage : %s <input sound file>\n\n", argv [0]) ;
		return 1 ;
		} ;
	
	beos_play (argc, argv) ;

	return 0 ;
} /* main */