]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - osx_writer.c
rename xxx_writer.c to xxx_write.c
[paraslash.git] / osx_writer.c
diff --git a/osx_writer.c b/osx_writer.c
deleted file mode 100644 (file)
index fab6085..0000000
+++ /dev/null
@@ -1,331 +0,0 @@
-/*
- * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
- *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
- */
-
-/** \file osx_writer.c paraslash's output plugin for MacOs */
-
-/*
- * based on mosx-mpg123, by Guillaume Outters and Steven A. Kortze
- * <skortze@sourceforge.net>
- */
-
-#include <CoreAudio/CoreAudio.h>
-#include "para.h"
-#include "fd.h"
-#include "string.h"
-#include "list.h"
-#include "sched.h"
-#include "write.h"
-#include "osx_write.cmdline.h"
-#include "error.h"
-
-
-#include <CoreAudio/CoreAudio.h>
-#include <AudioUnit/AudioUnit.h>
-#include <AudioToolbox/DefaultAudioOutput.h>
-struct osx_buffer {
-       float *buffer;
-       long size;
-       float *ptr; /* Where in the buffer are we? */
-       long remaining;
-       struct osx_buffer *next;
-};
-typedef struct osx_buffer osx_buffer;
-
-struct private_osx_writer_data {
-       long size;
-       short *ptr;
-       AudioUnit output;
-       char play;
-       osx_buffer *from; /* Current buffers */
-       osx_buffer *to;
-       unsigned samplerate;
-       unsigned channels;
-};
-
-
-/*
- * Tried with 3 buffers, but then any little window move is sufficient to
- * stop the sound (OK, on a G3 400 with a Public Beta. Perhaps now we can
- * go down to 2 buffers). With 16 buffers we have 1.5 seconds music
- * buffered (or, if you're pessimistic, 1.5 seconds latency). Note 0
- * buffers don't work much further than the Bus error.
- */
-#define NUMBER_BUFFERS 2
-
-static void destroy_buffers(struct private_osx_writer_data *powd)
-{
-       osx_buffer *ptr;
-       osx_buffer *ptr2;
-       ptr = powd->to->next;
-       powd->to->next = NULL;
-       while (ptr) {
-               ptr2 = ptr->next;
-               free(ptr->buffer);
-               free(ptr);
-               ptr = ptr2;
-       }
-}
-
-static void init_buffers(struct private_osx_writer_data *powd)
-{
-       int i;
-
-       osx_buffer ** ptrptr;
-       ptrptr = &powd->to;
-       for (i = 0; i < NUMBER_BUFFERS; i++) {
-               *ptrptr = malloc(sizeof(osx_buffer));
-               (*ptrptr)->size = 0;
-               (*ptrptr)->remaining = 0;
-               (*ptrptr)->buffer = NULL;
-               ptrptr = &(*ptrptr)->next;
-               /* This buffer is ready for filling (of course, it is empty!) */
-       }
-       *ptrptr = powd->from = powd->to;
-}
-
-static void fill_buffer(osx_buffer *b, short *source, long size)
-{
-       float *dest;
-
-       if (b->remaining) /* Non empty buffer, must still be playing */
-               return;
-       PARA_INFO_LOG("%ld\n", size);
-       if (b->size != size) {
-               /*
-                * Hey! What's that? Coudn't this buffer size be fixed
-                * once (well, perhaps we just didn't allocate it yet)
-                */
-               if (b->buffer)
-                       free(b->buffer);
-               b->buffer = malloc(size * sizeof(float));
-               b->size = size;
-       }
-       dest = b->buffer;
-       while (size--) {
-               char *tmp = (char *)source;
-               char c = *tmp;
-               *tmp = *(tmp + 1);
-               *(tmp + 1) = c;
-               /* *dest++ = ((*source++) + 32768) / 65536.0; */
-               *dest++ = (*source++) / 32768.0;
-       }
-       b->ptr = b->buffer;
-       b->remaining = b->size;
-}
-
-static OSStatus osx_callback(void * inClientData,
-       __a_unused AudioUnitRenderActionFlags *inActionFlags,
-       __a_unused const AudioTimeStamp *inTimeStamp,
-       __a_unused  UInt32 inBusNumber,
-       __a_unused UInt32 inNumFrames,
-       AudioBufferList *outOutputData)
-{
-       long m, n;
-       float *dest;
-       int i;
-       struct private_osx_writer_data *powd = inClientData;
-
-//     PARA_INFO_LOG("%p\n", powd);
-       for (i = 0; i < outOutputData->mNumberBuffers; ++i) {
-               /* what we have to fill */
-               m = outOutputData->mBuffers[i].mDataByteSize / sizeof(float);
-               dest = outOutputData->mBuffers[i].mData;
-               while (m > 0) {
-                       if ((n = powd->from->remaining) <= 0) {
-                               PARA_INFO_LOG("%s", "buffer underrun\n");
-                               /* no more bytes in the current read buffer! */
-                               while ((n = powd->from->remaining) <= 0)
-                                       /* wait for the results */
-                                       usleep(2000);
-                       }
-                       PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
-                       /*
-                        * we dump what we can. In fact, just the necessary
-                        * should be sufficient
-                        */
-                       if (n > m)
-                               n = m;
-                       memcpy(dest, powd->from->ptr, n * sizeof(float));
-                       dest += n;
-                       /* remember all done work */
-                       m -= n;
-                       powd->from->ptr += n;
-                       if ((powd->from->remaining -= n) <= 0)
-                               powd->from = powd->from->next;
-               }
-       }
-       return 0;
-}
-
-static int osx_writer_open(struct writer_node *wn)
-{
-       struct private_osx_writer_data *powd = para_calloc(
-               sizeof(struct private_osx_writer_data));
-       ComponentDescription desc;
-       Component comp;
-       AURenderCallbackStruct inputCallback = {osx_callback, powd};
-       AudioStreamBasicDescription format;
-       int ret;
-       struct writer_node_group *wng = wn->wng;
-       struct osx_write_args_info *conf = wn->conf;
-
-       wn->private_data = powd;
-       /* where did that default audio output go? */
-       desc.componentType = kAudioUnitType_Output;
-       desc.componentSubType = kAudioUnitSubType_DefaultOutput;
-       /* NOTE: and if default output isn't Apple? */
-       desc.componentManufacturer = kAudioUnitManufacturer_Apple;
-       desc.componentFlags = 0;
-       desc.componentFlagsMask = 0;
-       ret = -E_DEFAULT_COMP;
-       comp = FindNextComponent(NULL, &desc);
-       if (!comp)
-               goto e0;
-       ret = -E_OPEN_COMP;
-       if (OpenAComponent(comp, &powd->output))
-               goto e0;
-       ret = -E_UNIT_INIT;
-       if (AudioUnitInitialize(powd->output))
-               goto e1;
-       powd->size = 0;
-       powd->ptr = NULL;
-       powd->play = 0;
-       /* Hmmm, let's choose PCM format */
-       /* We tell the Output Unit what format we're going to supply data to it.
-        * This is necessary if you're providing data through an input callback
-        * AND you want the DefaultOutputUnit to do any format conversions
-        * necessary from your format to the device's format.
-        */
-       if (!conf->samplerate_given && wng->samplerate)
-               powd->samplerate = *wng->samplerate;
-       else
-               powd->samplerate = conf->samplerate_arg;
-       format.mSampleRate = powd->samplerate;
-       /* The specific encoding type of audio stream*/
-       format.mFormatID = kAudioFormatLinearPCM;
-       /* flags specific to each format */
-       format.mFormatFlags = kLinearPCMFormatFlagIsFloat
-               | kLinearPCMFormatFlagIsPacked
-               | kLinearPCMFormatFlagIsBigEndian;
-       if (!conf->channels_given && wng->channels)
-               powd->channels = *wng->channels;
-       else
-               powd->channels = conf->channels_arg;
-       format.mChannelsPerFrame = powd->channels;
-       format.mFramesPerPacket = 1;
-       format.mBytesPerPacket = format.mChannelsPerFrame * sizeof(float);
-       format.mBytesPerFrame = format.mFramesPerPacket * format.mBytesPerPacket;
-       /* one of the most constant constants of the whole computer history */
-       format.mBitsPerChannel = sizeof(float) * 8;
-       ret = -E_STREAM_FORMAT;
-       if (AudioUnitSetProperty(powd->output, kAudioUnitProperty_StreamFormat,
-                       kAudioUnitScope_Input, 0, &format,
-                       sizeof(AudioStreamBasicDescription)))
-               goto e2;
-       init_buffers(powd);
-       ret = -E_ADD_CALLBACK;
-       if (AudioUnitSetProperty(powd->output, kAudioUnitProperty_SetRenderCallback,
-                       kAudioUnitScope_Input, 0, &inputCallback,
-                       sizeof(inputCallback)) < 0)
-               goto e3;
-       return 1;
-e3:
-       destroy_buffers(powd);
-e2:
-       AudioUnitUninitialize(powd->output);
-e1:
-       CloseComponent(powd->output);
-e0:
-       return ret;
-}
-
-__malloc void *osx_write_parse_config(char *options)
-{
-       struct osx_write_args_info *conf
-               = para_calloc(sizeof(struct osx_write_args_info));
-       PARA_INFO_LOG("options: %s\n", options);
-       int ret = osx_cmdline_parser_string(options, conf, "osx_write");
-       if (ret)
-               goto err_out;
-       return conf;
-err_out:
-       free(conf);
-       return NULL;
-
-}
-
-static void osx_writer_close(struct writer_node *wn)
-{
-       struct private_osx_writer_data *powd = wn->private_data;
-
-       PARA_INFO_LOG("closing writer node %p\n", wn);
-       AudioOutputUnitStop(powd->output);
-       AudioUnitUninitialize(powd->output);
-       CloseComponent(powd->output);
-       destroy_buffers(powd);
-       free(powd);
-}
-
-static int need_new_buffer(struct writer_node *wn)
-{
-       struct writer_node_group *wng = wn->wng;
-       struct private_osx_writer_data *powd = wn->private_data;
-
-       if (*wng->loaded < sizeof(short))
-               return 0;
-       if (powd->to->remaining) /* Non empty buffer, must still be playing */
-               return 0;
-       return 1;
-}
-
-static int osx_write_post_select(__a_unused struct sched *s,
-               struct writer_node *wn)
-{
-       struct private_osx_writer_data *powd = wn->private_data;
-       struct writer_node_group *wng = wn->wng;
-       short *data = (short*)wng->buf;
-
-       if (!need_new_buffer(wn))
-               return 1;
-       fill_buffer(powd->to, data, *wng->loaded / sizeof(short));
-       powd->to = powd->to->next;
-       wn->written = *wng->loaded;
-       if (!powd->play) {
-               if (AudioOutputUnitStart(powd->output))
-                       return -E_UNIT_START;
-               powd->play = 1;
-       }
-       return 1;
-}
-
-static int osx_write_pre_select(struct sched *s, __a_unused struct writer_node *wn)
-{
-       s->timeout.tv_sec = 0;
-       s->timeout.tv_usec = 20;
-       return 1;
-}
-
-void osx_writer_init(struct writer *w)
-{
-       w->open = osx_writer_open;
-       w->close = osx_writer_close;
-       w->pre_select = osx_write_pre_select;
-       w->post_select = osx_write_post_select;
-       w->parse_config = osx_write_parse_config;
-       w->shutdown = NULL; /* nothing to do */
-}