xref: /haiku/src/tests/kits/media/soundplayer/SimplePlayerTest.cpp (revision 579f1dbca962a2a03df54f69fdc6e9423f91f20e)
1 //! From BeBook examples.
2 
3 
4 #include <Application.h>
5 #include <Sound.h>
6 #include <SoundPlayer.h>
7 
8 
9 typedef struct cookie_record {
10 	float value;
11 	float direction;
12 } cookie_record;
13 
14 
15 void
16 BufferProc(void* _cookie, void* buffer, size_t size,
17 	const media_raw_audio_format& format)
18 {
19 	// We're going to be cheap and only work for floating-point audio
20 
21 	if (format.format != media_raw_audio_format::B_AUDIO_FLOAT)
22 		return;
23 
24 	// Now fill the buffer with sound!
25 
26 	cookie_record* cookie = (cookie_record*)_cookie;
27 	uint32 channelCount = format.channel_count;
28 	size_t floatSize = size / 4;
29 	float* buf = (float*)buffer;
30 
31 	for (size_t i = 0; i < floatSize; i += channelCount) {
32 		for (size_t j = 0; j < channelCount; j++) {
33 			buf[i + j] = cookie->value;
34 		}
35 
36 		if (cookie->direction == 1.0 && cookie->value >= 1.0)
37 			cookie->direction = -1.0;
38 		else if (cookie->direction == -1.0 && cookie->value <= -1.0)
39 			cookie->direction = 1.0;
40 
41 		cookie->value += cookie->direction * (1.0 / 64.0);
42 	}
43 }
44 
45 
46 int
47 main()
48 {
49 	BApplication app("application/dzwiek");
50 
51 	cookie_record cookie;
52 
53 	cookie.value = 0.0;
54 	cookie.direction = 1.0;
55 
56 	BSoundPlayer player("wave_player", BufferProc, NULL, &cookie);
57 	player.Start();
58 	player.SetHasData(true);
59 
60 	sleep(5);
61 
62 	player.Stop();
63 }
64