tahoma2d/toonz/sources/common/tsound/tsound_mac.cpp

478 lines
14 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "tsound_t.h"
#include "texception.h"
#include "tthread.h"
#include "tthreadmessage.h"
#include <errno.h>
#include <unistd.h>
#include <queue>
#include <set>
#include <QByteArray>
#include <QAudioFormat>
#include <QBuffer>
#include <QAudioOutput>
2016-03-19 06:57:51 +13:00
using namespace std;
//==============================================================================
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
TThread::Mutex MutexOut;
}
2016-06-15 18:43:10 +12:00
class TSoundOutputDeviceImp
: public std::enable_shared_from_this<TSoundOutputDeviceImp> {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
bool m_isPlaying;
bool m_looped;
TSoundTrackFormat m_currentFormat;
std::set<int> m_supportedRate;
bool m_opened;
double m_volume = 0.5;
QAudioOutput *m_audioOutput;
QBuffer *m_buffer;
2016-06-15 18:43:10 +12:00
TSoundOutputDeviceImp()
: m_isPlaying(false)
, m_looped(false)
, m_supportedRate()
, m_opened(false){};
std::set<TSoundOutputDeviceListener *> m_listeners;
~TSoundOutputDeviceImp(){};
bool doOpenDevice();
bool doSetStreamFormat(const TSoundTrackFormat &format);
bool doStopDevice();
void play(const TSoundTrackP &st, TINT32 s0, TINT32 s1, bool loop,
bool scrubbing);
void prepareVolume(double volume);
2016-03-19 06:57:51 +13:00
};
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
struct MyData {
2016-06-15 18:43:10 +12:00
char *entireFileBuffer;
quint64 totalPacketCount;
quint64 fileByteCount;
quint32 maxPacketSize;
quint64 packetOffset;
quint64 byteOffset;
2016-06-15 18:43:10 +12:00
bool m_doNotify;
void *sourceBuffer;
// AudioConverterRef converter;
2016-06-15 18:43:10 +12:00
std::shared_ptr<TSoundOutputDeviceImp> imp;
bool isLooping;
MyData()
: entireFileBuffer(0)
, totalPacketCount(0)
, fileByteCount(0)
, maxPacketSize(0)
, packetOffset(0)
, byteOffset(0)
, sourceBuffer(0)
, isLooping(false)
, m_doNotify(true) {}
2016-03-19 06:57:51 +13:00
};
2016-06-15 18:43:10 +12:00
class PlayCompletedMsg : public TThread::Message {
std::set<TSoundOutputDeviceListener *> m_listeners;
MyData *m_data;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
PlayCompletedMsg(MyData *data) : m_data(data) {}
TThread::Message *clone() const { return new PlayCompletedMsg(*this); }
void onDeliver() {
if (m_data->imp) {
if (m_data->m_doNotify == false) return;
m_data->m_doNotify = false;
if (m_data->imp->m_isPlaying) m_data->imp->doStopDevice();
std::set<TSoundOutputDeviceListener *>::iterator it =
m_data->imp->m_listeners.begin();
for (; it != m_data->imp->m_listeners.end(); ++it)
(*it)->onPlayCompleted();
}
}
2016-03-19 06:57:51 +13:00
};
}
2016-06-15 18:43:10 +12:00
#define checkStatus(err) \
if (err) { \
printf("Error: 0x%x -> %s: %d\n", (int)err, __FILE__, __LINE__); \
fflush(stdout); \
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool TSoundOutputDeviceImp::doOpenDevice() {
m_opened = false;
m_audioOutput = NULL;
m_opened = true;
2016-06-15 18:43:10 +12:00
return m_opened;
}
bool TSoundOutputDeviceImp::doSetStreamFormat(const TSoundTrackFormat &format) {
if (!m_opened) doOpenDevice();
if (!m_opened) return false;
m_opened = true;
return true;
2016-03-19 06:57:51 +13:00
}
//==============================================================================
2016-06-15 18:43:10 +12:00
TSoundOutputDevice::TSoundOutputDevice() : m_imp(new TSoundOutputDeviceImp) {
try {
supportsVolume();
} catch (TSoundDeviceException &e) {
throw TSoundDeviceException(e.getType(), e.getMessage());
}
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TSoundOutputDevice::~TSoundOutputDevice() {
stop();
close();
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSoundOutputDevice::installed() { return true; }
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSoundOutputDevice::open(const TSoundTrackP &st) {
if (!m_imp->doOpenDevice())
throw TSoundDeviceException(TSoundDeviceException::UnableOpenDevice,
"Problem to open the output device");
if (!m_imp->doSetStreamFormat(st->getFormat()))
throw TSoundDeviceException(
TSoundDeviceException::UnableOpenDevice,
"Problem to open the output device setting some params");
return true;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSoundOutputDevice::close() {
stop();
m_imp->m_opened = false;
return true;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
void TSoundOutputDeviceImp::prepareVolume(double volume) {
m_volume = volume;
}
//------------------------------------------------------------------------------
void TSoundOutputDevice::prepareVolume(double volume) {
m_imp->prepareVolume(volume);
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSoundOutputDevice::play(const TSoundTrackP &st, TINT32 s0, TINT32 s1,
bool loop, bool scrubbing) {
int lastSample = st->getSampleCount() - 1;
notLessThan(0, s0);
notLessThan(0, s1);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
notMoreThan(lastSample, s0);
notMoreThan(lastSample, s1);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (s0 > s1) {
2016-03-19 06:57:51 +13:00
#ifdef DEBUG
2016-06-15 18:43:10 +12:00
cout << "s0 > s1; reorder" << endl;
2016-03-19 06:57:51 +13:00
#endif
2016-06-15 18:43:10 +12:00
swap(s0, s1);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (isPlaying()) {
2016-03-19 06:57:51 +13:00
#ifdef DEBUG
2016-06-15 18:43:10 +12:00
cout << "is playing, stop it!" << endl;
2016-03-19 06:57:51 +13:00
#endif
2016-06-15 18:43:10 +12:00
stop();
}
m_imp->play(st, s0, s1, loop, scrubbing);
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSoundOutputDeviceImp::play(const TSoundTrackP &st, TINT32 s0, TINT32 s1,
bool loop, bool scrubbing) {
if (!doSetStreamFormat(st->getFormat())) return;
MyData *myData = new MyData();
myData->imp = shared_from_this();
2016-06-15 18:43:10 +12:00
myData->totalPacketCount = s1 - s0;
myData->fileByteCount = (s1 - s0) * st->getSampleSize();
myData->entireFileBuffer = new char[myData->fileByteCount];
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
memcpy(myData->entireFileBuffer, st->getRawData() + s0 * st->getSampleSize(),
myData->fileByteCount);
2016-03-19 06:57:51 +13:00
m_isPlaying = true;
2016-06-15 18:43:10 +12:00
myData->isLooping = loop;
2016-03-19 06:57:51 +13:00
QAudioFormat format;
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
format.setSampleSize(st->getBitPerSample());
format.setCodec("audio/pcm");
format.setChannelCount(st->getChannelCount());
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(st->getFormat().m_signedSample
? QAudioFormat::SignedInt
: QAudioFormat::UnSignedInt);
format.setSampleRate(st->getSampleRate());
QList<QAudioFormat::Endian> sbos = info.supportedByteOrders();
QList<int> sccs = info.supportedChannelCounts();
QList<int> ssrs = info.supportedSampleRates();
QList<QAudioFormat::SampleType> sstypes = info.supportedSampleTypes();
QList<int> ssss = info.supportedSampleSizes();
QStringList supCodes = info.supportedCodecs();
if (!info.isFormatSupported((format))) {
format = info.nearestFormat(format);
int newChannels = format.channelCount();
int newBitsPerSample = format.sampleSize();
int newSampleRate = format.sampleRate();
QAudioFormat::SampleType newSampleType = format.sampleType();
QAudioFormat::Endian newBo = format.byteOrder();
}
int test = st->getSampleCount() / st->getSampleRate();
QByteArray *data =
new QByteArray(myData->entireFileBuffer, myData->fileByteCount);
QBuffer *newBuffer = new QBuffer;
newBuffer->setBuffer(data);
newBuffer->open(QIODevice::ReadOnly);
newBuffer->seek(0);
if (m_audioOutput == NULL) {
m_audioOutput = new QAudioOutput(format, NULL);
}
m_audioOutput->start(newBuffer);
m_audioOutput->setVolume(m_volume);
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSoundOutputDeviceImp::doStopDevice() {
m_isPlaying = false;
m_audioOutput->stop();
2016-06-15 18:43:10 +12:00
return true;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSoundOutputDevice::stop() {
if (m_imp->m_opened == false) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_imp->doStopDevice();
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSoundOutputDevice::attach(TSoundOutputDeviceListener *listener) {
m_imp->m_listeners.insert(listener);
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSoundOutputDevice::detach(TSoundOutputDeviceListener *listener) {
m_imp->m_listeners.erase(listener);
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
double TSoundOutputDevice::getVolume() {
if (m_imp->m_audioOutput != NULL)
return m_imp->m_audioOutput->volume();
else return m_imp->m_volume;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSoundOutputDevice::setVolume(double volume) {
m_imp->m_volume = volume;
m_imp->m_audioOutput->setVolume(volume);
2016-06-15 18:43:10 +12:00
return true;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSoundOutputDevice::supportsVolume() { return true; }
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
bool TSoundOutputDevice::isPlaying() const { return m_imp->m_isPlaying; }
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
bool TSoundOutputDevice::isLooping() { return m_imp->m_looped; }
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
void TSoundOutputDevice::setLooping(bool loop) { m_imp->m_looped = loop; }
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TSoundTrackFormat TSoundOutputDevice::getPreferredFormat(TUINT32 sampleRate,
int channelCount,
int bitPerSample) {
TSoundTrackFormat fmt(sampleRate, bitPerSample, channelCount, true);
return fmt;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TSoundTrackFormat TSoundOutputDevice::getPreferredFormat(
const TSoundTrackFormat &format) {
return getPreferredFormat(format.m_sampleRate, format.m_channelCount,
format.m_bitPerSample);
2016-03-19 06:57:51 +13:00
}
//==============================================================================
//==============================================================================
// REGISTRAZIONE
//==============================================================================
//==============================================================================
2016-06-15 18:43:10 +12:00
class TSoundInputDeviceImp {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
// ALport m_port;
bool m_stopped;
bool m_isRecording;
bool m_oneShotRecording;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
long m_recordedSampleCount;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TSoundTrackFormat m_currentFormat;
TSoundTrackP m_st;
std::set<int> m_supportedRate;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TThread::Executor m_executor;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TSoundInputDeviceImp()
: m_stopped(false)
, m_isRecording(false)
// , m_port(NULL)
, m_oneShotRecording(false)
, m_recordedSampleCount(0)
, m_st(0)
, m_supportedRate(){};
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
~TSoundInputDeviceImp(){};
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool doOpenDevice(const TSoundTrackFormat &format,
TSoundInputDevice::Source devType);
2016-03-19 06:57:51 +13:00
};
bool TSoundInputDeviceImp::doOpenDevice(const TSoundTrackFormat &format,
2016-06-15 18:43:10 +12:00
TSoundInputDevice::Source devType) {
return true;
2016-03-19 06:57:51 +13:00
}
//==============================================================================
2016-06-15 18:43:10 +12:00
class RecordTask : public TThread::Runnable {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TSoundInputDeviceImp *m_devImp;
int m_ByteToSample;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
RecordTask(TSoundInputDeviceImp *devImp, int numByte)
: TThread::Runnable(), m_devImp(devImp), m_ByteToSample(numByte){};
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
~RecordTask(){};
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void run();
2016-03-19 06:57:51 +13:00
};
2016-06-15 18:43:10 +12:00
void RecordTask::run() {}
2016-03-19 06:57:51 +13:00
//==============================================================================
2016-06-15 18:43:10 +12:00
TSoundInputDevice::TSoundInputDevice() : m_imp(new TSoundInputDeviceImp) {}
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TSoundInputDevice::~TSoundInputDevice() {}
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSoundInputDevice::installed() {
/*
if (alQueryValues(AL_SYSTEM, AL_DEFAULT_INPUT, 0, 0, 0, 0) <=0)
return false;
*/
return true;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSoundInputDevice::record(const TSoundTrackFormat &format,
TSoundInputDevice::Source type) {}
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSoundInputDevice::record(const TSoundTrackP &st,
TSoundInputDevice::Source type) {}
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TSoundTrackP TSoundInputDevice::stop() {
TSoundTrackP st;
return st;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
double TSoundInputDevice::getVolume() { return 0.0; }
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSoundInputDevice::setVolume(double volume) { return true; }
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSoundInputDevice::supportsVolume() { return true; }
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TSoundTrackFormat TSoundInputDevice::getPreferredFormat(TUINT32 sampleRate,
int channelCount,
int bitPerSample) {
TSoundTrackFormat fmt;
return fmt;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TSoundTrackFormat TSoundInputDevice::getPreferredFormat(
const TSoundTrackFormat &format) {
/*
try {
*/
return getPreferredFormat(format.m_sampleRate, format.m_channelCount,
format.m_bitPerSample);
/*}
catch (TSoundDeviceException &e) {
throw TSoundDeviceException( e.getType(), e.getMessage());
}
*/
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSoundInputDevice::isRecording() { return m_imp->m_isRecording; }