From a9c425317e2129ce3e3acaf9ade2cc3f4aec2177 Mon Sep 17 00:00:00 2001 From: Konstantin Dmitriev Date: Fri, 25 May 2018 08:28:41 +0700 Subject: [PATCH] Fix sound playback on Linux by enabling fix #1302 for Linux platform (#1749) close #1302 close #1097 close #907 --- .../tsound/{tsound_mac.cpp => tsound_qt.cpp} | 0 toonz/sources/common/tsound/tsound_sdl.cpp | 551 ------- toonz/sources/common/tsound/tsound_x.cpp | 1294 ----------------- toonz/sources/include/tsound.h | 4 +- toonz/sources/tnzcore/CMakeLists.txt | 14 +- toonz/sources/toonzlib/txshsoundcolumn.cpp | 10 +- 6 files changed, 11 insertions(+), 1862 deletions(-) rename toonz/sources/common/tsound/{tsound_mac.cpp => tsound_qt.cpp} (100%) delete mode 100644 toonz/sources/common/tsound/tsound_sdl.cpp delete mode 100644 toonz/sources/common/tsound/tsound_x.cpp diff --git a/toonz/sources/common/tsound/tsound_mac.cpp b/toonz/sources/common/tsound/tsound_qt.cpp similarity index 100% rename from toonz/sources/common/tsound/tsound_mac.cpp rename to toonz/sources/common/tsound/tsound_qt.cpp diff --git a/toonz/sources/common/tsound/tsound_sdl.cpp b/toonz/sources/common/tsound/tsound_sdl.cpp deleted file mode 100644 index c5473ca2..00000000 --- a/toonz/sources/common/tsound/tsound_sdl.cpp +++ /dev/null @@ -1,551 +0,0 @@ - -#include "tsound_t.h" -#include "texception.h" -#include "tthread.h" -#include "tthreadmessage.h" - -#include -#include -#include -#include - -#include -using namespace std; - -//============================================================================== -namespace { -TThread::Mutex MutexOut; -} - -namespace { - -struct MyData { - char *entireFileBuffer; - - int totalPacketCount; - int fileByteCount; - // UInt32 maxPacketSize; - // UInt64 packetOffset; - int byteOffset; - bool m_doNotify; - - void *sourceBuffer; - // AudioConverterRef converter; - TSoundOutputDeviceImp *imp; - bool isLooping; - MyData() - : entireFileBuffer(0) - , totalPacketCount(0) - , fileByteCount(0) - , /*maxPacketSize(0), packetOffset(0),*/ byteOffset(0) - , sourceBuffer(0) - , isLooping(false) - , imp(0) - , m_doNotify(true) {} -}; -} - -class TSoundOutputDeviceImp { -public: - bool m_isPlaying; - bool m_looped; - TSoundTrackFormat m_currentFormat; - std::set m_supportedRate; - bool m_opened; - struct MyData *m_data; - int m_volume; - - TSoundOutputDeviceImp() - : m_isPlaying(false) - , m_looped(false) - , m_supportedRate() - , m_opened(false) - , m_data(NULL) - , m_volume(SDL_MIX_MAXVOLUME){}; - - std::set m_listeners; - - ~TSoundOutputDeviceImp(){}; - - bool doOpenDevice(const TSoundTrackFormat &format); - bool doStopDevice(); - void play(const TSoundTrackP &st, TINT32 s0, TINT32 s1, bool loop, - bool scrubbing); -}; - -//----------------------------------------------------------------------------- -namespace { - -class PlayCompletedMsg : public TThread::Message { - std::set m_listeners; - MyData *m_data; - -public: - 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; - std::set::iterator it = - m_data->imp->m_listeners.begin(); - for (; it != m_data->imp->m_listeners.end(); ++it) - (*it)->onPlayCompleted(); - if (m_data->imp->m_isPlaying) m_data->imp->doStopDevice(); - } - } -}; -} - -#define checkStatus(err) \ - if (err) { \ - printf("Error: 0x%x -> %s: %d\n", (int)err, __FILE__, __LINE__); \ - fflush(stdout); \ - } - -extern "C" { - -static void sdl_fill_audio(void *udata, Uint8 *stream, int len) { - TSoundOutputDeviceImp *_this = (TSoundOutputDeviceImp *)udata; - MyData *myData = _this->m_data; - - /* Only play if we have data left */ - if (myData == NULL) return; - { - // TThread::ScopedLock sl(MutexOut); - if (myData->imp->m_isPlaying == false) return; - } - - int audio_len = myData->fileByteCount - myData->byteOffset; - if (audio_len <= 0) { - delete[] myData->entireFileBuffer; - myData->entireFileBuffer = 0; -#if 0 - { - TThread::ScopedLock sl(MutexOut); - *(myData->isPlaying) = false; //questo lo faccio nel main thread - } -#endif - PlayCompletedMsg(myData).send(); - return; - } - - /* Mix as much data as possible */ - len = min(audio_len, len); - SDL_memset(stream, 0, len); - SDL_MixAudio(stream, (Uint8 *)myData->entireFileBuffer + myData->byteOffset, - len, _this->m_volume); - myData->byteOffset += len; -} - -} // extern "C" - -bool TSoundOutputDeviceImp::doOpenDevice(const TSoundTrackFormat &format) { - SDL_AudioSpec wanted; - - static bool first = true; // TODO: should be shared with InputDevice - if (first) { - SDL_Init(SDL_INIT_AUDIO); - first = false; - } - - if (m_opened) { - SDL_CloseAudio(); - // we'll just reopen right away - } - - wanted.freq = format.m_sampleRate; - switch (format.m_bitPerSample) { - case 8: - wanted.format = AUDIO_S8; - break; - case 16: - wanted.format = AUDIO_S16; - break; - default: - throw TSoundDeviceException(TSoundDeviceException::UnableOpenDevice, - "invalid bits per sample"); - return false; - } - wanted.channels = format.m_channelCount; /* 1 = mono, 2 = stereo */ - wanted.samples = 1024; /* Good low-latency value for callback */ - wanted.callback = sdl_fill_audio; - wanted.userdata = this; - - /* Open the audio device, forcing the desired format */ - if (SDL_OpenAudio(&wanted, NULL) < 0) { - std::string msg("Couldn't open audio: "); - msg += SDL_GetError(); - throw TSoundDeviceException(TSoundDeviceException::UnableOpenDevice, msg); - return false; - } - - m_opened = true; - return m_opened; -} - -//============================================================================== - -TSoundOutputDevice::TSoundOutputDevice() : m_imp(new TSoundOutputDeviceImp) { -#if 0 - try { - supportsVolume(); - } catch (TSoundDeviceException &e) { - throw TSoundDeviceException(e.getType(), e.getMessage()); - } -#endif -} - -//------------------------------------------------------------------------------ - -TSoundOutputDevice::~TSoundOutputDevice() { - stop(); - close(); -} - -//------------------------------------------------------------------------------ - -bool TSoundOutputDevice::installed() { return true; } - -//------------------------------------------------------------------------------ - -bool TSoundOutputDevice::open(const TSoundTrackP &st) { - if (!m_imp->doOpenDevice(st->getFormat())) - throw TSoundDeviceException( - TSoundDeviceException::UnableOpenDevice, - "Problem to open the output device setting some params"); - return true; -} - -//------------------------------------------------------------------------------ - -bool TSoundOutputDevice::close() { - stop(); - m_imp->m_opened = false; - return true; -} - -//------------------------------------------------------------------------------ - -void TSoundOutputDevice::play(const TSoundTrackP &st, TINT32 s0, TINT32 s1, - bool loop, bool scrubbing) { - // TThread::ScopedLock sl(MutexOut); - int lastSample = st->getSampleCount() - 1; - notLessThan(0, s0); - notLessThan(0, s1); - - notMoreThan(lastSample, s0); - notMoreThan(lastSample, s1); - - if (s0 > s1) { -#ifdef DEBUG - cout << "s0 > s1; reorder" << endl; -#endif - swap(s0, s1); - } - - if (isPlaying()) { -#ifdef DEBUG - cout << "is playing, stop it!" << endl; -#endif - stop(); - } - m_imp->play(st, s0, s1, loop, scrubbing); -} - -//------------------------------------------------------------------------------ - -void TSoundOutputDeviceImp::play(const TSoundTrackP &st, TINT32 s0, TINT32 s1, - bool loop, bool scrubbing) { - if (!doOpenDevice(st->getFormat())) return; - - MyData *myData = new MyData(); - - myData->imp = this; - myData->totalPacketCount = s1 - s0; - myData->fileByteCount = (s1 - s0) * st->getSampleSize(); - myData->entireFileBuffer = new char[myData->fileByteCount]; - - memcpy(myData->entireFileBuffer, st->getRawData() + s0 * st->getSampleSize(), - myData->fileByteCount); - - // myData->maxPacketSize = fileASBD.mFramesPerPacket * - // fileASBD.mBytesPerFrame; - { - // TThread::ScopedLock sl(MutexOut); - m_isPlaying = true; - } - myData->isLooping = loop; - - // cout << "total packet count = " << myData->totalPacketCount <fileByteCount << endl; - - m_data = myData; - SDL_PauseAudio(0); -} - -//------------------------------------------------------------------------------ - -bool TSoundOutputDeviceImp::doStopDevice() { - SDL_PauseAudio(1); - m_isPlaying = false; - delete m_data; - m_data = NULL; - SDL_CloseAudio(); - m_opened = false; - return true; -} - -//------------------------------------------------------------------------------ - -void TSoundOutputDevice::stop() { - // TThread::ScopedLock sl(MutexOut); - if (m_imp->m_opened == false) return; - - // TThread::ScopedLock sl(MutexOut); - m_imp->doStopDevice(); -} - -//------------------------------------------------------------------------------ - -void TSoundOutputDevice::attach(TSoundOutputDeviceListener *listener) { - m_imp->m_listeners.insert(listener); -} - -//------------------------------------------------------------------------------ - -void TSoundOutputDevice::detach(TSoundOutputDeviceListener *listener) { - m_imp->m_listeners.erase(listener); -} - -#if 0 -//------------------------------------------------------------------------------ - -double TSoundOutputDevice::getVolume() -{ - if (!m_imp->m_opened) - m_imp->doOpenDevice(); - - double vol = m_volume / SDL_MIX_MAXVOLUME; - - return (vol < 0. ? 0. : vol); -} - -//------------------------------------------------------------------------------ - -bool TSoundOutputDevice::setVolume(double volume) -{ - Float32 vol = volume; - - m_imp->m_volume = (int)(volume * SDL_MIX_MAXVOLUME); - return true; -} - -//------------------------------------------------------------------------------ - -bool TSoundOutputDevice::supportsVolume() -{ - return true; -} - -#endif -//------------------------------------------------------------------------------ - -bool TSoundOutputDevice::isPlaying() const { - // TThread::ScopedLock sl(MutexOut); - // TODO: handle actually queuing items? - return m_imp->m_isPlaying; -} - -//------------------------------------------------------------------------------ - -bool TSoundOutputDevice::isAllQueuedItemsPlayed() { - // TThread::ScopedLock sl(MutexOut); - return m_imp->m_data == NULL; -} - -//------------------------------------------------------------------------------ - -bool TSoundOutputDevice::isLooping() { - // TThread::ScopedLock sl(MutexOut); - return m_imp->m_looped; -} - -//------------------------------------------------------------------------------ - -void TSoundOutputDevice::setLooping(bool loop) { - // TThread::ScopedLock sl(MutexOut); - m_imp->m_looped = loop; -} - -//------------------------------------------------------------------------------ - -TSoundTrackFormat TSoundOutputDevice::getPreferredFormat(TUINT32 sampleRate, - int channelCount, - int bitPerSample) { - if (bitPerSample > 16) bitPerSample = 16; - // not sure SDL supports more than 2 channels - if (channelCount > 2) channelCount = 2; - TSoundTrackFormat fmt(sampleRate, bitPerSample, channelCount, true); - return fmt; -} - -//------------------------------------------------------------------------------ - -TSoundTrackFormat TSoundOutputDevice::getPreferredFormat( - const TSoundTrackFormat &format) { -#if 0 - try { -#endif - return getPreferredFormat(format.m_sampleRate, format.m_channelCount, - format.m_bitPerSample); -#if 0 - } - catch (TSoundDeviceException &e) { - throw TSoundDeviceException( e.getType(), e.getMessage()); - } -#endif -} - -//============================================================================== -//============================================================================== -// REGISTRAZIONE -//============================================================================== -//============================================================================== - -class TSoundInputDeviceImp { -public: - // ALport m_port; - bool m_stopped; - bool m_isRecording; - bool m_oneShotRecording; - - long m_recordedSampleCount; - - TSoundTrackFormat m_currentFormat; - TSoundTrackP m_st; - std::set m_supportedRate; - - TThread::Executor m_executor; - - TSoundInputDeviceImp() - : m_stopped(false) - , m_isRecording(false) - // , m_port(NULL) - , m_oneShotRecording(false) - , m_recordedSampleCount(0) - , m_st(0) - , m_supportedRate(){}; - - ~TSoundInputDeviceImp(){}; - - bool doOpenDevice(const TSoundTrackFormat &format, - TSoundInputDevice::Source devType); -}; - -bool TSoundInputDeviceImp::doOpenDevice(const TSoundTrackFormat &format, - TSoundInputDevice::Source devType) { - return true; -} - -//============================================================================== - -class RecordTask : public TThread::Runnable { -public: - TSoundInputDeviceImp *m_devImp; - int m_ByteToSample; - - RecordTask(TSoundInputDeviceImp *devImp, int numByte) - : TThread::Runnable(), m_devImp(devImp), m_ByteToSample(numByte){}; - - ~RecordTask(){}; - - void run(); -}; - -void RecordTask::run() {} - -//============================================================================== - -TSoundInputDevice::TSoundInputDevice() : m_imp(new TSoundInputDeviceImp) {} - -//------------------------------------------------------------------------------ - -TSoundInputDevice::~TSoundInputDevice() { -#if 0 - if(m_imp->m_port) - alClosePort(m_imp->m_port); - delete m_imp; -#endif -} - -//------------------------------------------------------------------------------ - -bool TSoundInputDevice::installed() { -#if 0 - if (alQueryValues(AL_SYSTEM, AL_DEFAULT_INPUT, 0, 0, 0, 0) <= 0) - return false; -#endif - return true; -} - -//------------------------------------------------------------------------------ - -void TSoundInputDevice::record(const TSoundTrackFormat &format, - TSoundInputDevice::Source type) {} - -//------------------------------------------------------------------------------ - -void TSoundInputDevice::record(const TSoundTrackP &st, - TSoundInputDevice::Source type) {} - -//------------------------------------------------------------------------------ - -TSoundTrackP TSoundInputDevice::stop() { - TSoundTrackP st; - return st; -} - -//------------------------------------------------------------------------------ - -double TSoundInputDevice::getVolume() { return 0.0; } - -//------------------------------------------------------------------------------ - -bool TSoundInputDevice::setVolume(double volume) { return true; } - -//------------------------------------------------------------------------------ - -bool TSoundInputDevice::supportsVolume() { return true; } - -//------------------------------------------------------------------------------ - -TSoundTrackFormat TSoundInputDevice::getPreferredFormat(TUINT32 sampleRate, - int channelCount, - int bitPerSample) { - TSoundTrackFormat fmt; - return fmt; -} - -//------------------------------------------------------------------------------ - -TSoundTrackFormat TSoundInputDevice::getPreferredFormat( - const TSoundTrackFormat &format) { -#if 0 - try { -#endif - return getPreferredFormat(format.m_sampleRate, format.m_channelCount, - format.m_bitPerSample); -#if 0 - } - catch (TSoundDeviceException &e) { - throw TSoundDeviceException( e.getType(), e.getMessage()); - } -#endif -} - -//------------------------------------------------------------------------------ - -bool TSoundInputDevice::isRecording() { return m_imp->m_isRecording; } diff --git a/toonz/sources/common/tsound/tsound_x.cpp b/toonz/sources/common/tsound/tsound_x.cpp deleted file mode 100644 index 120acca6..00000000 --- a/toonz/sources/common/tsound/tsound_x.cpp +++ /dev/null @@ -1,1294 +0,0 @@ - - -#include "tsound_t.h" -#include "texception.h" -#include "tthread.h" - -#include -#ifdef __sgi -#include -#endif -#include -#include -#include - -#ifndef __sgi -// Warning, this file is for SGI currently, -// otherwise we just stub out the functions. -typedef unsigned long ULONG; -typedef void *ALport; -typedef void *ALvalue; -typedef void *ALpv; -#endif - -// forward declaration -namespace { -bool isInterfaceSupported(int deviceType, int interfaceType); -bool setDefaultInput(TSoundInputDevice::Source type); -bool setDefaultOutput(); -bool isChangeOutput(ULONG sampleRate); -} -//============================================================================== - -class TSoundOutputDeviceImp { -public: - ALport m_port; - bool m_stopped; - bool m_isPlaying; - bool m_looped; - TSoundTrackFormat m_currentFormat; - std::queue m_queuedSoundTracks; - std::set m_supportedRate; - - TThread::Executor m_executor; - TThread::Mutex m_mutex; - - TSoundOutputDeviceImp() - : m_stopped(false) - , m_isPlaying(false) - , m_looped(false) - , m_port(NULL) - , m_queuedSoundTracks() - , m_supportedRate(){}; - - ~TSoundOutputDeviceImp(){}; - - bool doOpenDevice(const TSoundTrackFormat &format); - void insertAllRate(); - bool verifyRate(); -}; - -if (!isInterfaceSupported(AL_DEFAULT_OUTPUT, AL_SPEAKER_IF_TYPE)) - return false; // throw TException("Speakers are not supported"); - -int dev = - alGetResourceByName(AL_SYSTEM, (char *)"Headphone/Speaker", AL_DEVICE_TYPE); -if (!dev) return false; // throw TException("invalid device speakers"); - -pvbuf[0].param = AL_DEFAULT_OUTPUT; -pvbuf[0].value.i = dev; -alSetParams(AL_SYSTEM, pvbuf, 1); - -ALfixed buf[2] = {alDoubleToFixed(0), alDoubleToFixed(0)}; - -config = alNewConfig(); -// qui devo metterci gli altoparlanti e poi setto i valori per il default -// output -pvbuf[0].param = AL_RATE; -pvbuf[0].value.ll = alDoubleToFixed((double)format.m_sampleRate); -pvbuf[1].param = AL_GAIN; -pvbuf[1].value.ptr = buf; -pvbuf[1].sizeIn = 8; -pvbuf[2].param = AL_INTERFACE; -pvbuf[2].value.i = AL_SPEAKER_IF_TYPE; - -if (alSetParams(AL_DEFAULT_OUTPUT, pvbuf, 3) < 0) return false; -// throw TException("Unable to set params for output device"); - -if (alSetChannels(config, format.m_channelCount) == -1) - return false; // throw TException("Error to setting audio hardware."); - -int bytePerSample = format.m_bitPerSample >> 3; -switch (bytePerSample) { -case 3: - bytePerSample++; - break; -default: - break; -} - -bool TSoundOutputDeviceImp::doOpenDevice(const TSoundTrackFormat &format) { -#ifdef __sgi - ALconfig config; - ALpv pvbuf[3]; - - m_currentFormat = format; - - // AL_MONITOR_CTL fa parte dei vecchi andrebbero trovati quelli nuovi - pvbuf[0].param = AL_PORT_COUNT; - pvbuf[1].param = AL_MONITOR_CTL; - if (alGetParams(AL_DEFAULT_OUTPUT, pvbuf, 2) < 0) - if (oserror() == AL_BAD_DEVICE_ACCESS) - return false; // throw TException("Could not access audio hardware."); - - if (!isInterfaceSupported(AL_DEFAULT_OUTPUT, AL_SPEAKER_IF_TYPE)) - return false; // throw TException("Speakers are not supported"); - - int dev = alGetResourceByName(AL_SYSTEM, (char *)"Headphone/Speaker", - AL_DEVICE_TYPE); - if (!dev) return false; // throw TException("invalid device speakers"); - - pvbuf[0].param = AL_DEFAULT_OUTPUT; - pvbuf[0].value.i = dev; - alSetParams(AL_SYSTEM, pvbuf, 1); - - ALfixed buf[2] = {alDoubleToFixed(0), alDoubleToFixed(0)}; - - config = alNewConfig(); - // qui devo metterci gli altoparlanti e poi setto i valori per il default - // output - pvbuf[0].param = AL_RATE; - pvbuf[0].value.ll = alDoubleToFixed((double)format.m_sampleRate); - pvbuf[1].param = AL_GAIN; - pvbuf[1].value.ptr = buf; - pvbuf[1].sizeIn = 8; - pvbuf[2].param = AL_INTERFACE; - pvbuf[2].value.i = AL_SPEAKER_IF_TYPE; - - if (alSetParams(AL_DEFAULT_OUTPUT, pvbuf, 3) < 0) return false; - // throw TException("Unable to set params for output device"); - - if (alSetChannels(config, format.m_channelCount) == -1) - return false; // throw TException("Error to setting audio hardware."); - - int bytePerSample = format.m_bitPerSample >> 3; - switch (bytePerSample) { - case 3: - bytePerSample++; - break; - default: - break; - } - - if (alSetWidth(config, bytePerSample) == -1) - return false; // throw TException("Error to setting audio hardware."); - - if (alSetSampFmt(config, AL_SAMPFMT_TWOSCOMP) == -1) - return false; // throw TException("Error to setting audio hardware."); - - if (alSetQueueSize(config, (TINT32)format.m_sampleRate) == -1) - return false; // throw TException("Error to setting audio hardware."); - - m_port = alOpenPort("AudioOutput", "w", config); - if (!m_port) return false; // throw TException("Could not open audio port."); - - alFreeConfig(config); - return true; -#else - return false; -#endif -} - -//----------------------------------------------------------------------------- - -void TSoundOutputDeviceImp::insertAllRate() { - m_supportedRate.insert(8000); - m_supportedRate.insert(11025); - m_supportedRate.insert(16000); - m_supportedRate.insert(22050); - m_supportedRate.insert(32000); - m_supportedRate.insert(44100); - m_supportedRate.insert(48000); -} - -//----------------------------------------------------------------------------- - -bool TSoundOutputDeviceImp::verifyRate() { -#ifdef __sgi - // Sample Rate - ALparamInfo pinfo; - int ret = alGetParamInfo(AL_DEFAULT_OUTPUT, AL_RATE, &pinfo); - if (ret != -1 && pinfo.elementType == AL_FIXED_ELEM) { - int min = (int)alFixedToDouble(pinfo.min.ll); - int max = (int)alFixedToDouble(pinfo.max.ll); - - std::set::iterator it; - for (it = m_supportedRate.begin(); it != m_supportedRate.end(); ++it) - if (*it < min || *it > max) m_supportedRate.erase(*it); - if (m_supportedRate.end() == m_supportedRate.begin()) return false; - } else if (ret == AL_BAD_PARAM) - return false; - else - return false; -#endif - return true; -} - -//============================================================================== - -class PlayTask : public TThread::Runnable { -public: - TSoundOutputDeviceImp *m_devImp; - TSoundTrackP m_sndtrack; - - PlayTask(TSoundOutputDeviceImp *devImp, const TSoundTrackP &st) - : TThread::Runnable(), m_devImp(devImp), m_sndtrack(st){}; - - ~PlayTask(){}; - - void run(); -}; - -void PlayTask::run() { -#ifdef __sgi - int leftToPlay = m_sndtrack->getSampleCount(); - int i = 0; - - if (!m_devImp->m_port || - (m_devImp->m_currentFormat != m_sndtrack->getFormat()) || - isChangeOutput(m_sndtrack->getSampleRate())) - if (!m_devImp->doOpenDevice(m_sndtrack->getFormat())) return; - - while ((leftToPlay > 0) && m_devImp->m_isPlaying) { - int fillable = alGetFillable(m_devImp->m_port); - if (!fillable) continue; - - if (fillable < leftToPlay) { - alWriteFrames(m_devImp->m_port, (void *)(m_sndtrack->getRawData() + i), - fillable); - // ricorda getSampleSize restituisce m_sampleSize che comprende gia' - // la moltiplicazione per il numero dei canali - i += fillable * m_sndtrack->getSampleSize(); - leftToPlay -= fillable; - } else { - alWriteFrames(m_devImp->m_port, (void *)(m_sndtrack->getRawData() + i), - leftToPlay); - leftToPlay = 0; - } - } - - if (!m_devImp->m_stopped) { - while (ALgetfilled(m_devImp->m_port) > 0) sginap(1); - { - TThread::ScopedLock sl(m_devImp->m_mutex); - if (!m_devImp->m_looped) m_devImp->m_queuedSoundTracks.pop(); - if (m_devImp->m_queuedSoundTracks.empty()) { - m_devImp->m_isPlaying = false; - m_devImp->m_stopped = true; - m_devImp->m_looped = false; - } else { - m_devImp->m_executor.addTask( - new PlayTask(m_devImp, m_devImp->m_queuedSoundTracks.front())); - } - } - } else { - alDiscardFrames(m_devImp->m_port, alGetFilled(m_devImp->m_port)); - while (!m_devImp->m_queuedSoundTracks.empty()) - m_devImp->m_queuedSoundTracks.pop(); - } -#endif -} - -//============================================================================== - -TSoundOutputDevice::TSoundOutputDevice() : m_imp(new TSoundOutputDeviceImp) { -#ifdef __sgi - if (!setDefaultOutput()) - throw TSoundDeviceException(TSoundDeviceException::UnableSetDevice, - "Speaker not supported"); - try { - supportsVolume(); - } catch (TSoundDeviceException &e) { - throw TSoundDeviceException(e.getType(), e.getMessage()); - } - m_imp->insertAllRate(); -#endif -} - -//------------------------------------------------------------------------------ - -TSoundOutputDevice::~TSoundOutputDevice() { - close(); - delete m_imp; -} - -//------------------------------------------------------------------------------ - -bool TSoundOutputDevice::installed() { -#ifdef __sgi - if (alQueryValues(AL_SYSTEM, AL_DEFAULT_OUTPUT, 0, 0, 0, 0) <= 0) - return false; -#endif - return true; -} - -//------------------------------------------------------------------------------ - -bool TSoundOutputDevice::open(const TSoundTrackP &st) { - if (!m_imp->doOpenDevice(st->getFormat())) - throw TSoundDeviceException( - TSoundDeviceException::UnableOpenDevice, - "Problem to open the output device or set some params"); - return true; -} - -//------------------------------------------------------------------------------ - -bool TSoundOutputDevice::close() { -#ifdef __sgi - stop(); - if (m_imp->m_port) alClosePort(m_imp->m_port); - m_imp->m_port = NULL; -#endif - return true; -} - -//------------------------------------------------------------------------------ - -void TSoundOutputDevice::play(const TSoundTrackP &st, TINT32 s0, TINT32 s1, - bool loop, bool scrubbing) { -#ifdef __sgi - if (!st->getSampleCount()) return; - - { - TThread::ScopedLock sl(m_imp->m_mutex); - if (m_imp->m_looped) - throw TSoundDeviceException( - TSoundDeviceException::Busy, - "Unable to queue another playback when the sound player is looping"); - - m_imp->m_isPlaying = true; - m_imp->m_stopped = false; - m_imp->m_looped = loop; - } - - TSoundTrackFormat fmt; - try { - fmt = getPreferredFormat(st->getFormat()); - if (fmt != st->getFormat()) { - throw TSoundDeviceException(TSoundDeviceException::UnsupportedFormat, - "Unsupported Format"); - } - } catch (TSoundDeviceException &e) { - throw TSoundDeviceException(TSoundDeviceException::UnsupportedFormat, - e.getMessage()); - } - - assert(s1 >= s0); - TSoundTrackP subTrack = st->extract(s0, s1); - - // far partire il thread - if (m_imp->m_queuedSoundTracks.empty()) { - m_imp->m_queuedSoundTracks.push(subTrack); - - m_imp->m_executor.addTask(new PlayTask(m_imp, subTrack)); - } else - m_imp->m_queuedSoundTracks.push(subTrack); -#endif -} - -//------------------------------------------------------------------------------ - -void TSoundOutputDevice::stop() { -#ifdef __sgi - if (!m_imp->m_isPlaying) return; - - TThread::ScopedLock sl(m_imp->m_mutex); - m_imp->m_isPlaying = false; - m_imp->m_stopped = true; - m_imp->m_looped = false; -#endif -} - -//------------------------------------------------------------------------------ - -#if 0 -double TSoundOutputDevice::getVolume() { -#ifdef __sgi - ALpv pv[1]; - ALfixed value[2]; - - try { - supportsVolume(); - } catch (TSoundDeviceException &e) { - throw TSoundDeviceException(e.getType(), e.getMessage()); - } - - pv[0].param = AL_GAIN; - pv[0].value.ptr = value; - pv[0].sizeIn = 8; - alGetParams(AL_DEFAULT_OUTPUT, pv, 1); - - double val = - (((alFixedToDouble(value[0]) + alFixedToDouble(value[1])) / 2.) + 60.) / - 8.05; - return val; -#else - return 0.0f; -#endif -} -#endif - -//------------------------------------------------------------------------------ - -#if 0 -bool TSoundOutputDevice::setVolume(double volume) { - ALpv pv[1]; - ALfixed value[2]; - - try { - supportsVolume(); - } catch (TSoundDeviceException &e) { - throw TSoundDeviceException(e.getType(), e.getMessage()); - } - - double val = -60. + 8.05 * volume; - value[0] = alDoubleToFixed(val); - value[1] = alDoubleToFixed(val); - - pv[0].param = AL_GAIN; - pv[0].value.ptr = value; - pv[0].sizeIn = 8; - if (alSetParams(AL_DEFAULT_OUTPUT, pv, 1) < 0) return false; - return true; -} -#endif - -//------------------------------------------------------------------------------ - -#if 0 -bool TSoundOutputDevice::supportsVolume() -{ -#ifdef __sgi - ALparamInfo pinfo; - int ret; - ret = alGetParamInfo(AL_DEFAULT_OUTPUT, AL_GAIN, &pinfo); - double min = alFixedToDouble(pinfo.min.ll); - double max = alFixedToDouble(pinfo.max.ll); - if ((ret != -1) && (min != max) && (max != 0.0)) - return true; - else if ((ret == AL_BAD_PARAM) || ((min == max) && (max == 0.0))) - throw TSoundDeviceException(TSoundDeviceException::UnableVolume, - "It is impossible to chamge setting of volume"); - else - throw TSoundDeviceException(TSoundDeviceException::NoMixer, - "Output device is not accessible"); -#else - return true; -#endif -} -#endif - -//------------------------------------------------------------------------------ - -bool TSoundOutputDevice::isPlaying() const { return m_imp->m_isPlaying; } - -//------------------------------------------------------------------------------ - -bool TSoundOutputDevice::isLooping() { -#ifdef __sgi - TThread::ScopedLock sl(m_imp->m_mutex); - return m_imp->m_looped; -#else - return false; -#endif -} - -//------------------------------------------------------------------------------ - -void TSoundOutputDevice::setLooping(bool loop) { -#ifdef __sgi - TThread::ScopedLock sl(m_imp->m_mutex); - m_imp->m_looped = loop; -#endif -} - -//------------------------------------------------------------------------------ - -TSoundTrackFormat TSoundOutputDevice::getPreferredFormat(TUINT32 sampleRate, - int channelCount, - int bitPerSample) { - TSoundTrackFormat fmt; -#ifdef __sgi - int ret; - - if (!m_imp->verifyRate()) - throw TSoundDeviceException(TSoundDeviceException::UnsupportedFormat, - "There isn't any support rate"); - - if (m_imp->m_supportedRate.find((int)sampleRate) == - m_imp->m_supportedRate.end()) { - std::set::iterator it = - m_imp->m_supportedRate.lower_bound((int)sampleRate); - if (it == m_imp->m_supportedRate.end()) { - it = std::max_element(m_imp->m_supportedRate.begin(), - m_imp->m_supportedRate.end()); - if (it != m_imp->m_supportedRate.end()) - sampleRate = *(m_imp->m_supportedRate.rbegin()); - else - throw TSoundDeviceException(TSoundDeviceException::UnsupportedFormat, - "There isn't a supported rate"); - } else - sampleRate = *it; - } - - int value; - ALvalue vals[32]; - if ((ret = alQueryValues(AL_DEFAULT_OUTPUT, AL_CHANNELS, vals, 32, 0, 0)) > 0) - for (int i = 0; i < ret; ++i) value = vals[i].i; - else if (oserror() == AL_BAD_PARAM) - throw TSoundDeviceException( - TSoundDeviceException::NoMixer, - "It is impossible ask for the max numbers of channels supported"); - else - throw TSoundDeviceException( - TSoundDeviceException::NoMixer, - "It is impossibile information about ouput device"); - - if (value > 2) value = 2; - if (channelCount > value) - channelCount = value; - else if (channelCount <= 0) - channelCount = 1; - - if (bitPerSample <= 8) - bitPerSample = 8; - else if (bitPerSample <= 16) - bitPerSample = 16; - else - bitPerSample = 24; - - fmt.m_bitPerSample = bitPerSample; - fmt.m_channelCount = channelCount; - fmt.m_sampleRate = sampleRate; - fmt.m_signedSample = true; -#endif - - return fmt; -} - -//------------------------------------------------------------------------------ - -TSoundTrackFormat TSoundOutputDevice::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()); - } -} - -//============================================================================== -//============================================================================== -// REGISTRAZIONE -//============================================================================== -//============================================================================== - -class TSoundInputDeviceImp { -public: - ALport m_port; - bool m_stopped; - bool m_isRecording; - bool m_oneShotRecording; - - TINT32 m_recordedSampleCount; - - vector m_recordedBlocks; - vector m_samplePerBlocks; - TSoundTrackFormat m_currentFormat; - TSoundTrackP m_st; - std::set m_supportedRate; - - TThread::Executor m_executor; - - TSoundInputDeviceImp() - : m_stopped(false) - , m_isRecording(false) - , m_port(NULL) - , m_recordedBlocks() - , m_samplePerBlocks() - , m_recordedSampleCount(0) - , m_oneShotRecording(false) - , m_st(0) - , m_supportedRate(){}; - - ~TSoundInputDeviceImp(){}; - - bool doOpenDevice(const TSoundTrackFormat &format, - TSoundInputDevice::Source devType); - void insertAllRate(); - bool verifyRate(); -}; - -bool TSoundInputDeviceImp::doOpenDevice(const TSoundTrackFormat &format, - TSoundInputDevice::Source devType) { -#ifdef __sgi - ALconfig config; - ALpv pvbuf[2]; - - m_currentFormat = format; - - // AL_MONITOR_CTL fa parte dei vecchi andrebbero trovati quelli nuovi - pvbuf[0].param = AL_PORT_COUNT; - pvbuf[1].param = AL_MONITOR_CTL; - if (alGetParams(AL_DEFAULT_INPUT, pvbuf, 2) < 0) - if (oserror() == AL_BAD_DEVICE_ACCESS) - return false; // throw TException("Could not access audio hardware."); - - config = alNewConfig(); - - if (!setDefaultInput(devType)) - return false; // throw TException("Could not set the input device - // specified"); - - pvbuf[0].param = AL_RATE; - pvbuf[0].value.ll = alDoubleToFixed(format.m_sampleRate); - - ALfixed buf[2] = {alDoubleToFixed(0), alDoubleToFixed(0)}; - pvbuf[1].param = AL_GAIN; - pvbuf[1].value.ptr = buf; - pvbuf[1].sizeIn = 8; - - if (alSetParams(AL_DEFAULT_INPUT, pvbuf, 2) < 0) - return false; // throw TException("Problem to set params "); - - if (alSetChannels(config, format.m_channelCount) == -1) - return false; // throw TException("Error to setting audio hardware."); - - int bytePerSample = format.m_bitPerSample >> 3; - switch (bytePerSample) { - case 3: - bytePerSample++; - break; - default: - break; - } - if (alSetWidth(config, bytePerSample) == -1) - return false; // throw TException("Error to setting audio hardware."); - - if (alSetSampFmt(config, AL_SAMPFMT_TWOSCOMP) == -1) - return false; // throw TException("Error to setting audio hardware."); - - if (alSetQueueSize(config, (TINT32)format.m_sampleRate) == -1) - return false; // throw TException("Error to setting audio hardware."); - - alSetDevice(config, AL_DEFAULT_INPUT); - - m_port = alOpenPort("AudioInput", "r", config); - if (!m_port) return false; // throw TException("Could not open audio port."); - - alFreeConfig(config); -#endif - return true; -} - -//----------------------------------------------------------------------------- - -void TSoundInputDeviceImp::insertAllRate() { - m_supportedRate.insert(8000); - m_supportedRate.insert(11025); - m_supportedRate.insert(16000); - m_supportedRate.insert(22050); - m_supportedRate.insert(32000); - m_supportedRate.insert(44100); - m_supportedRate.insert(48000); -} - -//----------------------------------------------------------------------------- - -bool TSoundInputDeviceImp::verifyRate() { -#ifdef __sgi - // Sample Rate - ALparamInfo pinfo; - int ret = alGetParamInfo(AL_DEFAULT_INPUT, AL_RATE, &pinfo); - if (ret != -1 && pinfo.elementType == AL_FIXED_ELEM) { - int min = (int)alFixedToDouble(pinfo.min.ll); - int max = (int)alFixedToDouble(pinfo.max.ll); - - std::set::iterator it; - for (it = m_supportedRate.begin(); it != m_supportedRate.end(); ++it) - if (*it < min || *it > max) m_supportedRate.erase(*it); - if (m_supportedRate.end() == m_supportedRate.begin()) return false; - } else if (ret == AL_BAD_PARAM) - return false; - else - return false; -#endif - return true; -} - -//============================================================================== - -class RecordTask : public TThread::Runnable { -public: - TSoundInputDeviceImp *m_devImp; - int m_ByteToSample; - - RecordTask(TSoundInputDeviceImp *devImp, int numByte) - : TThread::Runnable(), m_devImp(devImp), m_ByteToSample(numByte){}; - - ~RecordTask(){}; - - void run(); -}; - -void RecordTask::run() { -#ifdef __sgi - TINT32 byteRecordedSample = 0; - int filled = alGetFilled(m_devImp->m_port); - - if (m_devImp->m_oneShotRecording) { - char *rawData = m_devImp->m_recordedBlocks.front(); - int sampleSize; - - if ((m_devImp->m_currentFormat.m_bitPerSample >> 3) == 3) - sampleSize = 4; - else - sampleSize = (m_devImp->m_currentFormat.m_bitPerSample >> 3); - - sampleSize *= m_devImp->m_currentFormat.m_channelCount; - while ((byteRecordedSample <= (m_ByteToSample - filled * sampleSize)) && - m_devImp->m_isRecording) { - alReadFrames(m_devImp->m_port, (void *)(rawData + byteRecordedSample), - filled); - byteRecordedSample += filled * sampleSize; - filled = alGetFilled(m_devImp->m_port); - } - - if (m_devImp->m_isRecording) { - alReadFrames(m_devImp->m_port, (void *)(rawData + byteRecordedSample), - (m_ByteToSample - byteRecordedSample) / sampleSize); - while (alGetFillable(m_devImp->m_port) > 0) sginap(1); - } - } else { - while (m_devImp->m_isRecording) { - filled = alGetFilled(m_devImp->m_port); - if (filled > 0) { - char *dataBuffer = new char[filled * m_ByteToSample]; - m_devImp->m_recordedBlocks.push_back(dataBuffer); - m_devImp->m_samplePerBlocks.push_back(filled * m_ByteToSample); - - alReadFrames(m_devImp->m_port, (void *)dataBuffer, filled); - m_devImp->m_recordedSampleCount += filled; - } - } - while (alGetFillable(m_devImp->m_port) > 0) sginap(1); - } - alClosePort(m_devImp->m_port); - m_devImp->m_port = 0; - m_devImp->m_stopped = true; -#endif -} - -//============================================================================== - -TSoundInputDevice::TSoundInputDevice() : m_imp(new TSoundInputDeviceImp) {} - -//------------------------------------------------------------------------------ - -TSoundInputDevice::~TSoundInputDevice() { -#ifdef __sgi - if (m_imp->m_port) alClosePort(m_imp->m_port); -#endif - delete m_imp; -} - -//------------------------------------------------------------------------------ - -bool TSoundInputDevice::installed() { -#ifdef __sgi - if (alQueryValues(AL_SYSTEM, AL_DEFAULT_INPUT, 0, 0, 0, 0) <= 0) return false; -#endif - return true; -} - -//------------------------------------------------------------------------------ - -void TSoundInputDevice::record(const TSoundTrackFormat &format, - TSoundInputDevice::Source type) { - if (m_imp->m_isRecording == true) - throw TSoundDeviceException(TSoundDeviceException::Busy, - "Just another recoding is in progress"); - - m_imp->m_recordedBlocks.clear(); - m_imp->m_samplePerBlocks.clear(); - - // registra creando una nuova traccia - m_imp->m_oneShotRecording = false; - - if (!setDefaultInput(type)) - throw TSoundDeviceException(TSoundDeviceException::UnableSetDevice, - "Error to set the input device"); - - m_imp->insertAllRate(); - TSoundTrackFormat fmt; - - try { - fmt = getPreferredFormat(format); - if (fmt != format) { - throw TSoundDeviceException(TSoundDeviceException::UnsupportedFormat, - "Unsupported Format"); - } - } catch (TSoundDeviceException &e) { - throw TSoundDeviceException(TSoundDeviceException::UnsupportedFormat, - e.getMessage()); - } - - if (!m_imp->m_port) m_imp->doOpenDevice(format, type); - - m_imp->m_currentFormat = format; - m_imp->m_isRecording = true; - m_imp->m_stopped = false; - m_imp->m_recordedSampleCount = 0; - - int bytePerSample = format.m_bitPerSample >> 3; - switch (bytePerSample) { - case 3: - bytePerSample++; - break; - default: - break; - } - bytePerSample *= format.m_channelCount; - - // far partire il thread - /*TRecordThread *recordThread = new TRecordThread(m_imp, bytePerSample); - if (!recordThread) - { - m_imp->m_isRecording = false; - m_imp->m_stopped = true; -throw TSoundDeviceException( -TSoundDeviceException::UnablePrepare, -"Unable to create the recording thread"); - } -recordThread->start();*/ - m_imp->m_executor.addTask(new RecordTask(m_imp, bytePerSample)); -} - -//------------------------------------------------------------------------------ - -void TSoundInputDevice::record(const TSoundTrackP &st, - TSoundInputDevice::Source type) { - if (m_imp->m_isRecording == true) - throw TSoundDeviceException(TSoundDeviceException::Busy, - "Just another recoding is in progress"); - - m_imp->m_recordedBlocks.clear(); - m_imp->m_samplePerBlocks.clear(); - - if (!setDefaultInput(type)) - throw TSoundDeviceException(TSoundDeviceException::UnableSetDevice, - "Error to set the input device"); - - m_imp->insertAllRate(); - TSoundTrackFormat fmt; - - try { - fmt = getPreferredFormat(st->getFormat()); - if (fmt != st->getFormat()) { - throw TSoundDeviceException(TSoundDeviceException::UnsupportedFormat, - "Unsupported Format"); - } - } catch (TSoundDeviceException &e) { - throw TSoundDeviceException(TSoundDeviceException::UnsupportedFormat, - e.getMessage()); - } - - if (!m_imp->m_port) - if (!m_imp->doOpenDevice(st->getFormat(), type)) - throw TSoundDeviceException(TSoundDeviceException::UnableOpenDevice, - "Unable to open input device"); - - // Sovrascive un'intera o parte di traccia gia' esistente - m_imp->m_oneShotRecording = true; - m_imp->m_currentFormat = st->getFormat(); - m_imp->m_isRecording = true; - m_imp->m_stopped = false; - m_imp->m_recordedSampleCount = 0; - m_imp->m_st = st; - - m_imp->m_recordedBlocks.push_back((char *)st->getRawData()); - - int totByteToSample = st->getSampleCount() * st->getSampleSize(); - - // far partire il thread - /*TRecordThread *recordThread = new TRecordThread(m_imp, totByteToSample); - if (!recordThread) - { - m_imp->m_isRecording = false; - m_imp->m_stopped = true; -throw TSoundDeviceException( -TSoundDeviceException::UnablePrepare, -"Unable to create the recording thread"); - } -recordThread->start();*/ - m_imp->m_executor.addTask(new RecordTask(m_imp, totByteToSample)); -} - -//------------------------------------------------------------------------------ - -TSoundTrackP TSoundInputDevice::stop() { - TSoundTrackP st; -#ifdef __sgi - if (!m_imp->m_isRecording) - throw TSoundDeviceException(TSoundDeviceException::UnablePrepare, - "No recording process is in execution"); - - m_imp->m_isRecording = false; - - alDiscardFrames(m_imp->m_port, alGetFilled(m_imp->m_port)); - - while (!m_imp->m_stopped) sginap(1); - - if (m_imp->m_oneShotRecording) - st = m_imp->m_st; - else { - st = TSoundTrack::create(m_imp->m_currentFormat, - m_imp->m_recordedSampleCount); - TINT32 bytesCopied = 0; - - for (int i = 0; i < (int)m_imp->m_recordedBlocks.size(); ++i) { - memcpy((void *)(st->getRawData() + bytesCopied), - m_imp->m_recordedBlocks[i], m_imp->m_samplePerBlocks[i]); - - delete[] m_imp->m_recordedBlocks[i]; - - bytesCopied += m_imp->m_samplePerBlocks[i]; - } - m_imp->m_samplePerBlocks.clear(); - } -#endif - return st; -} - -//------------------------------------------------------------------------------ - -double TSoundInputDevice::getVolume() { -#ifdef __sgi - ALpv pv[1]; - ALfixed value[2]; - - try { - supportsVolume(); - } catch (TSoundDeviceException &e) { - throw TSoundDeviceException(e.getType(), e.getMessage()); - } - - pv[0].param = AL_GAIN; - pv[0].value.ptr = value; - pv[0].sizeIn = 8; - alGetParams(AL_DEFAULT_INPUT, pv, 1); - - double val = - (((alFixedToDouble(value[0]) + alFixedToDouble(value[1])) / 2.) + 60.) / - 8.05; - return val; -#else - return 0.0f; -#endif -} - -//------------------------------------------------------------------------------ - -bool TSoundInputDevice::setVolume(double volume) { -#ifdef __sgi - ALpv pv[1]; - ALfixed value[2]; - - try { - supportsVolume(); - } catch (TSoundDeviceException &e) { - throw TSoundDeviceException(e.getType(), e.getMessage()); - } - - double val = -60. + 8.05 * volume; - value[0] = alDoubleToFixed(val); - value[1] = alDoubleToFixed(val); - - pv[0].param = AL_GAIN; - pv[0].value.ptr = value; - pv[0].sizeIn = 8; - alSetParams(AL_DEFAULT_INPUT, pv, 1); -#endif - return true; -} - -//------------------------------------------------------------------------------ - -bool TSoundInputDevice::supportsVolume() { -#ifdef __sgi - ALparamInfo pinfo; - int ret; - ret = alGetParamInfo(AL_DEFAULT_INPUT, AL_GAIN, &pinfo); - double min = alFixedToDouble(pinfo.min.ll); - double max = alFixedToDouble(pinfo.max.ll); - if ((ret != -1) && (min != max) && (max != 0.0)) - return true; - else if ((ret == AL_BAD_PARAM) || ((min == max) && (max == 0.0))) - throw TSoundDeviceException(TSoundDeviceException::UnableVolume, - "It is impossible to chamge setting of volume"); - else - throw TSoundDeviceException(TSoundDeviceException::NoMixer, - "Output device is not accessible"); -#endif -} - -//------------------------------------------------------------------------------ - -TSoundTrackFormat TSoundInputDevice::getPreferredFormat(TUINT32 sampleRate, - int channelCount, - int bitPerSample) { - TSoundTrackFormat fmt; -#ifdef __sgi - int ret; - - if (!m_imp->verifyRate()) - throw TSoundDeviceException(TSoundDeviceException::UnsupportedFormat, - "There isn't any support rate"); - - if (m_imp->m_supportedRate.find((int)sampleRate) == - m_imp->m_supportedRate.end()) { - std::set::iterator it = - m_imp->m_supportedRate.lower_bound((int)sampleRate); - if (it == m_imp->m_supportedRate.end()) { - it = std::max_element(m_imp->m_supportedRate.begin(), - m_imp->m_supportedRate.end()); - if (it != m_imp->m_supportedRate.end()) - sampleRate = *(m_imp->m_supportedRate.rbegin()); - else - throw TSoundDeviceException(TSoundDeviceException::UnsupportedFormat, - "There isn't a supported rate"); - } else - sampleRate = *it; - } - - int value; - ALvalue vals[32]; - if ((ret = alQueryValues(AL_DEFAULT_INPUT, AL_CHANNELS, vals, 32, 0, 0)) > 0) - for (int i = 0; i < ret; ++i) value = vals[i].i; - else if (oserror() == AL_BAD_PARAM) - throw TSoundDeviceException( - TSoundDeviceException::NoMixer, - "It is impossible ask for the max nembers of channels supported"); - else - throw TSoundDeviceException( - TSoundDeviceException::NoMixer, - "It is impossibile information about ouput device"); - - if (value > 2) value = 2; - if (channelCount > value) - channelCount = value; - else if (channelCount <= 0) - channelCount = 1; - - if (bitPerSample <= 8) - bitPerSample = 8; - else if (bitPerSample <= 16) - bitPerSample = 16; - else - bitPerSample = 24; - - fmt.m_bitPerSample = bitPerSample; - fmt.m_channelCount = channelCount; - fmt.m_sampleRate = sampleRate; - fmt.m_signedSample = true; -#endif - return fmt; -} - -//------------------------------------------------------------------------------ - -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()); - } -} - -//------------------------------------------------------------------------------ - -bool TSoundInputDevice::isRecording() { return m_imp->m_isRecording; } - -//****************************************************************************** -//****************************************************************************** -// funzioni per l'interazione con -// la -// libreria -// audio -//****************************************************************************** -//****************************************************************************** -namespace { -bool isInterfaceSupported(int deviceType, int interfaceType) { -#ifdef __sgi - ALvalue vals[16]; - int devNum; - - if ((devNum = alQueryValues(AL_SYSTEM, deviceType, vals, 16, 0, 0)) > 0) { - int i; - for (i = 0; i < devNum; ++i) { - ALpv quals[2]; - quals[0].param = AL_TYPE; - quals[0].value.i = interfaceType; - if (alQueryValues(vals[i].i, AL_INTERFACE, 0, 0, quals, 1) > 0) - return true; - } - } -#endif - return false; -} - -//------------------------------------------------------------------------------ - -bool setDefaultInput(TSoundInputDevice::Source type) { -#ifdef __sgi - string label; - - switch (type) { - case TSoundInputDevice::LineIn: - label = "Line In"; - break; - case TSoundInputDevice::DigitalIn: - label = "AES Input"; - break; - default: - label = "Microphone"; - } - - int dev = - alGetResourceByName(AL_SYSTEM, (char *)label.c_str(), AL_DEVICE_TYPE); - if (!dev) return false; // throw TException("Error to set input device"); - int itf; - if (itf = alGetResourceByName(AL_SYSTEM, (char *)label.c_str(), - AL_INTERFACE_TYPE)) { - ALpv p; - - p.param = AL_INTERFACE; - p.value.i = itf; - if (alSetParams(dev, &p, 1) < 0 || p.sizeOut < 0) - return false; // throw TException("Error to set input device"); - } - - ALpv param; - - param.param = AL_DEFAULT_INPUT; - param.value.i = dev; - if (alSetParams(AL_SYSTEM, ¶m, 1) < 0) - return false; // throw TException("Error to set input device"); -#endif - return true; -} - -//------------------------------------------------------------------------------ - -bool setDefaultOutput() { -#ifdef __sgi - ALpv pvbuf[1]; - - if (!isInterfaceSupported(AL_DEFAULT_OUTPUT, AL_SPEAKER_IF_TYPE)) - return false; // throw TException("Speakers are not supported"); - - int dev = alGetResourceByName(AL_SYSTEM, (char *)"Headphone/Speaker", - AL_DEVICE_TYPE); - if (!dev) return false; // throw TException("invalid device speakers"); - - pvbuf[0].param = AL_DEFAULT_OUTPUT; - pvbuf[0].value.i = dev; - alSetParams(AL_SYSTEM, pvbuf, 1); - - // qui devo metterci gli altoparlanti e poi setto i valori per il default - // output - pvbuf[0].param = AL_INTERFACE; - pvbuf[0].value.i = AL_SPEAKER_IF_TYPE; - - if (alSetParams(AL_DEFAULT_OUTPUT, pvbuf, 1) < 0) - return false; // throw TException("Unable to set output device params"); -#endif - return true; -} - -//------------------------------------------------------------------------------ - -// return the indexes of all input device of a particular type -list getInputDevice(int deviceType) { - ALvalue vals[16]; - ALpv quals[1]; - list devList; -#ifdef __sgi - int devNum; - - quals[0].param = AL_TYPE; - quals[0].value.i = deviceType; - if ((devNum = alQueryValues(AL_SYSTEM, AL_DEFAULT_INPUT, vals, 16, quals, - 1)) > 0) { - int i; - for (i = 0; i < devNum; ++i) { - int itf; - ALvalue val[16]; - if ((itf = alQueryValues(i, AL_INTERFACE, val, 16, 0, 0)) > 0) { - int j; - for (j = 0; j < itf; ++j) devList.push_back(vals[j].i); - } - } - } -#endif - return devList; -} - -//------------------------------------------------------------------------------ - -// return the indexes of all input device of a particular type and interface -list getInputDevice(int deviceType, int itfType) { - ALvalue vals[16]; - ALpv quals[1]; - list devList; - int devNum; -#ifdef __sgi - - quals[0].param = AL_TYPE; - quals[0].value.i = deviceType; - if ((devNum = alQueryValues(AL_SYSTEM, AL_DEFAULT_INPUT, vals, 16, quals, - 1)) > 0) { - int i; - for (i = 0; i < devNum; ++i) { - int itf; - ALvalue val[16]; - quals[0].param = AL_TYPE; - quals[0].value.i = itfType; - if ((itf = alQueryValues(i, AL_INTERFACE, val, 16, quals, 1)) > 0) { - int j; - for (j = 0; j < itf; ++j) devList.push_back(vals[j].i); - } - } - } -#endif - return devList; -} - -//------------------------------------------------------------------------------ - -string getResourceLabel(int resourceID) { -#ifdef __sgi - ALpv par[1]; - char l[32]; - - par[0].param = AL_LABEL; - par[0].value.ptr = l; - par[0].sizeIn = 32; - - alGetParams(resourceID, par, 1); - - return string(l); -#else - return ""; -#endif -} - -//------------------------------------------------------------------------------ - -// verify the samplerate of the select device is changed from another -// application -bool isChangeOutput(ULONG sampleRate) { -#ifdef __sgi - ALpv par[2]; - char l[32]; - - par[0].param = AL_LABEL; - par[0].value.ptr = l; - par[0].sizeIn = 32; - par[1].param = AL_RATE; - - alGetParams(AL_DEFAULT_OUTPUT, par, 2); - if ((strcmp(l, "Analog Out") != 0) || - (alFixedToDouble(par[1].value.ll) != sampleRate)) - return true; - else - return false; -#else - return true; -#endif -} -} diff --git a/toonz/sources/include/tsound.h b/toonz/sources/include/tsound.h index f635fa93..b75b1507 100644 --- a/toonz/sources/include/tsound.h +++ b/toonz/sources/include/tsound.h @@ -372,7 +372,7 @@ Returns true if on the machine there is an audio card installed correctly bool isFormatSupported(const TSoundTrackFormat &); -#ifdef MACOSX +#ifndef _WIN32 //! Returns true if is possible to change volume setting on current input //! interface bool supportsVolume(); @@ -412,7 +412,7 @@ interaction between sound and mouse //! Returns if the device is busy with a playback bool isPlaying() const; -#ifndef MACOSX +#ifdef _WIN32 //! Return true if the playback of all soundtracks is ended. bool isAllQueuedItemsPlayed(); #endif diff --git a/toonz/sources/tnzcore/CMakeLists.txt b/toonz/sources/tnzcore/CMakeLists.txt index a46db6d8..16603f9f 100644 --- a/toonz/sources/tnzcore/CMakeLists.txt +++ b/toonz/sources/tnzcore/CMakeLists.txt @@ -252,18 +252,12 @@ if(BUILD_TARGET_WIN) ) elseif(BUILD_TARGET_APPLE) set(SOURCES ${SOURCES} - ../common/tsound/tsound_mac.cpp + ../common/tsound/tsound_qt.cpp ) elseif(BUILD_TARGET_UNIX) - if(SDL_LIB_FOUND) - set(SOURCES ${SOURCES} - ../common/tsound/tsound_sdl.cpp - ) - else() - set(SOURCES ${SOURCES} - ../common/tsound/tsound_x.cpp - ) - endif() + set(SOURCES ${SOURCES} + ../common/tsound/tsound_qt.cpp + ) endif() add_translation(tnzcore ${HEADERS} ${SOURCES}) diff --git a/toonz/sources/toonzlib/txshsoundcolumn.cpp b/toonz/sources/toonzlib/txshsoundcolumn.cpp index 98c22655..63a94746 100644 --- a/toonz/sources/toonzlib/txshsoundcolumn.cpp +++ b/toonz/sources/toonzlib/txshsoundcolumn.cpp @@ -788,7 +788,7 @@ void TXshSoundColumn::updateFrameRate(double fps) { void TXshSoundColumn::setVolume(double value) { m_volume = tcrop(value, 0.0, 1.0); if (m_player && m_player->isPlaying()) -#ifdef MACOSX +#ifndef _WIN32 m_player->setVolume(m_volume); #else stop(); @@ -808,7 +808,7 @@ void TXshSoundColumn::play(TSoundTrackP soundtrack, int s0, int s1, bool loop) { if (m_player) { try { -#ifdef MACOSX +#ifndef _WIN32 m_player->prepareVolume(m_volume); TSoundTrackP mixedTrack = soundtrack; #else @@ -818,7 +818,7 @@ void TXshSoundColumn::play(TSoundTrackP soundtrack, int s0, int s1, bool loop) { #endif m_player->play(mixedTrack, s0, s1, loop); m_currentPlaySoundTrack = mixedTrack; -#ifndef MACOSX +#ifdef _WIN32 m_timer.start(); #endif } catch (TSoundDeviceException &) { @@ -934,7 +934,7 @@ void TXshSoundColumn::clear() { //----------------------------------------------------------------------------- void TXshSoundColumn::onTimerOut() { -#ifndef MACOSX +#ifdef _WIN32 if (m_player && m_player->isAllQueuedItemsPlayed()) stop(); #endif } @@ -1002,7 +1002,7 @@ TSoundTrackP TXshSoundColumn::getOverallSoundTrack(int fromFrame, int toFrame, // We prefer to have 22050 as a maximum sampleRate (to avoid crashes or // another issues) -#ifndef MACOSX +#ifdef _WIN32 if (format.m_sampleRate >= 44100) format.m_sampleRate = 22050; #else QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());