xref: /haiku/src/tests/kits/game/push_game_sound_test/push_game_sound_triangle.cpp (revision 478ea251528764ed0c8e1a968734f62b6bba6ef2)
1 /*
2  * Copyright 2009, Krzysztof Ćwiertnia (krzysiek.bmkx_gmail_com).
3  * Copyright 2011, Adrien Destugues (pulkomandy@pulkomandy.ath.cx)
4  * All Rights Reserved. Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include <GameSoundDefs.h>
13 #include <InterfaceDefs.h>
14 #include <PushGameSound.h>
15 #include <View.h>
16 
17 
18 int
main(int argc,char * argv[])19 main(int argc, char *argv[])
20 {
21 	// 256 frames * 4 buffer parts * 2 channels * 2 bytes per sample
22 	// will give us internal buffer of 4096 bytes
23 	size_t framesPerBufferPart = 256;
24 	size_t bufferPartCount = 4;
25 
26 	if (argc != 1 && argc != 3) {
27 		printf("Usage: %s [<frames per part> <parts>]\n",
28 			argv[0]);
29 		return 0;
30 	}
31 
32 	if (argc == 3) {
33 		size_t size = strtoul(argv[1], NULL, 10);
34 		if (size > 0)
35 			framesPerBufferPart = size;
36 
37 		size = strtoul(argv[2], NULL,  10);
38 		if (size == 1) {
39 			printf("at least 2 buffer parts are needed\n");
40 			return 1;
41 		}
42 		if (size > 0)
43 			bufferPartCount = size;
44 	}
45 
46 	printf("frames per buffer part: %ld\n", framesPerBufferPart);
47 	printf("buffer part count: %ld\n", bufferPartCount);
48 
49 	gs_audio_format gsFormat;
50 	memset(&gsFormat, 0, sizeof(gsFormat));
51 	gsFormat.frame_rate = 44100;
52 	gsFormat.channel_count = 1;
53 	gsFormat.format = gs_audio_format::B_GS_U8;
54 
55 	BPushGameSound pushGameSound(framesPerBufferPart, &gsFormat,
56 		bufferPartCount);
57 	if (pushGameSound.InitCheck() != B_OK) {
58 		printf("trouble initializing push game sound: %s\n",
59 			strerror(pushGameSound.InitCheck()));
60 		return 1;
61 	}
62 
63 	uint8 *buffer;
64 	size_t bufferSize;
65 	if (pushGameSound.LockForCyclic((void **)&buffer, &bufferSize)
66 			!= BPushGameSound::lock_ok) {
67 		printf("cannot lock buffer\n");
68 		return 1;
69 	}
70 
71 	for (int i = 0; i < bufferSize; i++)
72 		buffer[i] = i;
73 
74 	if (pushGameSound.StartPlaying() != B_OK) {
75 		printf("cannot start playback\n");
76 		return 1;
77 	}
78 
79 	getchar();
80 
81 	pushGameSound.StopPlaying();
82 
83 	printf("\nfinished.\n");
84 
85 	return 0;
86 }
87