~ubuntu-branches/ubuntu/vivid/imaze/vivid

« back to all changes in this revision

Viewing changes to source/voxware_audio.c

  • Committer: Bazaar Package Importer
  • Author(s): Hans Freitag
  • Date: 2002-11-28 13:24:12 UTC
  • Revision ID: james.westby@ubuntu.com-20021128132412-lw82xl9oq1j36g8b
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
** - - -  iMaze  - - -
 
3
**
 
4
** Copyright (c) 1993-2001 by Hans-Ulrich Kiel & Joerg Czeranski
 
5
** All rights reserved.
 
6
**
 
7
** Redistribution and use in source and binary forms, with or without
 
8
** modification, are permitted provided that the following conditions are
 
9
** met:
 
10
**
 
11
** 1. Redistributions of source code must retain the above copyright
 
12
**    notice, this list of conditions and the following disclaimer.
 
13
** 2. Redistributions in binary form must reproduce the above copyright
 
14
**    notice, this list of conditions and the following disclaimer in the
 
15
**    documentation and/or other materials provided with the distribution.
 
16
** 3. The name of the authors may not be used to endorse or promote
 
17
**    products derived from this software without specific prior written
 
18
**    permission.
 
19
** 4. The name ``iMaze'' may not be used for products derived from this
 
20
**    software unless a prefix or a suffix is added to the name.
 
21
**
 
22
** THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 
23
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
24
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
25
** DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT,
 
26
** INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
27
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
28
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
29
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 
30
** STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 
31
** IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
32
** POSSIBILITY OF SUCH DAMAGE.
 
33
**
 
34
**
 
35
** Datei: voxware_audio.c
 
36
**
 
37
** Kommentar:
 
38
**  Plays sounds via Voxware kernel driver
 
39
*/
 
40
 
 
41
#include <stdio.h>
 
42
#include <string.h>
 
43
#include <signal.h>
 
44
#include <fcntl.h>
 
45
#include <unistd.h>
 
46
#include <errno.h>
 
47
#include <time.h>
 
48
#include <sys/ioctl.h>
 
49
 
 
50
#ifdef linux
 
51
#include <linux/soundcard.h>
 
52
#else
 
53
#include <machine/soundcard.h>
 
54
#endif
 
55
 
 
56
#include "argv.h"
 
57
#include "ereignisse.h"
 
58
#include "fehler.h"
 
59
#include "system.h"
 
60
#include "audio.h"
 
61
#include "audio_hw.h"
 
62
 
 
63
static char sccsid[] = "@(#)voxware_audio.c     3.18 12/09/01";
 
64
 
 
65
 
 
66
struct arg_option audio_hw_opts[] =
 
67
{
 
68
        { Arg_End }
 
69
};
 
70
 
 
71
 
 
72
#define DEV_AUDIO "/dev/dsp"
 
73
 
 
74
/* Daten pro Sekunde */
 
75
#define SAMPLING_RATE 8000
 
76
 
 
77
#define LOG2_PUFFER_GROESSE 10
 
78
#define PUFFER_GROESSE (1 << LOG2_PUFFER_GROESSE)
 
79
 
 
80
static int audio_deskriptor; /* Deskriptor fuer das Audio-Device */
 
81
 
 
82
/* tatsaechliche Puffer-Groesse */
 
83
static int puffer_groesse;
 
84
 
 
85
 
 
86
/* bis hier lokaler Teil                       */
 
87
/***********************************************/
 
88
/* ab hier globaler Teil                       */
 
89
 
 
90
 
 
91
void audio_init(void)
 
