xref: /haiku/src/tests/kits/media/soundplayer/SoundPlayerTest.cpp (revision c123bb99360546118e849c5da57eda81a152d62c)
1 #include <Application.h>
2 #include <SoundPlayer.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include "OS.h"
7 
8 #define NAMESPACE
9 //#define NAMESPACE	BExperimental::
10 
11 #define FILENAME "/boot/home/test1.wav"
12 
13 sem_id finished = -1;
14 int fd = -1;
15 NAMESPACE BSoundPlayer *sp = 0;
16 
abs(float f)17 inline float abs(float f) { return f < 0 ? -f : f; }
18 
PlayBuffer(void * cookie,void * buffer,size_t size,const media_raw_audio_format & format)19 void PlayBuffer(void *cookie, void * buffer, size_t size, const media_raw_audio_format & format)
20 {
21 	if (size != (size_t)read(fd, buffer, size)) {
22 		sp->SetHasData(false);
23 		release_sem(finished);
24 	}
25 }
26 
main(int argc,char * argv[])27 int main(int argc, char *argv[])
28 {
29 	fd = open((argc > 1) ? argv[1] : FILENAME, O_RDONLY);
30 	if (fd < 0)
31 		return -1;
32 
33 	lseek(fd, 44, SEEK_SET); // skip wav header
34 
35 	new BApplication("application/x-vnd.SoundPlayTest");
36 	finished = create_sem(0, "finish wait");
37 
38 	media_raw_audio_format format;
39 	format = media_raw_audio_format::wildcard;
40 	format.frame_rate = 44100;
41 	format.channel_count = 2;
42 	format.format = media_raw_audio_format::B_AUDIO_SHORT;
43 	format.byte_order = B_MEDIA_LITTLE_ENDIAN;
44 	format.buffer_size = 4 * 4096;
45 
46 
47 	sp = new NAMESPACE BSoundPlayer(&format, "sound player test", PlayBuffer);
48 
49 	printf("playing soundfile\n");
50 	sp->SetHasData(true);
51 	sp->Start();
52 
53 	printf("volume now %.2f\n", sp->Volume());
54 	printf("volumeDB now %.2f\n", sp->VolumeDB());
55 
56 	float f;
57 	media_node out_node;
58 	int32 out_parameter;
59 	float out_min_dB;
60 	float out_max_dB;
61 
62 	sp->GetVolumeInfo(&out_node, &out_parameter, &out_min_dB, &out_max_dB);
63 
64 	printf("out_min_dB %.2f\n", out_min_dB);
65 	printf("out_max_dB %.2f\n", out_max_dB);
66 
67 	/* Initial volume is not unrelyable! sometimes 0, 0.4, 1.0 and 3.2 have been observed */
68 	sp->SetVolume(1.0f);
69 
70 	printf("waiting 5 seconds\n");
71 	snooze(5000000);
72 
73 	printf("skipping 10MB now\n");
74 	lseek(fd, 10000000, SEEK_CUR); // skip 10 MB
75 
76 	printf("waiting 3 seconds");
77 	snooze(3000000);
78 
79 	do {
80 		printf("\nDoing %% ramping...");
81 		printf("\nramping down\n");
82 		for (f = 1.0f; f >= 0.0; f -= ((f < 0.08) ? 0.005 : 0.05)) {
83 			sp->SetVolume(f);
84 			printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bvolume now %.2f, should be %.2f", sp->Volume(), f);
85 			fflush(stdout);
86 			snooze(250000);
87 		}
88 
89 		if (B_OK == acquire_sem_etc(finished,1, B_TIMEOUT, 0))
90 			break;
91 
92 		printf("\nramping up\n");
93 		for (f = 0; f <= 1.0f; f += ((f < 0.08) ? 0.005 : 0.05)) {
94 			sp->SetVolume(f);
95 			printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bvolume now %.2f, should be %.2f", sp->Volume(), f);
96 			fflush(stdout);
97 			snooze(250000);
98 		}
99 
100 		if (B_OK == acquire_sem_etc(finished,1, B_TIMEOUT, 0))
101 			break;
102 
103 		printf("\nDoing DB ramping...");
104 
105 		printf("\nramping down\n");
106 		for (f = out_max_dB; f >= out_min_dB; f -= abs(out_max_dB - out_min_dB) / 50) {
107 			sp->SetVolumeDB(f);
108 			printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bvolume now %6.2f, should be %6.2f", sp->VolumeDB(), f);
109 			fflush(stdout);
110 			snooze(250000);
111 		}
112 
113 		if (B_OK == acquire_sem_etc(finished,1, B_TIMEOUT, 0))
114 			break;
115 
116 		printf("\nramping up\n");
117 		for (f = out_min_dB; f <= out_max_dB; f += abs(out_max_dB - out_min_dB) / 50) {
118 			sp->SetVolumeDB(f);
119 			printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bvolume now %+6.2f, should be %6.2f", sp->VolumeDB(), f);
120 			fflush(stdout);
121 			snooze(250000);
122 		}
123 	} while (B_OK != acquire_sem_etc(finished,1, B_TIMEOUT, 0));
124 
125 	printf("\nplayback finished\n");
126 
127 	delete sp;
128 
129 	close(fd);
130 }
131