tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_WaveOutputDevice.cpp
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11namespace tracktion { inline namespace engine
12{
13
14WaveOutputDevice::WaveOutputDevice (Engine& e, const WaveDeviceDescription& desc)
15 : OutputDevice (e, NEEDS_TRANS("Wave Audio Output"), desc.name, desc.name),
16 deviceChannels (desc.channels),
17 channelSet (createChannelSet (desc.channels)),
18 ditheringEnabled (false),
19 leftRightReversed (false)
20{
21 enabled = desc.enabled;
22 loadProps();
23}
24
25WaveOutputDevice::~WaveOutputDevice()
26{
27 notifyListenersOfDeletion();
28 saveProps();
29}
30
31void WaveOutputDevice::resetToDefault()
32{
33 engine.getPropertyStorage().removePropertyItem (SettingID::waveout, getName());
34 loadProps();
35}
36
37void WaveOutputDevice::setEnabled (bool b)
38{
39 if (enabled != b)
40 {
41 enabled = b;
42 changed();
43 engine.getDeviceManager().setWaveOutChannelsEnabled (deviceChannels, b);
44 // do nothing now! this object is probably deleted..
45 }
46}
47
48juce::String WaveOutputDevice::openDevice()
49{
50 return {};
51}
52
53void WaveOutputDevice::closeDevice()
54{
55}
56
57//==============================================================================
58void WaveOutputDevice::loadProps()
59{
60 ditheringEnabled = false;
61 leftRightReversed = false;
62
63 if (auto n = engine.getPropertyStorage().getXmlPropertyItem (SettingID::waveout, getName()))
64 {
65 ditheringEnabled = n->getBoolAttribute ("dithering", ditheringEnabled);
66 leftRightReversed = n->getBoolAttribute ("reversed", leftRightReversed);
67 }
68}
69
70void WaveOutputDevice::saveProps()
71{
72 juce::XmlElement n ("SETTINGS");
73
74 n.setAttribute ("dithering", ditheringEnabled);
75 n.setAttribute ("reversed", leftRightReversed);
76
77 engine.getPropertyStorage().setXmlPropertyItem (SettingID::waveout, getName(), n);
78}
79
80void WaveOutputDevice::reverseChannels (bool shouldReverse)
81{
82 jassert (isStereoPair());
83
84 if (leftRightReversed != shouldReverse)
85 {
86 leftRightReversed = shouldReverse;
87 changed();
88 saveProps();
89 }
90}
91
92void WaveOutputDevice::setDithered (bool dither)
93{
94 if (ditheringEnabled != dither)
95 {
96 ditheringEnabled = dither;
97 changed();
98 saveProps();
99 }
100}
101
102//==============================================================================
103WaveOutputDeviceInstance* WaveOutputDevice::createInstance (EditPlaybackContext& c)
104{
105 return new WaveOutputDeviceInstance (*this, c);
106}
107
108//==============================================================================
109WaveOutputDeviceInstance::WaveOutputDeviceInstance (WaveOutputDevice& d, EditPlaybackContext& c)
110 : OutputDeviceInstance (d, c),
111 outputBuffer (1, 128)
112{
113}
114
115void WaveOutputDeviceInstance::prepareToPlay (double, int blockSize)
116{
117 outputBuffer.setSize (2, blockSize);
118
119 int ditherDepth = juce::jlimit (16, 32, edit.engine.getDeviceManager().getBitDepth());
120
121 ditherers[0].reset (ditherDepth);
122 ditherers[1].reset (ditherDepth);
123}
124
125//==============================================================================
126int WaveOutputDevice::getLeftChannel() const
127{
128 return deviceChannels.size() >= 1 ? deviceChannels[0].indexInDevice : -1;
129}
130
131int WaveOutputDevice::getRightChannel() const
132{
133 return deviceChannels.size() >= 2 ? deviceChannels[1].indexInDevice : -1;
134}
135
136bool WaveOutputDevice::isStereoPair() const
137{
138 return deviceChannels.size() == 2;
139}
140
141void WaveOutputDevice::setStereoPair (bool stereo)
142{
143 auto& dm = engine.getDeviceManager();
144
145 if (deviceChannels.size() == 2)
146 dm.setDeviceOutChannelStereo (std::max (getLeftChannel(), getRightChannel()), stereo);
147 else if (deviceChannels.size() == 1)
148 dm.setDeviceOutChannelStereo (getLeftChannel(), stereo);
149}
150
151}} // namespace tracktion { inline namespace engine
#define NEEDS_TRANS(stringLiteral)
#define jassert(expression)
T max(T... args)
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
juce::String getName(LaunchQType t)
Retuns the name of a LaunchQType for display purposes.
juce::AudioChannelSet createChannelSet(const std::vector< ChannelIndex > &channels)
Creates an AudioChannelSet for a list of ChannelIndexes.