Mercurial > hg > audiostuff
comparison intercom/oss.cpp @ 2:13be24d74cd2
import intercom-0.4.1
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 09:57:52 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:9cadc470e3da | 2:13be24d74cd2 |
---|---|
1 /* oss.cpp | |
2 * | |
3 * Copyright (C) DFS Deutsche Flugsicherung (2004, 2005). | |
4 * All Rights Reserved. | |
5 * | |
6 * Open Sound System functions | |
7 * | |
8 * Version 0.3 | |
9 */ | |
10 | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <string.h> | |
14 #include <math.h> | |
15 | |
16 /* Error handling */ | |
17 #include <assert.h> | |
18 #include <errno.h> | |
19 extern int errno; | |
20 | |
21 /* low level io */ | |
22 #include <sys/types.h> | |
23 #include <sys/stat.h> | |
24 #include <fcntl.h> | |
25 #include <sys/ioctl.h> | |
26 | |
27 /* OSS, see www.4front-tech.com/pguide/index.html */ | |
28 #include <sys/soundcard.h> | |
29 | |
30 #include "oss.h" | |
31 #include "intercomd.h" | |
32 | |
33 int audio_init(char *pathname, int channels_, int mode) | |
34 { | |
35 /* Using OSS is simple in theory. The application just: | |
36 * Opens the device. | |
37 * Optional turns on full duplex | |
38 * Sets fragment size if necessary | |
39 * Sets number of channels, sample format and sampling rate | |
40 * Starts reading and/or writing the device | |
41 * | |
42 * Linux ALSA dmix note: To allow dmix simultaneous playback of | |
43 * different PCM-out, do not open OSS in full duplex mode. Open two file | |
44 * descriptors instead. | |
45 */ | |
46 // fprintf(stderr, "OSS Header SOUND_VERSION = %x\n", SOUND_VERSION); | |
47 | |
48 int audio_fd = open(pathname, mode); | |
49 return_if(audio_fd < 0, -1); | |
50 | |
51 fprintf(stderr, "audio_init() mode = %d\n", mode); | |
52 | |
53 int sound_version = 0; | |
54 ioctl(audio_fd, OSS_GETVERSION, &sound_version); | |
55 // fprintf(stderr, "OSS Driver SOUND_VERSION = %x\n", SOUND_VERSION); | |
56 | |
57 ioctl(audio_fd, SNDCTL_DSP_SETDUPLEX, 0); | |
58 | |
59 /* The 16 most significant bits (MMMM) determine maximum number of | |
60 * fragments. By default the driver computes this based on available | |
61 * buffer space. | |
62 * The minimum value is 2 and the maximum depends on the situation. | |
63 * Set MMMM=0x7fff if you don't want to limit the number of fragments | |
64 */ | |
65 int frag = AUDIOBUFS << 16 | FRAGSIZELD; | |
66 if (2 == channels_) { | |
67 ++frag; // double FRAGSIZE in stereo mode | |
68 } | |
69 int frag_ = frag; | |
70 ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag); | |
71 // fprintf(stderr, "SETFRAGMENT=0x%x\n", frag); | |
72 assert_errno(frag_ == frag); | |
73 | |
74 int format = FORMAT_OSS; | |
75 ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format); | |
76 assert_errno(format == FORMAT_OSS); | |
77 | |
78 int channels = channels_; | |
79 ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels); | |
80 assert_errno(channels_ == channels); | |
81 // fprintf(stderr, "SNDCTL_DSP_CHANNELS=%d\n", channels); | |
82 | |
83 int rate = RATE; | |
84 ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate); | |
85 assert_errno(RATE == rate); | |
86 // fprintf(stderr, "mode=%d SNDCTL_DSP_SPEED=%d\n", mode, rate); | |
87 | |
88 fprintf(stderr, "\n"); | |
89 return audio_fd; | |
90 } |