aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/src/openmpt/sounddevice/SoundDeviceRtAudio.hpp
blob: 0a414bee878f3a96d58b423ba2ce7b8a93581fbb (plain) (blame)
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
/* SPDX-License-Identifier: BSD-3-Clause */
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */


#pragma once

#include "openmpt/all/BuildSettings.hpp"

#include "SoundDeviceBase.hpp"

#include "openmpt/base/Types.hpp"
#include "openmpt/logging/Logger.hpp"

#include <atomic>
#include <memory>
#include <vector>

#ifdef MPT_WITH_RTAUDIO
#if MPT_COMPILER_MSVC
#pragma warning(push)
#pragma warning(disable : 4244)  // conversion from 'int' to 'unsigned char', possible loss of data
#endif
#if MPT_COMPILER_GCC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-copy"
#endif
#include <RtAudio.h>
#if MPT_COMPILER_GCC
#pragma GCC diagnostic pop
#endif
#if MPT_COMPILER_MSVC
#pragma warning(pop)
#endif
#endif  // MPT_WITH_RTAUDIO

OPENMPT_NAMESPACE_BEGIN

namespace SoundDevice
{


#ifdef MPT_WITH_RTAUDIO


class CRtAudioDevice : public SoundDevice::Base
{

protected:
	std::unique_ptr<RtAudio> m_RtAudio;

	RtAudio::StreamParameters m_InputStreamParameters;
	RtAudio::StreamParameters m_OutputStreamParameters;
	unsigned int m_FramesPerChunk;
	RtAudio::StreamOptions m_StreamOptions;

	void *m_CurrentFrameBufferOutput;
	void *m_CurrentFrameBufferInput;
	unsigned int m_CurrentFrameBufferCount;
	double m_CurrentStreamTime;

	std::atomic<uint32> m_StatisticLatencyFrames;
	std::atomic<uint32> m_StatisticPeriodFrames;

public:
	CRtAudioDevice(ILogger &logger, SoundDevice::Info info, SoundDevice::SysInfo sysInfo);
	~CRtAudioDevice();

public:
	bool InternalOpen();
	bool InternalClose();
	void InternalFillAudioBuffer();
	bool InternalStart();
	void InternalStop();
	bool InternalIsOpen() const { return m_RtAudio && m_RtAudio->isStreamOpen(); }
	bool InternalHasGetStreamPosition() const { return true; }
	int64 InternalGetStreamPositionFrames() const;
	SoundDevice::BufferAttributes InternalGetEffectiveBufferAttributes() const;
	SoundDevice::Statistics GetStatistics() const;
	SoundDevice::Caps InternalGetDeviceCaps();
	SoundDevice::DynamicCaps GetDeviceDynamicCaps(const std::vector<uint32> &baseSampleRates);

private:
	void SendError(const RtAudioError &e);

	void AudioCallback(void *outputBuffer, void *inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status);

	static int RtAudioCallback(void *outputBuffer, void *inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status, void *userData);

	static RtAudio::Api GetApi(SoundDevice::Info info);
	static unsigned int GetDevice(SoundDevice::Info info);

public:
	static std::unique_ptr<SoundDevice::BackendInitializer> BackendInitializer() { return std::make_unique<SoundDevice::BackendInitializer>(); }
	static std::vector<SoundDevice::Info> EnumerateDevices(ILogger &logger, SoundDevice::SysInfo sysInfo);
};


#endif  // MPT_WITH_RTAUDIO


}  // namespace SoundDevice


OPENMPT_NAMESPACE_END