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

« back to all changes in this revision

Viewing changes to mpeglib/lib/util/audio/audioIO_HPUX.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: Lutz Vieweg
 
4
        Modified by:
 
5
        * Andrew Richards - moved code from audio.c
 
6
 
 
7
 */
 
8
 
 
9
 
 
10
#include <sys/audio.h>
 
11
#include <fcntl.h>
 
12
#include <sys/ioctl.h>
 
13
#include <sys/lock.h>
 
14
#include <unistd.h>
 
15
#include <stdio.h>
 
16
#include "audioIO.h"
 
17
 
 
18
/* declare these static to effectively isolate the audio device */
 
19
 
 
20
static int audio_fd;
 
21
 
 
22
 
 
23
/* audioOpen() */
 
24
/* should open the audio device, perform any special initialization              */
 
25
/* Set the frequency, no of channels and volume. Volume is only set if */
 
26
/* it is not -1 */
 
27
 
 
28
void audioOpen() {
 
29
  if ((audio_fd = open("/dev/audio",O_RDWR))==-1)
 
30
    die(" unable to open the audio device\n");
 
31
  
 
32
  DB(audio, msg("Audio device opened on %d\n",audio_fd); )
 
33
    }
 
34
        
 
35
 
 
36
void
 
37
audioInit(int sampleSize,int frequency, int stereo)
 
38
{
 
39
        int flags;
 
40
        int failed = 0;
 
41
        int volume=100;
 
42
        
 
43
        if ((flags = fcntl (audio_fd, F_GETFL, 0)) < 0) {
 
44
                die("unable to set non-blocking mode for /dev/audio\n");
 
45
        }
 
46
        flags |= O_NDELAY;
 
47
        if (fcntl (audio_fd, F_SETFL, flags) < 0) {
 
48
                die("unable to set non-blocking mode for /dev/audio\n");
 
49
        }
 
50
        
 
51
        if ( ioctl(audio_fd, AUDIO_SET_DATA_FORMAT, AUDIO_FORMAT_LINEAR16BIT) < 0 ||
 
52
                         ioctl(audio_fd, AUDIO_SET_CHANNELS, stereo ? 2 : 1) < 0 ||
 
53
                         ioctl(audio_fd, AUDIO_SET_OUTPUT, AUDIO_OUT_SPEAKER | AUDIO_OUT_HEADPHONE
 
54
                                                 | AUDIO_OUT_LINE) < 0 ||
 
55
                         ioctl(audio_fd, AUDIO_SET_SAMPLE_RATE, frequency) < 0) {
 
56
                failed = -1;            
 
57
        }
 
58
        if (volume != -1) {
 
59
                struct audio_describe description;
 
60
                struct audio_gains gains;
 
61
                float fvolume = (float)volume / 100.0f;
 
62
                if (ioctl(audio_fd, AUDIO_DESCRIBE, &description)) {
 
63
                        failed = -1;
 
64
                }
 
65
                if (ioctl (audio_fd, AUDIO_GET_GAINS, &gains)) {
 
66
                        failed = -1;
 
67
                }
 
68
                
 
69
                gains.transmit_gain = (int)((float)description.min_transmit_gain +
 
70
                                                                                                                                (float)(description.max_transmit_gain
 
71
                                                                                                                                                                - description.min_transmit_gain)
 
72
                                                                                                                                * fvolume);
 
73
                
 
74
                /* gains.monitor_gain = description.min_monitor_gain; */ /* don't monitor ! */
 
75
 
 
76
                if (ioctl (audio_fd, AUDIO_SET_GAINS, &gains)) {
 
77
                        failed = -1;
 
78
                }
 
79
        }
 
80
        
 
81
        if (ioctl(audio_fd, AUDIO_SET_TXBUFSIZE, 4096 * 8)) {
 
82
                failed = -1;
 
83
        }
 
84
        if (failed)
 
85
                die(" unable to setup /dev/audio\n");
 
86
}
 
