~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpeglib/lib/util/audio/audioIO_Linux.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* this file is a part of amp software, (C) tomislav uzelac 1996,1997
 
2
 
 
3
        Origional code by: tomislav uzelac
 
4
        Modified by:
 
5
        * Dan Nelson - BSD mods.
 
6
        * Andrew Richards - moved code from audio.c and added mixer support etc
 
7
        * Martin Vogt
 
8
 */
 
9
 
 
10
/* Support for Linux and BSD sound devices */
 
11
 
 
12
#include <sys/types.h>
 
13
#include <sys/stat.h>
 
14
#include <sys/ioctl.h>
 
15
#include <fcntl.h>
 
16
#include <unistd.h>
 
17
#include <stdio.h>
 
18
#include "audioIO.h"
 
19
#include <stdlib.h>
 
20
#include <stdio.h>
 
21
 
 
22
 
 
23
 
 
24
 
 
25
//
 
26
//
 
27
// Why all these different system cannot make a standard where this
 
28
// soundcard.h file is ?
 
29
//
 
30
 
 
31
 
 
32
 
 
33
#if defined(HAVE_MACHINE_SOUNDCARD_H) 
 
34
#undef AUSIZ
 
35
#include <machine/soundcard.h>
 
36
#else
 
37
 
 
38
#if defined(HAVE_SYS_SOUNDCARD_H)
 
39
#undef AUSIZ
 
40
#include <sys/soundcard.h>
 
41
#else
 
42
// fallback:
 
43
#include <linux/soundcard.h>
 
44
 
 
45
#endif
 
46
#endif
 
47
 
 
48
 
 
49
/* optimal fragment size */
 
50
 
 
51
int AUSIZ = 0;
 
52
 
 
53
// declare these static to effectively isolate the audio device 
 
54
 
 
55
static int audio_fd;
 
56
static int mixer_fd;
 
57
static int volumeIoctl;
 
58
 
 
59
 
 
60
 
 
61
int audioConstruct() {
 
62
  audio_fd=-1;
 
63
  mixer_fd=-1;
 
64
  return true;
 
65
}
 
66
 
 
67
 
 
68
void audioDestruct() {
 
69
  
 
70
}
 
71
 
 
72
 
 
73
 
 
74
/* 
 
75
   should open the audio device, perform any special initialization     
 
76
*/
 
77
int  audioOpen() {
 
78
  audio_fd = open ("/dev/dsp", O_WRONLY, 0);
 
79
  if (audio_fd < 0)  {
 
80
    perror("Unable to open the audio");
 
81
  }
 
82
 
 
83
  // Ok here something important if your programm forks:
 
84
  if (audio_fd > 0) {
 
85
    if (fcntl(audio_fd,F_SETFD,true) < 0) {
 
86
      perror("fcntl socket");exit(1);
 
87
    }
 
88
  }
 
89
  
 
90
  return (audio_fd > 0);
 
91
}
 
92
 
 
93
inline void audioFlush() {
 
94
  if (ioctl(audio_fd, SNDCTL_DSP_RESET, 0) == -1)
 
95
    perror("Unable to reset audio device\n");
 
96
}
 
97
 
 
98
/* 
 
99
   should close the audio device and perform any special shutdown 
 
100
*/
 
101
void audioClose() {
 
102
  audioFlush();
 
103
  if (close(audio_fd) < 0) {
 
104
    perror("error close audiodevice:");
 
105
  }
 
106
}
 
