Use configured linker to link the kernel
[AROS.git] / test / playoss.c
blob8c24d3490c15381d59891663140722c5f77bee27
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 #include <stdio.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <string.h>
16 #include <dos/dos.h>
17 #include <proto/oss.h>
18 #include <proto/exec.h>
19 #include <proto/dos.h>
21 #define DSP_DRIVER_NAME "/dev/dsp"
23 #define BUFFERSIZE 0x57300
25 static struct Library *OSSBase;
27 static void cleanup(void *buffer, BPTR infh, BOOL closeoss)
29 if(closeoss) OSS_Close();
30 if(OSSBase) CloseLibrary(OSSBase);
31 if(infh) Close(infh);
32 free(buffer);
36 int main (int argc, char *argv[])
38 BPTR infh;
39 char *path;
40 int format, channels, rate;
41 int readlength, written, ok;
42 void *buffer, *bufptr;
44 OSSBase = NULL;
45 format = 0; //AFMT_S16_LE;
46 channels = 2;
47 rate = 44100;
48 if (argc == 5) {
49 format = atoi(argv[2]);
50 channels = atoi(argv[3]);
51 rate = atoi(argv[4]);
52 } else if (argc != 2) {
53 printf ("Usage:\nplayoss <filename> [<format> <channels> <rate>]\n");
54 return -1;
57 buffer = malloc(BUFFERSIZE);
58 if (!buffer) {
59 printf ("malloc error\n");
60 return -1;
63 path = argv[1];
64 infh = Open(path, MODE_OLDFILE);
65 if (infh == 0) {
66 printf ("error %ld opening file %s\n", IoErr(), path);
67 cleanup(buffer, NULL, FALSE);
68 return -1;
71 OSSBase = OpenLibrary("oss.library", 0);
72 if (!OSSBase)
74 printf("Could not open oss.library!\n");
75 cleanup(buffer, infh, FALSE);
76 return -1;
79 ok = OSS_Open(DSP_DRIVER_NAME, FALSE, TRUE, TRUE);
80 if (!ok) {
81 printf ("error opening DSP\n");
82 cleanup(buffer, infh, FALSE);
83 return -1;
86 if( !OSS_SetFormat_S16LE() ) {
87 printf("error setting format\n");
88 cleanup(buffer, infh, TRUE);
89 return -1;
92 if( !OSS_SetNumChannels(channels) ) {
93 printf("error setting channels\n");
94 cleanup(buffer, infh, TRUE);
95 return -1;
98 if( !OSS_SetWriteRate(rate, &rate) ) {
99 printf("error setting write rate\n");
100 cleanup(buffer, infh, TRUE);
101 return -1;
103 printf ("File: %s\nFormat: %d\nChannels: %d\nRate: %d\n", path, format, channels, rate);
105 while( (readlength = Read(infh, buffer, BUFFERSIZE)) > 0 ) {
106 // printf("read %d\n", readlength);
107 bufptr = buffer;
108 do {
109 written = OSS_Write(bufptr, readlength);
110 // printf("written %d\n", written);
111 if( written < 0 ) {
112 printf ("error writing audio %d\n", written);
113 cleanup(buffer, infh, TRUE);
114 return -1;
117 bufptr += written;
118 readlength -= written;
119 } while( readlength > 0 );
122 if( readlength < 0 ) {
123 printf ("error %ld reading file\n", IoErr());
124 cleanup(buffer, infh, TRUE);
125 return -1;
128 cleanup(buffer, infh, TRUE);
129 return 0;