summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/util/audio/audioIO_Linux.cpp
blob: 5ca9231cfd88b544ed9307a081acec6b79e9327e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* this file is a part of amp software, (C) tomislav uzelac 1996,1997

	Origional code by: tomislav uzelac
	Modified by:
	* Dan Nelson - BSD mods.
	* Andrew Richards - moved code from audio.c and added mixer support etc
        * Martin Vogt
 */

/* Support for Linux and BSD sound devices */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include "audioIO.h"
#include <stdlib.h>
#include <stdio.h>

//
// Why all these different system cannot make a standard where this
// soundcard.h file is ?
//
#if defined(HAVE_SYS_SOUNDCARD_H)
	#undef AUSIZ
	#undef HAVE_SYS_SOUNDCARD_H
	#include <sys/soundcard.h>
#elif defined(HAVE_MACHINE_SOUNDCARD_H) 
	#undef AUSIZ
	#include <machine/soundcard.h>
#elif defined(__NetBSD__)
	#undef AUSIZ
	#include <soundcard.h>
#else
	// fallback:
	#include <linux/soundcard.h>
#endif


/* optimal fragment size */

int AUSIZ = 0;

// declare these static to effectively isolate the audio device 

static int audio_fd;
static int mixer_fd;
static int volumeIoctl;



int audioConstruct() {
  audio_fd=-1;
  mixer_fd=-1;
  return true;
}


void audioDestruct() {
  
}



/* 
   should open the audio device, perform any special initialization	
*/
int  audioOpen() {
  audio_fd = open ("/dev/dsp", O_WRONLY, 0);
  if (audio_fd < 0)  {
    perror("Unable to open the audio");
  }

  // Ok here something important if your programm forks:
  if (audio_fd > 0) {
    if (fcntl(audio_fd,F_SETFD,true) < 0) {
      perror("fcntl socket");exit(1);
    }
  }
  
  return (audio_fd > 0);
}

inline void audioFlush() {
  if (ioctl(audio_fd, SNDCTL_DSP_RESET, 0) == -1)
    perror("Unable to reset audio device\n");
}

/* 
   should close the audio device and perform any special shutdown 
*/
void audioClose() {
  audioFlush();
  if (close(audio_fd) < 0) {
    perror("error close audiodevice:");
  }
}


void audioInit(int sampleSize,int frequency, int stereo, int sign, int big) {
  if( sign == 0 )
  {
      fprintf(stderr,
	      "%s, %d: expecting signed audio data, "
	      "initialized unsigned (ignored)\n",
	      __FILE__, __LINE__ );
  }
  if( big != 0 )
  {
      fprintf(stderr,
	      "%s, %d: expecting little endian audio data, "
	      "initialized big endian (ignored)\n",
	      __FILE__, __LINE__ );
  }

  int play_format=AFMT_S16_LE;

  if (sampleSize == 8) {
    play_format=AFMT_S8;
  }
  ioctl(audio_fd,SNDCTL_DSP_RESET,NULL);
  
  if (ioctl(audio_fd, SNDCTL_DSP_SETFMT,&play_format) < 0) {
    perror("Unable to set required audio format\n");
  }
  
  /* Set 1 or 2 channels */
  stereo=(stereo ? 1 : 0);

  if (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) < 0) {
    perror("Unable to set stereo/mono\n");
    exit(0);
  }
  
  /* Set the output frequency */
  if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &frequency) < 0) {
    perror("Unable to set frequency");
    exit(0);
  }
  
  if (ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &AUSIZ) == -1) {
    perror("Unable to get fragment size\n");
    exit(0);
  }
}


int getAudioBufferSize() {
  struct audio_buf_info buf_info;
  int buf=1024*65;
  if (ioctl(audio_fd,SNDCTL_DSP_GETOSPACE,&buf_info) == -1) {
    perror("ioctl getAudioBufferSize using default");
  } else {
    buf=buf_info.bytes;
  }
  return buf;
}


int mixerOpen() {
  int supportedMixers;

  if ((mixer_fd=open("/dev/mixer",O_RDWR)) == -1) {
    perror("Unable to open mixer device");
  }
  
  // Ok here something important if your programm forks:
  if (mixer_fd > 0) {
    if (fcntl(mixer_fd,F_SETFD,true) < 0) {
      perror("fcntl socket");exit(1);
    }
  }
  
  if (ioctl(mixer_fd, SOUND_MIXER_READ_DEVMASK, &supportedMixers) == -1){
    perror("Unable to get mixer info assuming master volume");
    volumeIoctl=SOUND_MIXER_WRITE_VOLUME;
  } else {
    if ((supportedMixers & SOUND_MASK_PCM) != 0)
      volumeIoctl=SOUND_MIXER_WRITE_PCM;
    else
      volumeIoctl=0;
  }

  return (mixer_fd > 0);
}


void mixerClose() {
  if (mixer_fd != -1) {
    close(mixer_fd);
  }
}

/* 
   only code this if your system can change the volume while 
   playing
*/
void mixerSetVolume(int leftVolume,int rightVolume) {
  int volume;

  volume=leftVolume+(rightVolume<<8);
  if ((mixer_fd != -1) && (volumeIoctl!=0)) {
    if (ioctl(mixer_fd, volumeIoctl, &volume) < 0) {
      perror("Unable to set sound volume");
    }
  }
}



int audioWrite(char *buffer, int count) {
  return(write(audio_fd,buffer,count));
}


int getAudioFd() {
  return(audio_fd);
}