92
{
 
93
        {
 
94
                /* 2 Fragmente der Groesse PUFFER_GROESSE */
 
95
                int frag = 2 << 16 | LOG2_PUFFER_GROESSE;
 
96
 
 
97
                if (ioctl(audio_deskriptor, SNDCTL_DSP_SETFRAGMENT, &frag))
 
98
                {
 
99
                        static char *meldung[] = { "iMaze - Sound Error", "",
 
100
                                "Can't set fragment size:", NULL, NULL };
 
101
 
 
102
                        meldung[3] = fehler_text();
 
103
                        uebler_fehler(meldung, NULL);
 
104
                }
 
105
        }
 
106
 
 
107
        /* Puffer-Groesse abfragen */
 
108
        if (ioctl(audio_deskriptor, SNDCTL_DSP_GETBLKSIZE, &puffer_groesse))
 
109
        {
 
110
                static char *meldung[] = { "iMaze - Sound Error", "",
 
111
                        "Can't check audio buffer size:", NULL, NULL };
 
112
 
 
113
                meldung[3] = fehler_text();
 
114
                uebler_fehler(meldung, NULL);
 
115
        }
 
116
 
 
117
        /* Puffer zu gross? */
 
118
        if (puffer_groesse > PUFFER_GROESSE)
 
119
                puffer_groesse = PUFFER_GROESSE;
 
120
 
 
121
        {
 
122
                int speed = SAMPLING_RATE;
 
123
 
 
124
                /* Abtastrate setzen */
 
125
                if (ioctl(audio_deskriptor, SNDCTL_DSP_SPEED, &speed))
 
126
                {
 
127
                        static char *meldung[] = { "iMaze - Sound Error", "",
 
128
                                "Can't set audio sample rate:", NULL, NULL };
 
129
 
 
130
                        meldung[3] = fehler_text();
 
131
                        uebler_fehler(meldung, NULL);
 
132
                }
 
133
        }
 
134
}
 
135
 
 
136
 
 
137
void audio_hw_start(void)
 
138
{
 
139
        deskriptor_nicht_blockieren(audio_deskriptor);
 
140
}
 
141
 
 
142
 
 
143
void audio_hw_close(void)
 
144
{
 
145
        close(audio_deskriptor);
 
146
}
 
147
 
 
148
 
 
149
int audio_hw_protect(void)
 
150
{
 
151
        return 0; /* don't care about state information */
 
152
}
 
153
 
 
154
 
 
155
void audio_hw_unprotect(int state)
 
156
{
 
157
}
 
158
 
 
159
 
 
160
int audio_hw_is_busy(void)
 
161
{
 
162
        return 0; /* always try to write */
 
163
}
 
164
 
 
165
 
 
166
int audio_hw_is_multiwrite_allowed(void)
 
167
{
 
168
        return 0; /* never try to write more than once */
 
169
}
 
170
 
 
171
 
 
172
/* buffer[length], buffer[length + 1], ... might be modified! */
 
173
int audio_hw_write(unsigned char *buffer, int length)
 
174
{
 
175
        int written;
 
176
 
 
177
        if (length < puffer_groesse)
 
178
                memset(buffer + length, 128, puffer_groesse - length);
 
179
 
 
180
        written = write(audio_deskriptor, buffer, puffer_groesse);
 
181
 
 
182
        if (written >= length)
 
183
                return length;
 
184
        else
 
185
                return written;
 
186
}
 
187
 
 
188
 
 
189
void audio_hw_hint_waiting(void)
 
190
{
 
191
        audio_select_descriptor(audio_deskriptor, audio_hw_ready_callback, 1);
 
192
}
 
193
 
 
194
 
 
195
void audio_hw_hint_not_waiting(void)
 
196
{
 
197
        audio_select_descriptor(audio_deskriptor, (void (*)(void))NULL, 1);
 
198
}
 
199
 
 
200
 
 
201
int audio_open(char ***meldung)
 
202
{
 
203
        static char *message[] = { "iMaze - Sound Error", "",
 
204
                NULL, NULL, NULL };
 
205
        static char text[80];
 
206
 
 
207
        if ((audio_deskriptor = open(DEV_AUDIO, O_WRONLY | O_NDELAY, 0)) >= 0)
 
208
                return 1;
 
209
 
 
210
        sprintf(text, "Can't open %s:", DEV_AUDIO);
 
211
        message[2] = text;
 
212
        message[3] = fehler_text();
 
213
 
 
214
        *meldung = message;
 
215
 
 
216
        return errno == EBUSY ? 2 : 0;
 
217
}
 
218
 
 
219
 
 
220
int audio_hw_get_buffer_size(void)
 
221
{
 
222
        return puffer_groesse;
 
223
}
 
224
 
 
225
 
 
226
int audio_hw_uses_ulaw(void)
 
227
{
 
228
        return 0; /* no, linear encoding used */
 
229
}