1 #include <Application.h> 2 #include <stdio.h> 3 #include <unistd.h> 4 #include <fcntl.h> 5 #include <OS.h> 6 7 #define USE_NEW_PLAYER 0 8 9 #if USE_NEW_PLAYER 10 #define NAMESPACE BExperimental:: 11 #include "NewSoundPlayer.h" 12 #else 13 #define NAMESPACE 14 #include <SoundPlayer.h> 15 #endif 16 17 #define FILENAME "/boot/home/test1.wav" 18 #define SIZE 2048 19 20 port_id port = -1; 21 sem_id finished = -1; 22 int fd = -1; 23 NAMESPACE BSoundPlayer *sp = 0; 24 25 inline float abs(float f) { return f < 0 ? -f : f; } 26 27 void PlayBuffer(void *cookie, void * buffer, size_t size, const media_raw_audio_format & format) 28 { 29 size_t portsize = port_buffer_size(port); 30 int32 code; 31 32 read_port(port, &code, buffer, portsize); 33 34 if (size != portsize) { 35 sp->SetHasData(false); 36 release_sem(finished); 37 } 38 } 39 40 int32 filereader(void *arg) 41 { 42 char buffer[SIZE]; 43 int size; 44 45 printf("file reader started\n"); 46 47 for (;;) { 48 size = read(fd, buffer, SIZE); 49 write_port(port, 0, buffer, size); 50 if (size != SIZE) 51 break; 52 } 53 54 write_port(port, 0, buffer, 0); 55 56 printf("file reader finished\n"); 57 58 return 0; 59 } 60 61 int main(int argc, char *argv[]) 62 { 63 fd = open((argc > 1) ? argv[1] : FILENAME, O_RDONLY); 64 if (fd < 0) 65 return -1; 66 67 lseek(fd, 44, SEEK_SET); // skip wav header 68 69 new BApplication("application/x-vnd.playfile"); 70 finished = create_sem(0, "finish wait"); 71 port = create_port(64, "buffer"); 72 73 media_raw_audio_format format; 74 format = media_raw_audio_format::wildcard; 75 format.frame_rate = 44100; 76 format.channel_count = 2; 77 format.format = media_raw_audio_format::B_AUDIO_SHORT; 78 format.byte_order = B_MEDIA_LITTLE_ENDIAN; 79 format.buffer_size = 2048; 80 81 printf("spawning reader thread...\n"); 82 83 resume_thread(spawn_thread(filereader, "filereader", 8, 0)); 84 85 snooze(1000000); 86 87 printf("playing soundfile...\n"); 88 89 sp = new NAMESPACE BSoundPlayer(&format, "sound player test", PlayBuffer); 90 91 sp->SetHasData(true); 92 sp->Start(); 93 94 sp->SetVolume(1.0f); 95 96 // wait for playback end 97 acquire_sem(finished); 98 99 printf("\nplayback finished\n"); 100 101 delete sp; 102 103 close(fd); 104 } 105