87
        
 
88
 
 
89
/* audioSetVolume - only code this if your system can change the volume while */
 
90
/*                                                                      playing. sets the output volume 0-100 */
 
91
 
 
92
void
 
93
audioSetVolume(int volume)
 
94
{
 
95
        struct audio_describe description;
 
96
        struct audio_gains gains;
 
97
        int failed = 0;
 
98
        float fvolume = ((float)volume) / 100.0f;
 
99
        if (ioctl(audio_fd, AUDIO_DESCRIBE, &description)) {
 
100
                failed = -1;
 
101
        }
 
102
        if (ioctl (audio_fd, AUDIO_GET_GAINS, &gains)) {
 
103
                failed = -1;
 
104
}
 
105
 
 
106
        gains.transmit_gain = (int)((float)description.min_transmit_gain +
 
107
                                                                                                        (float)(description.max_transmit_gain
 
108
                                                                                                                                - description.min_transmit_gain)
 
109
                                                                                                        * fvolume);
 
110
        if (ioctl (audio_fd, AUDIO_SET_GAINS, &gains)) {
 
111
                failed = -1;
 
112
        }
 
113
        
 
114
        /* could evaluate "failed" here - but who cares? */ 
 
115
 
 
116
        DB(audio, msg("volume set to %d%%\n",volume); )
 
117
 
 
118
}
 
119
 
 
120
/* audioFlush() */
 
121
/* should flush the audio device */
 
122
 
 
123
inline void
 
124
audioFlush()
 
125
{
 
126
        DB(audio, msg("audio: flush %d\n",audio_fd) );
 
127
}
 
128
 
 
129
 
 
130
/* audioClose() */
 
131
/* should close the audio device and perform any special shutdown */
 
132
 
 
133
void
 
134
audioClose()
 
135
{
 
136
        close(audio_fd);
 
137
        DB(audio, msg("audio: closed %d\n",audio_fd) );
 
138
}
 
139
 
 
140
 
 
141
/* audioWrite */
 
142
/* writes count bytes from buffer to the audio device */
 
143
/* returns the number of bytes actually written */
 
144
 
 
145
int audioWrite(char *buffer, int count)
 
146
{
 
147
        DB(audio, msg("audio: Writing %d bytes to audio descriptor %d\n",count,getAudioFd()) );
 
148
        return(write(audio_fd,buffer,count));
 
149
}
 
150
 
 
151
 
 
152
/* Let buffer.c have the audio descriptor so it can select on it. This means    */
 
153
/* that the program is dependent on an file descriptor to work. Should really */
 
154
/* move the select's etc (with inlines of course) in here so that this is the */
 
155
/* ONLY file which has hardware dependent audio stuff in it                                                                             */
 
156
 
 
157
int
 
158
getAudioFd()
 
159
{
 
160
        return(audio_fd);
 
161
}
 
162
 
 
163
/*
 
164
        Try to set the priority of this process to a value which
 
165
        allows us to play without buffering, thus saving memory
 
166
        and avoiding cache-misses.
 
167
        If we cannot get any priority high enough to allow for
 
168
        undisturbed replay (because we don't have sufficient
 
169
        priviledges), return a zero, otherwise, return a one.
 
170
*/
 
171
int audioSetPriority(void) {
 
172
        
 
173
        /* try to lock process in physical memory, just ignore if this fails */
 
174
        plock(PROCSHLIBLOCK);
 
175
        
 
176
        /* try to set a realtime-priority of 64 */
 
177
        if (-1 != rtprio(0, 64)) {
 
178
                DB(audio, msg("using real-time priority\n"); )
 
179
                return 1; 
 
180
        }
 
181
        
 
182
        /* try to set a nice-level of -20 */
 
183
        if (-1 != nice(-20)) {
 
184
                DB(audio, msg("using nice-level -20\n"); )
 
185
                return 1; 
 
186
        }
 
187
        
 
188
        DB(audio, msg("using buffered output\n"); )
 
189
        return 0; /* need to use a buffer */
 
190
}