PageRenderTime 28ms CodeModel.GetById 12ms app.highlight 13ms RepoModel.GetById 0ms app.codeStats 1ms

/include/AudioDevice.h

http://github.com/digego/extempore
C Header | 193 lines | 130 code | 23 blank | 40 comment | 9 complexity | 0495978addc049f51d2bc83cc5c92e7e MD5 | raw file
  1/*
  2 * Copyright (c) 2011, Andrew Sorensen
  3 *
  4 * All rights reserved.
  5 *
  6 *
  7 * Redistribution and use in source and binary forms, with or without
  8 * modification, are permitted provided that the following conditions are met:
  9 *
 10 * 1. Redistributions of source code must retain the above copyright notice,
 11 *    this list of conditions and the following disclaimer.
 12 *
 13 * 2. Redistributions in binary form must reproduce the above copyright notice,
 14 *    this list of conditions and the following disclaimer in the documentation
 15 *    and/or other materials provided with the distribution.
 16 *
 17 * Neither the name of the authors nor other contributors may be used to endorse
 18 * or promote products derived from this software without specific prior written
 19 * permission.
 20 *
 21 *
 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 32 * POSSIBILITY OF SUCH DAMAGE.
 33 *
 34 */
 35
 36#ifndef _AUDIO_DEVICE_H
 37#define _AUDIO_DEVICE_H
 38
 39#if defined (__APPLE__)
 40#include <CoreAudio/AudioHardware.h>
 41#endif
 42
 43#if defined (COREAUDIO) //__APPLE__)
 44#include <CoreAudio/AudioHardware.h>
 45#elif defined (ALSA_AUDIO)
 46#include <alsa/asoundlib.h>
 47#else
 48#include <portaudio.h>
 49#endif
 50
 51#include <stdint.h>
 52
 53#include <vector>
 54#include "UNIV.h"
 55#include "EXTThread.h"
 56
 57typedef float SAMPLE;
 58
 59typedef void (*dsp_f_ptr_array)(void*, void*, float*, float*, uint64_t, void*);
 60typedef void (*dsp_f_ptr_sum_array)(void* ,void* ,float** ,float*, uint64_t, void*);
 61typedef SAMPLE (*dsp_f_ptr)(void*, void*, SAMPLE, uint64_t, uint64_t, const SAMPLE*);
 62typedef SAMPLE (*dsp_f_ptr_sum)(void*, void*, SAMPLE*, uint64_t, uint64_t, const SAMPLE*);
 63
 64typedef SAMPLE (*closure_fn_type)(SAMPLE, uint64_t, uint64_t, const SAMPLE*);
 65
 66namespace extemp
 67{
 68
 69class AudioDevice
 70{
 71private:
 72    typedef void* (*closure_getter_fn_type)();
 73public:
 74    static const unsigned MAX_RT_AUDIO_THREADS = 16;
 75private:
 76    bool m_started;
 77    PaStream* stream;
 78    float* buffer;
 79    closure_getter_fn_type m_dsp_closure;
 80    closure_getter_fn_type m_dsp_mt_closure[128];
 81    dsp_f_ptr dsp_wrapper;
 82    dsp_f_ptr_sum dsp_wrapper_sum;
 83    dsp_f_ptr_array dsp_wrapper_array;
 84    dsp_f_ptr_sum_array dsp_wrapper_sum_array;
 85    SAMPLE* outbuf;
 86    SAMPLE* inbuf;
 87    float* outbuf_f;
 88    float* inbuf_f;
 89    EXTThread* m_threads[MAX_RT_AUDIO_THREADS];
 90    unsigned   m_numThreads;
 91    bool       m_zeroLatency;
 92    bool       m_toggle;
 93
 94    //static AudioDevice* SINGLETON;
 95    static AudioDevice SINGLETON;
 96private:
 97    bool WrapperSet() const {
 98        return dsp_wrapper || dsp_wrapper_array || dsp_wrapper_sum || dsp_wrapper_sum_array;
 99    }
100public:
101    AudioDevice();
102    ~AudioDevice();
103
104    // start and stop audio processing (which also stops time!!)
105    void start();
106    void stop();
107
108    bool getZeroLatency() { return m_zeroLatency; }
109    void setZeroLatency(bool Val) { m_zeroLatency = Val; }
110    bool getToggle() {
111        m_toggle = !m_toggle;
112        return m_toggle;
113    }
114
115    void setDSPClosure(void* Function) {
116        if (m_dsp_closure) {
117            printf("You can only set the DSP callback once, but you\ncan re-define that function as often as you like\n");
118            return;
119        }
120        m_dsp_closure = reinterpret_cast<closure_getter_fn_type>(Function);
121    }
122    closure_getter_fn_type getDSPClosure() { return m_dsp_closure; }
123    void setDSPMTClosure(void* Function, int Index) {
124        if (m_dsp_mt_closure[Index]) {
125            printf("You can only set the DSP callback once, but you\ncan re-define that function as often as you like\n");
126            return;
127        }
128        m_dsp_mt_closure[Index] = reinterpret_cast<closure_getter_fn_type>(Function);
129    }
130    closure_getter_fn_type getDSPMTClosure(int Index) { return m_dsp_mt_closure[Index]; }
131
132    void setDSPWrapper(dsp_f_ptr Wrapper) {
133        if (WrapperSet()) {
134            return;
135        }
136        dsp_wrapper = Wrapper;
137    }
138    void setDSPWrapperArray(dsp_f_ptr_array Wrapper) {
139        if (WrapperSet()) {
140            return;
141        }
142        dsp_wrapper_array = Wrapper;
143    }
144    void setDSPMTWrapper(dsp_f_ptr_sum WrapperSum, dsp_f_ptr Wrapper) {
145        if (WrapperSet()) {
146            return;
147        }
148        dsp_wrapper_sum = WrapperSum;
149        dsp_wrapper = Wrapper;
150    }
151    void setDSPMTWrapperArray(dsp_f_ptr_sum_array WrapperSumArray, dsp_f_ptr_array WrapperArray) {
152        if (WrapperSet()) {
153            return;
154        }
155        dsp_wrapper_sum_array = WrapperSumArray;
156        dsp_wrapper_array = WrapperArray;
157    }
158
159    void initMTAudio(int NumThreads, bool ZeroLatency);
160    void initMTAudioBuf(int,bool);
161
162    EXTThread** getMTThreads() { return m_threads; }
163    int getNumThreads() { return m_numThreads; }
164    dsp_f_ptr getDSPWrapper() { return dsp_wrapper; }
165    dsp_f_ptr_array getDSPWrapperArray() { return dsp_wrapper_array; }
166    dsp_f_ptr_sum getDSPSUMWrapper() { return dsp_wrapper_sum; }
167    dsp_f_ptr_sum_array getDSPSUMWrapperArray() { return dsp_wrapper_sum_array; }
168
169    SAMPLE* getDSPMTInBuffer() { return inbuf; }
170    SAMPLE* getDSPMTOutBuffer() { return outbuf; }
171    float* getDSPMTInBufferArray() { return inbuf_f; }
172    float* getDSPMTOutBufferArray() { return outbuf_f; }
173
174    PaStream* getPaStream() { return stream; }
175
176    static AudioDevice* I() { 
177        // if (!SINGLETON) {
178        //     SINGLETON = new AudioDevice();
179        // }
180        // return SINGLETON;
181        return &SINGLETON;
182    }
183
184    static double getCPULoad();
185    static void printDevices();
186
187    static double CLOCKBASE;
188    static double REALTIME;
189    static double CLOCKOFFSET;
190};
191
192} //End Namespace
193#endif