1 /* aylet 0.4, a .AY music file player.
2 * Copyright (C) 2001-2004 Russell Marks and Ian Collier. See main.c for licence.
3 *
4 * drv_obsd.c - (Rough) native audio driver for OpenBSD (by Chris Cox).
5 */
6
7 #ifdef DRIVER_OPENBSD
8
9 #include <sys/types.h>
10 #include <sys/ioctl.h>
11 #include <string.h>
12 #include <sys/audioio.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include "sound.h"
16
17 #include "driver.h"
18
19
driver_init(int * freqptr,int * stereoptr)20 int driver_init (int *freqptr, int *stereoptr)
21 {
22 audio_info_t ainfo;
23
24 if (play_to_stdout)
25 {
26 soundfd = 1;
27 return 1;
28 }
29
30 if ((soundfd = open ("/dev/sound", O_WRONLY, 0)) < 0)
31 return 0;
32
33 AUDIO_INITINFO (&ainfo);
34
35 /* TODO: hackish. */
36 ainfo.play.encoding = AUDIO_ENCODING_SLINEAR;
37 ainfo.play.channels = (*stereoptr) ? 2 : 1;
38 ainfo.play.precision = (sixteenbit ? 16 : 8);
39 ainfo.play.sample_rate = (*freqptr);
40
41 if ((ioctl (soundfd, AUDIO_SETINFO, &ainfo)) < 0)
42 {
43 sixteenbit = 0;
44 return 0;
45 }
46 else
47 sixteenbit = 1;
48
49 return 1;
50 }
51
driver_end(void)52 void driver_end (void)
53 {
54 if (soundfd > 1)
55 close (soundfd);
56 }
57
58
driver_frame(signed short * data,int len)59 void driver_frame(signed short *data,int len)
60 {
61 static unsigned char buf8[4096];
62 unsigned char *data8=(unsigned char *)data;
63 int ret=0,ofs=0;
64
65 len<<=1; /* now in bytes */
66
67 if(!sixteenbit)
68 {
69 signed short *src;
70 unsigned char *dst;
71 int f;
72
73 src=data; dst=buf8;
74 len>>=1;
75 for(f=0;f<len;f++)
76 *dst++=128+(int)((*src++)/256);
77
78 data8=buf8;
79 }
80
81 while(len)
82 {
83 ret=write(soundfd,data8+ofs,len);
84 if(ret>0)
85 ofs+=ret,len-=ret;
86 }
87 }
88
89 #endif /* DRIVER_OPENBSD */
90