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