aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/hw/xquartz/quartzAudio.c
blob: 5dee32f5435dbc6008d176fe0e1f107e55d9941c (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// QuartzAudio.m
//
// X Window bell support using CoreAudio or AppKit.
// Greg Parker  gparker@cs.stanford.edu  19 Feb 2001
//
// Info about sine wave sound playback:
// CoreAudio code derived from macosx-dev posting by Tim Wood
//  http://www.omnigroup.com/mailman/archive/macosx-dev/2000-May/002004.html
// Smoothing transitions between sounds
//  http://www.wam.umd.edu/~mphoenix/dss/dss.html
//
/*
 * Copyright (c) 2001 Greg Parker. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name(s) of the above copyright
 * holders shall not be used in advertising or otherwise to promote the sale,
 * use or other dealings in this Software without prior written authorization.
 */

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include "quartzCommon.h"
#include "quartzAudio.h"

#include <CoreAudio/CoreAudio.h>
#include <pthread.h>

#include "inputstr.h"
#include <X11/extensions/XI.h>
#include <assert.h>

void NSBeep(void);

typedef struct QuartzAudioRec {
    double frequency;
    double amplitude;

    UInt32 curFrame;
    UInt32 remainingFrames;
    UInt32 totalFrames;
    UInt32 bytesPerFrame;
    double sampleRate;
    UInt32 fadeLength;

    UInt32 bufferByteCount;
    Boolean playing;
    pthread_mutex_t lock;

    // used to fade out interrupted sound and avoid 'pop'
    double prevFrequency;
    double prevAmplitude;
    UInt32 prevFrame; 
} QuartzAudioRec;

static AudioDeviceID quartzAudioDevice = kAudioDeviceUnknown;
static QuartzAudioRec data;


/*
 * QuartzAudioEnvelope
 *  Fade sound in and out to avoid pop.
 *  Sounds with shorter duration will never reach full amplitude. Deal.
 */
static double QuartzAudioEnvelope(
    UInt32 curFrame,
    UInt32 totalFrames,
    UInt32 fadeLength )
{
    double fadeFrames = min(fadeLength, totalFrames / 2);
    if (fadeFrames < 1) return 0;

    if (curFrame < fadeFrames) {
        return curFrame / fadeFrames;
    } else if (curFrame > totalFrames - fadeFrames) {
        return (totalFrames-curFrame) / fadeFrames;
    } else {
        return 1.0;
    }
}


/*
 * QuartzFillBuffer
 *  Fill this buffer with data and update the data position.
 *  FIXME: this is ugly
 */
static void QuartzFillBuffer(
    AudioBuffer *audiobuffer,
    QuartzAudioRec *data )
{
    float *buffer, *b;
    unsigned int frame, frameCount;
    unsigned int bufferFrameCount;
    float multiplier, v;
    int i;

    buffer = (float *)audiobuffer->mData;
    bufferFrameCount = audiobuffer->mDataByteSize / data->bytesPerFrame;

    frameCount = min(bufferFrameCount, data->remainingFrames);

    // Fade out previous sine wave, if any.
    b = buffer;
    if (data->prevFrame) {
        multiplier = 2*M_PI*(data->prevFrequency/data->sampleRate);
        for (frame = 0; frame < data->fadeLength; frame++) {
            v = data->prevAmplitude *
                QuartzAudioEnvelope(frame+data->fadeLength,
                                    2*data->fadeLength,
                                    data->fadeLength) *
                sin(multiplier * (data->prevFrame+frame));
            for (i = 0; i < audiobuffer->mNumberChannels; i++) {
                *b++ = v;
            }
        }
        // no more prev fade
        data->prevFrame = 0;

        // adjust for space eaten by prev fade
        buffer += audiobuffer->mNumberChannels*frame;
        bufferFrameCount -= frame;
        frameCount = min(bufferFrameCount, data->remainingFrames);
    }

    // Write a sine wave with the specified frequency and amplitude
    multiplier = 2*M_PI*(data->frequency/data->sampleRate);
    for (frame = 0; frame < frameCount; frame++) {
        v = data->amplitude * 
            QuartzAudioEnvelope(data->curFrame+frame, data->totalFrames,
                                data->fadeLength) *
            sin(multiplier * (data->curFrame+frame));
        for (i = 0; i < audiobuffer->mNumberChannels; i++) {
            *b++ = v;
        }
    }

    // Zero out the rest of the buffer, if any
    memset(b, 0, sizeof(float) * audiobuffer->mNumberChannels *
           (bufferFrameCount-frame));

    data->curFrame += frameCount;
    data->remainingFrames -= frameCount;
    if (data->remainingFrames == 0) {
        data->playing = FALSE;
        data->curFrame = 0;
    }
}


/*
 * QuartzAudioIOProc
 *  Callback function for audio playback.
 *  FIXME: use inOutputTime to correct for skipping
 */
static OSStatus 
QuartzAudioIOProc(
    AudioDeviceID inDevice, 
    const AudioTimeStamp *inNow, 
    const AudioBufferList *inInputData, 
    const AudioTimeStamp *inInputTime, 
    AudioBufferList *outOutputData, 
    const AudioTimeStamp *inOutputTime, 
    void *inClientData )
{
    QuartzAudioRec *data = (QuartzAudioRec *)inClientData;
    int i;
    Boolean wasPlaying;

    pthread_mutex_lock(&data->lock);
    wasPlaying = data->playing;
    for (i = 0; i < outOutputData->mNumberBuffers; i++) {
        if (data->playing) {
            QuartzFillBuffer(outOutputData->mBuffers+i, data); 
        }
        else {
            memset(outOutputData->mBuffers[i].mData, 0, 
                   outOutputData->mBuffers[i].mDataByteSize);
        }
    }
    if (wasPlaying  &&  !data->playing) {
        OSStatus err;
        err = AudioDeviceStop(inDevice, QuartzAudioIOProc);
    }
    pthread_mutex_unlock(&data->lock);
    return 0;
}


/*
 * QuartzCoreAudioBell
 *  Play a tone using the CoreAudio API
 */
static void QuartzCoreAudioBell(
    int volume,         // volume is % of max
    int pitch,          // pitch is Hz
    int duration )      // duration is milliseconds
{
    if (quartzAudioDevice == kAudioDeviceUnknown) return;

    pthread_mutex_lock(&data.lock);

    // fade previous sound, if any
    data.prevFrequency = data.frequency;
    data.prevAmplitude = data.amplitude;
    data.prevFrame = data.curFrame;

    // set new sound
    data.frequency = pitch;
    data.amplitude = volume / 100.0;
    data.curFrame = 0;
    data.totalFrames = (int)(data.sampleRate * duration / 1000.0);
    data.remainingFrames = data.totalFrames;

    if (! data.playing) {
        OSStatus status;
        status = AudioDeviceStart(quartzAudioDevice, QuartzAudioIOProc);
        if (status) {
            ErrorF("QuartzAudioBell: AudioDeviceStart returned %ld\n", status);
        } else {
            data.playing = TRUE;
        }
    }
    pthread_mutex_unlock(&data.lock);
}


/*
 * QuartzBell
 *  Ring the bell
 */
void QuartzBell(
    int volume,             // volume in percent of max
    DeviceIntPtr pDevice,
    pointer ctrl,
    int class )
{
    int pitch;              // pitch in Hz
    int duration;           // duration in milliseconds

    if (class == BellFeedbackClass) {
        pitch = ((BellCtrl*)ctrl)->pitch;
        duration = ((BellCtrl*)ctrl)->duration;
    } else if (class == KbdFeedbackClass) {
        pitch = ((KeybdCtrl*)ctrl)->bell_pitch;
        duration = ((KeybdCtrl*)ctrl)->bell_duration;    
    } else {
        ErrorF("QuartzBell: bad bell class %d\n", class);
        return;
    }

    if (quartzUseSysBeep) {
        if (volume)
            NSBeep();
    } else {
        QuartzCoreAudioBell(volume, pitch, duration);
    }
}


/*
 * QuartzAudioInit
 *  Prepare to play the bell with the CoreAudio API
 */
void QuartzAudioInit(void) 
{
    UInt32 propertySize;
    OSStatus status;
    AudioDeviceID outputDevice;
    AudioStreamBasicDescription outputStreamDescription;
    double sampleRate;

    // Get the default output device
    propertySize = sizeof(outputDevice);
    status = AudioHardwareGetProperty(
                    kAudioHardwarePropertyDefaultOutputDevice, 
                    &propertySize, &outputDevice);
    if (status) {
        ErrorF("QuartzAudioInit: AudioHardwareGetProperty returned %ld\n",
               status);
        return;
    }
    if (outputDevice == kAudioDeviceUnknown) {
        ErrorF("QuartzAudioInit: No audio output devices available.\n");
        return;
    }

    // Get the basic device description
    propertySize = sizeof(outputStreamDescription);
    status = AudioDeviceGetProperty(outputDevice, 0, FALSE, 
                                    kAudioDevicePropertyStreamFormat, 
                                    &propertySize, &outputStreamDescription);
    if (status) {
        ErrorF("QuartzAudioInit: GetProperty(stream format) returned %ld\n",
               status);
        return;
    }
    sampleRate = outputStreamDescription.mSampleRate;

    // Fill in the playback data
    data.frequency = 0;
    data.amplitude = 0;
    data.curFrame = 0;
    data.remainingFrames = 0; 
    data.bytesPerFrame = outputStreamDescription.mBytesPerFrame;
    data.sampleRate = sampleRate;
    // data.bufferByteCount = bufferByteCount;
    data.playing = FALSE;
    data.prevAmplitude = 0;
    data.prevFrame = 0;
    data.prevFrequency = 0;
    data.fadeLength = data.sampleRate / 200;
    pthread_mutex_init(&data.lock, NULL); // fixme error check

    // fixme assert fadeLength<framesPerBuffer

    // Prepare for playback
    status = AudioDeviceAddIOProc(outputDevice, QuartzAudioIOProc, &data);
    if (status) {
        ErrorF("QuartzAudioInit: AddIOProc returned %ld\n", status);
        return;
    }

    // success!
    quartzAudioDevice = outputDevice;
}