Search
lxdream.org :: lxdream/src/drivers/audio_osx.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/audio_osx.c
changeset 786:8f6ece92500e
prev736:a02d1475ccfd
author nkeynes
date Tue Oct 14 08:44:37 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Fix a few more subtle flag problems
view annotate diff log raw
     1 /**
     2  * $Id$
     3  * 
     4  * The darwin core-audio audio driver
     5  *
     6  * Copyright (c) 2008 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    18 #include <stdio.h>
    19 #include <unistd.h>
    20 #include <CoreAudio/CoreAudio.h>
    21 #include "aica/audio.h"
    22 #include "lxdream.h"
    24 #define BUFFER_SIZE (sizeof(float)*2*2205)
    26 static AudioDeviceID output_device;
    27 static volatile audio_buffer_t output_buffer = NULL;
    28 static uint32_t buffer_size;
    30 OSStatus audio_osx_callback( AudioDeviceID inDevice,
    31                              const AudioTimeStamp *inNow,
    32                              const AudioBufferList *inInputData,
    33                              const AudioTimeStamp *inInputTime,
    34                              AudioBufferList *outOutputData,
    35                              const AudioTimeStamp *inOutputTime,
    36                              void *inClientData)
    37 {
    38     char *output = outOutputData->mBuffers[0].mData;
    39     int data_requested = buffer_size;
    41     while( output_buffer != NULL && data_requested > 0 ) {
    42         int copysize = output_buffer->length - output_buffer->posn;
    43         if( copysize > data_requested ) {
    44             copysize = data_requested;
    45         }
    46         memcpy( output, &output_buffer->data[output_buffer->posn], copysize );
    47         output += copysize;
    48         data_requested -= copysize;
    49         output_buffer->posn += copysize;
    50         if( output_buffer->posn >= output_buffer->length ) {
    51             output_buffer = audio_next_read_buffer();
    52         }
    53     }
    54     if( data_requested > 0 ) {
    55         memset( output, 0, data_requested );
    56     }
    57     return noErr;
    58 }
    60 static gboolean audio_osx_shutdown()
    61 {
    62     AudioDeviceStop( output_device, audio_osx_callback );
    63     AudioDeviceRemoveIOProc( output_device, audio_osx_callback );
    64     return TRUE;
    65 }
    67 static gboolean audio_osx_init()
    68 {
    69     UInt32 size = sizeof(output_device);
    70     AudioStreamBasicDescription outputDesc;
    71     UInt32 outputDescSize = sizeof(outputDesc);
    73     if( AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
    74             &size, &output_device) != noErr ||
    75             output_device == kAudioDeviceUnknown ) {
    76         return FALSE;
    77     }
    79     if( AudioDeviceGetProperty( output_device, 1, 0, kAudioDevicePropertyStreamFormat,
    80             &outputDescSize, &outputDesc ) != noErr ) {
    81         return FALSE;
    82     }
    84     buffer_size = BUFFER_SIZE;
    86     if( AudioDeviceSetProperty( output_device, 0, 0, 0, kAudioDevicePropertyBufferSize,
    87             sizeof(buffer_size), &buffer_size ) != noErr ) {
    88         return FALSE;
    89     }
    91     AudioDeviceAddIOProc( output_device, audio_osx_callback, NULL );    
    92     return TRUE;
    93 }
    94 static gboolean audio_osx_process_buffer( audio_buffer_t buffer )
    95 {
    96     if( output_buffer == NULL ) {
    97         output_buffer = buffer;
    98         output_buffer->posn = 0;
    99         AudioDeviceStart(output_device, audio_osx_callback);
   100     }
   101     return FALSE;
   102 }
   104 void audio_osx_start()
   105 {
   106     if( output_buffer != NULL ) {
   107         AudioDeviceStart(output_device, audio_osx_callback);
   108     }
   109 }
   111 void audio_osx_stop()
   112 {
   113     AudioDeviceStop( output_device, audio_osx_callback );
   114 }
   117 struct audio_driver audio_osx_driver = { 
   118         "osx",
   119         N_("OS X CoreAudio system driver"), 
   120         DEFAULT_SAMPLE_RATE,
   121         AUDIO_FMT_FLOATST,
   122         audio_osx_init,
   123         audio_osx_start, 
   124         audio_osx_process_buffer,
   125         audio_osx_stop,
   126         audio_osx_shutdown};
.