107
 
 
108
 
 
109
void audioInit(int sampleSize,int frequency, int stereo, int sign, int big) {
 
110
  if( sign == 0 )
 
111
  {
 
112
      fprintf(stderr,
 
113
              "%s, %d: expecting signed audio data, "
 
114
              "initialized unsigned (ignored)\n",
 
115
              __FILE__, __LINE__ );
 
116
  }
 
117
  if( big != 0 )
 
118
  {
 
119
      fprintf(stderr,
 
120
              "%s, %d: expecting little endian audio data, "
 
121
              "initialized big endian (ignored)\n",
 
122
              __FILE__, __LINE__ );
 
123
  }
 
124
 
 
125
  int play_format=AFMT_S16_LE;
 
126
 
 
127
  if (sampleSize == 8) {
 
128
    play_format=AFMT_S8;
 
129
  }
 
130
  ioctl(audio_fd,SNDCTL_DSP_RESET,NULL);
 
131
  
 
132
  if (ioctl(audio_fd, SNDCTL_DSP_SETFMT,&play_format) < 0) {
 
133
    perror("Unable to set required audio format\n");
 
134
  }
 
135
  
 
136
  /* Set 1 or 2 channels */
 
137
  stereo=(stereo ? 1 : 0);
 
138
 
 
139
  if (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) < 0) {
 
140
    perror("Unable to set stereo/mono\n");
 
141
    exit(0);
 
142
  }
 
143
  
 
144
  /* Set the output frequency */
 
145
  if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &frequency) < 0) {
 
146
    perror("Unable to set frequency");
 
147
    exit(0);
 
148
  }
 
149
  
 
150
  if (ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &AUSIZ) == -1) {
 
151
    perror("Unable to get fragment size\n");
 
152
    exit(0);
 
153
  }
 
154
}
 
155
 
 
156
 
 
157
int getAudioBufferSize() {
 
158
  struct audio_buf_info buf_info;
 
159
  int buf=1024*65;
 
160
  if (ioctl(audio_fd,SNDCTL_DSP_GETOSPACE,&buf_info) == -1) {
 
161
    perror("ioctl getAudioBufferSize using default");
 
162
  } else {
 
163
    buf=buf_info.bytes;
 
164
  }
 
165
  return buf;
 
166
}
 
167
 
 
168
 
 
169
int mixerOpen() {
 
170
  int supportedMixers;
 
171
 
 
172
  if ((mixer_fd=open("/dev/mixer",O_RDWR)) == -1) {
 
173
    perror("Unable to open mixer device");
 
174
  }
 
175
  
 
176
  // Ok here something important if your programm forks:
 
177
  if (mixer_fd > 0) {
 
178
    if (fcntl(mixer_fd,F_SETFD,true) < 0) {
 
179
      perror("fcntl socket");exit(1);
 
180
    }
 
181
  }
 
182
  
 
183
  if (ioctl(mixer_fd, SOUND_MIXER_READ_DEVMASK, &supportedMixers) == -1){
 
184
    perror("Unable to get mixer info assuming master volume");
 
185
    volumeIoctl=SOUND_MIXER_WRITE_VOLUME;
 
186
  } else {
 
187
    if ((supportedMixers & SOUND_MASK_PCM) != 0)
 
188
      volumeIoctl=SOUND_MIXER_WRITE_PCM;
 
189
    else
 
190
      volumeIoctl=0;
 
191
  }
 
192
 
 
193
  return (mixer_fd > 0);
 
194
}
 
195
 
 
196
 
 
197
void mixerClose() {
 
198
  if (mixer_fd != -1) {
 
199
    close(mixer_fd);
 
200
  }
 
201
}
 
202
 
 
203
/* 
 
204
   only code this if your system can change the volume while 
 
205
   playing
 
206
*/
 
207
void mixerSetVolume(int leftVolume,int rightVolume) {
 
208
  int volume;
 
209
 
 
210
  volume=leftVolume+(rightVolume<<8);
 
211
  if ((mixer_fd != -1) && (volumeIoctl!=0)) {
 
212
    if (ioctl(mixer_fd, volumeIoctl, &volume) < 0) {
 
213
      perror("Unable to set sound volume");
 
214
    }
 
215
  }
 
216
}
 
217
 
 
218
 
 
219
 
 
220
int audioWrite(char *buffer, int count) {
 
221
  return(write(audio_fd,buffer,count));
 
222
}
 
223
 
 
224
 
 
225
int getAudioFd() {
 
226
  return(audio_fd);
 
227
}