2014-11-17 03:49:20 +08:00
|
|
|
/*
|
|
|
|
Copyright (C) 2014 by Project Tox <https://tox.im>
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
This program is libre 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 3 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 COPYING file for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
#ifndef AUDIO_H
|
|
|
|
#define AUDIO_H
|
|
|
|
|
2014-11-20 05:26:04 +08:00
|
|
|
#include <QObject>
|
|
|
|
#include <QHash>
|
2014-11-17 02:04:45 +08:00
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
|
|
#include <OpenAL/al.h>
|
|
|
|
#include <OpenAL/alc.h>
|
|
|
|
#else
|
|
|
|
#include <AL/al.h>
|
|
|
|
#include <AL/alc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class QString;
|
|
|
|
class QByteArray;
|
2014-11-20 05:26:04 +08:00
|
|
|
class QTimer;
|
|
|
|
class QThread;
|
2015-01-05 00:55:39 +08:00
|
|
|
class QMutex;
|
2014-11-20 05:26:04 +08:00
|
|
|
struct Tox;
|
2014-11-17 02:04:45 +08:00
|
|
|
|
2014-11-20 05:26:04 +08:00
|
|
|
class Audio : QObject
|
2014-11-17 02:04:45 +08:00
|
|
|
{
|
2014-11-20 05:26:04 +08:00
|
|
|
Q_OBJECT
|
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
public:
|
2014-11-20 05:26:04 +08:00
|
|
|
static Audio& getInstance(); ///< Returns the singleton's instance. Will construct on first call.
|
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
static void suscribeInput(); ///< Call when you need to capture sound from the open input device.
|
|
|
|
static void unsuscribeInput(); ///< Call once you don't need to capture on the open input device anymore.
|
|
|
|
|
|
|
|
static void openInput(const QString& inDevDescr); ///< Open an input device, use before suscribing
|
|
|
|
static void openOutput(const QString& outDevDescr); ///< Open an output device
|
|
|
|
|
|
|
|
static void closeInput(); ///< Close an input device, please don't use unless everyone's unsuscribed
|
|
|
|
static void closeOutput(); ///< Close an output device
|
|
|
|
|
2015-01-05 00:55:39 +08:00
|
|
|
static bool isInputReady(); ///< Returns true if the input device is open and suscribed to
|
|
|
|
static bool isOutputClosed(); ///< Returns true if the output device is open
|
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
static void playMono16Sound(const QByteArray& data); ///< Play a 44100Hz mono 16bit PCM sound
|
2015-01-05 00:55:39 +08:00
|
|
|
static bool tryCaptureSamples(uint8_t* buf, int framesize); ///< Does nothing and return false on failure
|
2014-11-17 02:04:45 +08:00
|
|
|
|
2014-11-20 05:26:04 +08:00
|
|
|
/// May be called from any thread, will always queue a call to playGroupAudio
|
|
|
|
/// The first and last argument are ignored, but allow direct compatibility with toxcore
|
|
|
|
static void playGroupAudioQueued(Tox*, int group, int peer, const int16_t* data,
|
|
|
|
unsigned samples, uint8_t channels, unsigned sample_rate, void*);
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
/// Must be called from the audio thread, plays a group call's received audio
|
|
|
|
void playGroupAudio(int group, int peer, const int16_t* data,
|
|
|
|
unsigned samples, uint8_t channels, unsigned sample_rate);
|
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
public:
|
2014-11-20 05:26:04 +08:00
|
|
|
static QThread* audioThread;
|
2014-11-17 02:04:45 +08:00
|
|
|
static ALCcontext* alContext;
|
|
|
|
static ALuint alMainSource;
|
|
|
|
|
|
|
|
private:
|
2014-11-20 05:26:04 +08:00
|
|
|
explicit Audio()=default;
|
2015-01-24 22:31:11 +08:00
|
|
|
~Audio();
|
2014-11-20 05:26:04 +08:00
|
|
|
static void playAudioBuffer(ALuint alSource, const int16_t *data, int samples, unsigned channels, int sampleRate);
|
2014-11-17 02:04:45 +08:00
|
|
|
|
|
|
|
private:
|
2014-11-20 05:26:04 +08:00
|
|
|
static Audio* instance;
|
2014-11-17 02:04:45 +08:00
|
|
|
static std::atomic<int> userCount;
|
2015-01-05 00:55:39 +08:00
|
|
|
static ALCdevice* alOutDev, *alInDev;
|
|
|
|
static QMutex* audioInLock, *audioOutLock;
|
2014-11-17 02:04:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // AUDIO_H
|