r26351@plastic: rob | 2007-05-09 20:51:33 +1000
[cake.git] / test / playoss.c
bloba6bea5f729dbc3ea082d4d21e2545bcc5154a73d
1 /*
2 Copyright © 1995-2004, The AROS Development Team. All rights reserved.
3 $Id$
5 Demo for oss.library to playback audio samples.
6 It can play raw audio files containing 16 bit signed PCM data in little endian format.
7 Chaning the format isn't possible for now.
8 Default number of channels is 2 (stereo) and default playback rate is 44100 (CD quality).
9 */
11 #define __OSS_NOLIBBASE__
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <string.h>
18 #include <dos/dos.h>
19 #include <proto/oss.h>
20 #include <proto/exec.h>
21 #include <proto/dos.h>
23 #define DSP_DRIVER_NAME "/dev/dsp"
25 #define BUFFERSIZE 0x57300
27 static struct Library *OSSBase;
29 static void cleanup(void *buffer, BPTR infh, BOOL closeoss)
31 if(closeoss) OSS_Close();
32 if(OSSBase) CloseLibrary(OSSBase);
33 if(infh) Close(infh);
34 free(buffer);
38 int main (int argc, char *argv[])
40 BPTR infh;
41 char *path;
42 int format, channels, rate;
43 int readlength, written, ok;
44 void *buffer, *bufptr;
46 OSSBase = NULL;
47 format = 0; //AFMT_S16_LE;
48 channels = 2;
49 rate = 44100;
50 if (argc == 5) {
51 format = atoi(argv[2]);
52 channels = atoi(argv[3]);
53 rate = atoi(argv[4]);
54 } else if (argc != 2) {
55 printf ("Usage:\nplayoss <filename> [<format> <channels> <rate>]\n");
56 return -1;
59 buffer = malloc(BUFFERSIZE);
60 if (!buffer) {
61 printf ("malloc error\n");
62 return -1;
65 path = argv[1];
66 infh = Open(path, MODE_OLDFILE);
67 if (infh == 0) {
68 printf ("error %ld opening file %s\n", IoErr(), path);
69 cleanup(buffer, NULL, FALSE);
70 return -1;
73 OSSBase = OpenLibrary("oss.library", 0);
74 if (!OSSBase)
76 printf("Could not open oss.library!\n");
77 cleanup(buffer, infh, FALSE);
78 return -1;
81 ok = OSS_Open(DSP_DRIVER_NAME, FALSE, TRUE, TRUE);
82 if (!ok) {
83 printf ("error opening DSP\n");
84 cleanup(buffer, infh, FALSE);
85 return -1;
88 if( !OSS_SetFormat_S16LE() ) {
89 printf("error setting format\n");
90 cleanup(buffer, infh, TRUE);
91 return -1;
94 if( !OSS_SetNumChannels(channels) ) {
95 printf("error setting channels\n");
96 cleanup(buffer, infh, TRUE);
97 return -1;
100 if( !OSS_SetWriteRate(rate, &rate) ) {
101 printf("error setting write rate\n");
102 cleanup(buffer, infh, TRUE);
103 return -1;
105 printf ("File: %s\nFormat: %d\nChannels: %d\nRate: %d\n", path, format, channels, rate);
107 while( (readlength = Read(infh, buffer, BUFFERSIZE)) > 0 ) {
108 // printf("read %d\n", readlength);
109 bufptr = buffer;
110 do {
111 written = OSS_Write(bufptr, readlength);
112 // printf("written %d\n", written);
113 if( written < 0 ) {
114 printf ("error writing audio %d\n", written);
115 cleanup(buffer, infh, TRUE);
116 return -1;
119 bufptr += written;
120 readlength -= written;
121 } while( readlength > 0 );
124 if( readlength < 0 ) {
125 printf ("error %ld reading file\n", IoErr());
126 cleanup(buffer, infh, TRUE);
127 return -1;
130 cleanup(buffer, infh, TRUE);
131 return 0;