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
|
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.com
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include "MsgFifo.h"
#include "SC_FifoMsg.h"
#include "OSC_Packet.h"
#include "SC_SyncCondition.h"
#include "PriorityQueue.h"
#include <boost/optional.hpp>
#include <SC_Lock.h>
#define SC_AUDIO_API_COREAUDIO 1
#define SC_AUDIO_API_JACK 2
#define SC_AUDIO_API_PORTAUDIO 3
#define SC_AUDIO_API_AUDIOUNITS 4
#define SC_AUDIO_API_COREAUDIOIPHONE 5
#define SC_AUDIO_API_BELA 6
#ifdef SC_IPHONE
# define SC_AUDIO_API SC_AUDIO_API_COREAUDIOIPHONE
#endif
#ifndef SC_AUDIO_API
# if defined(_WIN32)
# define SC_AUDIO_API SC_AUDIO_API_PORTAUDIO
# elif defined(__APPLE__)
# define SC_AUDIO_API SC_AUDIO_API_COREAUDIO
# else
# error SC_AUDIO_API undefined, cannot determine audio backend
# endif
#endif // SC_AUDIO_API
#if SC_AUDIO_API == SC_AUDIO_API_COREAUDIO || SC_AUDIO_API == SC_AUDIO_API_AUDIOUNITS
# include <CoreAudio/AudioHardware.h>
# include <CoreAudio/HostTime.h>
#endif
#if SC_AUDIO_API == SC_AUDIO_API_COREAUDIOIPHONE
# include <AudioUnit/AudioUnit.h>
# include <AudioToolbox/AudioToolbox.h>
# include <AudioToolbox/AudioConverter.h>
# include <AudioToolbox/AUGraph.h>
#endif
struct SC_ScheduledEvent {
/// Callback function responsible for freeing the OSC packet in the correct thread.
typedef void (*PacketFreeFunc)(struct World* world, OSC_Packet* packet);
/// Frees an OSC packet in the realtime thread (to be used as a PacketFreeFunc).
static void FreeInRT(struct World* world, OSC_Packet* packet);
/// Frees an OSC packet in the non-realtime thread (to be used as a PacketFreeFunc).
static void FreeInNRT(struct World* world, OSC_Packet* packet);
SC_ScheduledEvent(): mTime(0), mPacket(0) {}
SC_ScheduledEvent(struct World* inWorld, int64 inTime, OSC_Packet* inPacket, PacketFreeFunc freeFunc):
mTime(inTime),
mPacket(inPacket),
mPacketFreeFunc(freeFunc),
mWorld(inWorld) {}
int64 Time() { return mTime; }
void Perform();
struct key_t {
int64 time, stabilityCount;
bool operator<(key_t const& rhs) const {
if (time < rhs.time)
return true;
if (time > rhs.time)
return false;
return stabilityCount < rhs.stabilityCount;
}
bool operator>(key_t const& rhs) const {
if (time > rhs.time)
return true;
if (time < rhs.time)
return false;
return stabilityCount > rhs.stabilityCount;
}
bool operator==(key_t const& rhs) const { return (time == rhs.time) && (stabilityCount == rhs.stabilityCount); }
};
key_t key() const {
key_t ret;
ret.time = mTime;
ret.stabilityCount = mStabilityCount;
return ret;
}
int64 mTime;
int64 mStabilityCount;
OSC_Packet* mPacket;
PacketFreeFunc mPacketFreeFunc;
struct World* mWorld;
};
typedef MsgFifo<FifoMsg, 65536> EngineFifo;
// Functions to be implemented by the driver backend
extern "C" {
int32 server_timeseed();
int64 oscTimeNow();
};
void initializeScheduler();
/** Denotes whether an OSC packet has been performed immediately or has been scheduled for later execution.
If the package has been scheduled, memory ownership is transferred from the caller to the scheduler.
*/
enum PacketStatus { PacketPerformed, PacketScheduled };
/** Perform a completion message in the realtime thread.
The return value denotes whether ownership is transferred to the scheduler or not.
*/
PacketStatus PerformCompletionMsg(World* world, const OSC_Packet& packet);
class SC_AudioDriver {
protected:
int64 mOSCincrement;
struct World* mWorld;
double mOSCtoSamples;
int mSampleTime;
float mSafetyClipThreshold;
// Common members
uint32 mHardwareBufferSize; // bufferSize returned by kAudioDevicePropertyBufferSize
EngineFifo mFromEngine, mToEngine;
EngineFifo mOscPacketsToEngine;
SC_SyncCondition mAudioSync;
SC_Thread mThread;
bool mRunThreadFlag;
uint32 mSafetyOffset;
PriorityQueueT<SC_ScheduledEvent, 2048> mScheduler;
int mNumSamplesPerCallback;
uint32 mPreferredHardwareBufferFrameSize;
uint32 mPreferredSampleRate;
boost::optional<uint32> mExplicitSampleRate;
double mBuffersPerSecond;
double mAvgCPU, mPeakCPU;
int mPeakCounter, mMaxPeakCounter;
double mOSCincrementNumerator;
double mStartHostSecs;
double mPrevHostSecs;
double mStartSampleTime;
double mPrevSampleTime;
double mSmoothSampleRate;
double mSampleRate;
// Driver interface methods, implemented by subclasses
/**
* DriverSetup() should init the driver and write the num of samples per callback
* and the sample rate into the two addresses supplied as arguments.
* The driver will have access to the "preferred" values of these two args
* (mPreferredHardwareBufferFrameSize, mPreferredSampleRate) and ideally should follow them.
* This method should open the resources (and return true if successful), but shouldn't
* really start the streaming (this is the responsibility of DriverStart()).
*/
virtual bool DriverSetup(int* outNumSamplesPerCallback, double* outSampleRate) = 0;
/**
* Start the audio streaming. Return true iff successful.
*/
virtual bool DriverStart() = 0;
/**
* Stop the audio streaming. Return true iff successful.
*/
virtual bool DriverStop() = 0;
public:
// Common methods
SC_AudioDriver(struct World* inWorld);
virtual ~SC_AudioDriver();
int64 mOSCbuftime;
bool Setup();
bool Start();
bool Stop();
void ClearSched() { mScheduler.Empty(); }
void RunNonRealTime(float* in, float* out, int numSamples, int64 oscTime);
void RunThread();
int SafetyOffset() const { return mSafetyOffset; }
int NumSamplesPerCallback() const { return mNumSamplesPerCallback; }
void SetPreferredHardwareBufferFrameSize(int inSize) { mPreferredHardwareBufferFrameSize = inSize; }
void SetPreferredSampleRate(int inRate) { mPreferredSampleRate = inRate; }
void SetSafetyClipThreshold(float thr) { mSafetyClipThreshold = thr; }
bool SendMsgToEngine(FifoMsg& inMsg); // called by NRT thread
bool SendMsgFromEngine(FifoMsg& inMsg);
bool
SendOscPacketMsgToEngine(FifoMsg& inMsg); // called by OSC socket listener threads, protected by mWorld->mDriverLock
void AddEvent(SC_ScheduledEvent& event) { mScheduler.Add(event); }
double GetAvgCPU() const { return mAvgCPU; }
double GetPeakCPU() const { return mPeakCPU; }
double GetSampleRate() const { return mSampleRate; }
double GetActualSampleRate() const { return mSmoothSampleRate; }
};
extern SC_AudioDriver* SC_NewAudioDriver(struct World* inWorld);
// the following classes should be split out into separate source files.
#if SC_AUDIO_API == SC_AUDIO_API_COREAUDIO || SC_AUDIO_API == SC_AUDIO_API_AUDIOUNITS
class SC_CoreAudioDriver : public SC_AudioDriver {
AudioBufferList* mInputBufList;
AudioDeviceID mInputDevice;
AudioDeviceID mOutputDevice;
AudioStreamBasicDescription inputStreamDesc; // info about the default device
AudioStreamBasicDescription outputStreamDesc; // info about the default device
template <bool IsClipping>
friend OSStatus appIOProc(AudioDeviceID inDevice, const AudioTimeStamp* inNow, const AudioBufferList* inInputData,
const AudioTimeStamp* inInputTime, AudioBufferList* outOutputData,
const AudioTimeStamp* inOutputTime, void* defptr);
friend OSStatus appIOProcSeparateIn(AudioDeviceID device, const AudioTimeStamp* inNow,
const AudioBufferList* inInputData, const AudioTimeStamp* inInputTime,
AudioBufferList* outOutputData, const AudioTimeStamp* inOutputTime,
void* defptr);
bool isClippingEnabled() const { return mSafetyClipThreshold > 0 && mSafetyClipThreshold < INFINITY; }
protected:
// Driver interface methods
virtual bool DriverSetup(int* outNumSamplesPerCallback, double* outSampleRate);
virtual bool DriverStart();
virtual bool DriverStop();
AudioDeviceIOProcID mOutputID;
AudioDeviceIOProcID mInputID;
public:
int builtinoutputflag_;
SC_CoreAudioDriver(struct World* inWorld);
virtual ~SC_CoreAudioDriver();
bool StopStart();
template <bool IsClipping>
void Run(const AudioBufferList* inInputData, AudioBufferList* outOutputData, int64 oscTime);
bool UseInput() { return mInputDevice != kAudioDeviceUnknown; }
bool UseSeparateIO() { return UseInput() && mInputDevice != mOutputDevice; }
AudioDeviceID InputDevice() { return mInputDevice; }
AudioDeviceID OutputDevice() { return mOutputDevice; }
void SetInputBufferList(AudioBufferList* inBufList) { mInputBufList = inBufList; }
AudioBufferList* GetInputBufferList() const { return mInputBufList; }
};
#endif
#if SC_AUDIO_API == SC_AUDIO_API_COREAUDIOIPHONE
class SC_iCoreAudioDriver : public SC_AudioDriver {
AUGraph graph;
AudioStreamBasicDescription inputStreamDesc; // info about the default device
AudioStreamBasicDescription outputStreamDesc; // info about the default device
protected:
// Driver interface methods
virtual bool DriverSetup(int* outNumSamplesPerCallback, double* outSampleRate);
virtual bool DriverStart();
virtual bool DriverStop();
public:
SC_iCoreAudioDriver(struct World* inWorld);
virtual ~SC_iCoreAudioDriver();
void Run(const AudioBufferList* inInputData, AudioBufferList* outOutputData, int64 oscTime);
AudioBufferList* buflist;
AudioBufferList* floatInputList;
AudioBufferList* floatOutputList;
AudioConverterRef converter_in_to_F32;
AudioConverterRef converter_F32_to_out;
AudioConverterRef converter_in_to_out;
int* converter_buffer;
int receivedIn;
AudioUnit inputUnit;
};
inline SC_AudioDriver* SC_NewAudioDriver(struct World* inWorld) { return new SC_iCoreAudioDriver(inWorld); }
#endif // SC_AUDIO_API_COREAUDIOIPHONE
|