VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
AudioPort.h
1// -----------------------------------------------------------------------------
2// This file is part of VirtualC64
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// This FILE is dual-licensed. You are free to choose between:
6//
7// - The GNU General Public License v3 (or any later version)
8// - The Mozilla Public License v2
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0
11// -----------------------------------------------------------------------------
12
13#pragma once
14
15#include "AudioPortTypes.h"
16#include "SIDTypes.h"
17#include "SubComponent.h"
18#include "Concurrency.h"
19#include "Volume.h"
20
21namespace vc64 {
22
23class SIDBridge;
24
25class AudioPort final :
26public SubComponent,
27public Inspectable<AudioPortInfo, AudioPortStats>,
28public util::RingBuffer <SamplePair, 12288> {
29
30 Descriptions descriptions = {{
31
32 .name = "Audio",
33 .description = "Audio Port"
34 }};
35
36 ConfigOptions options = {
37
48 };
49
50 // Current configuration
51 AudioPortConfig config = { };
52
53 // Time stamp of the last write pointer alignment
54 util::Time lastAlignment;
55
56 // Sample rate adjustment
57 double sampleRateCorrection = 0.0;
58
59 // Channel volumes
60 float vol[4] = { };
61
62 // Panning factors
63 float pan[4] ={ };
64
65 // Master volumes (fadable)
66 Volume volL;
67 Volume volR;
68
69 // Used to determine if a MSG_MUTE should be sent to the GUI
70 bool muted = false;
71
72
73 //
74 // Methods
75 //
76
77public:
78
79 AudioPort(C64 &ref) : SubComponent(ref) { };
80
81 AudioPort& operator= (const AudioPort& other) {
82
83 CLONE(lastAlignment)
84 CLONE(config)
85
86 return *this;
87 }
88
89
90 //
91 // Methods from Serializable
92 //
93
94public:
95
96 template <class T>
97 void serialize(T& worker)
98 {
99 if (isResetter(worker)) return;
100
101 worker
102
103 << config.vol
104 << config.pan
105 << config.volL
106 << config.volR;
107
108 } SERIALIZERS(serialize);
109
110
111 //
112 // Methods from CoreComponent
113 //
114
115public:
116
117 const Descriptions &getDescriptions() const override { return descriptions; }
118
119private:
120
121 void _dump(Category category, std::ostream& os) const override;
122 void _reset(bool hard) override;
123 void _powerOn() override;
124 void _run() override;
125 void _pause() override;
126 void _warpOn() override;
127 void _warpOff() override;
128 void _focus() override;
129 void _unfocus() override;
130
131
132 //
133 // Methods from Inspectable
134 //
135
136public:
137
138 void cacheInfo(AudioPortInfo &result) const override;
139 void cacheStats(AudioPortStats &result) const override;
140
141
142 //
143 // Methods from Configurable
144 //
145
146public:
147
148 const AudioPortConfig &getConfig() const { return config; }
149 const ConfigOptions &getOptions() const override { return options; }
150 i64 getOption(Option opt) const override;
151 void checkOption(Option opt, i64 value) override;
152 void setOption(Option opt, i64 value) override;
153
154
155 //
156 // Managing the ring buffer
157 //
158
159public:
160
161 // Puts the write pointer somewhat ahead of the read pointer
162 void alignWritePtr();
163
164 /* Handles a buffer underflow condition. A buffer underflow occurs when the
165 * audio device of the host machine needs sound samples than SID hasn't
166 * produced, yet.
167 */
168 void handleBufferUnderflow();
169
170 /* Handles a buffer overflow condition. A buffer overflow occurs when SID
171 * is producing more samples than the audio device of the host machine is
172 * able to consume.
173 */
174 void handleBufferOverflow();
175
176 // Reduces the sample count to the specified number
177 void clamp(isize maxSamples);
178
179
180 //
181 // Generating audio samples
182 //
183
184public:
185
186 // Generates samples
187 void generateSamples();
188
189 // Returns the sample rate adjustment
190 double getSampleRateCorrection() { return sampleRateCorrection; }
191
192 // Rescale the existing samples to gradually fade out (to avoid cracks)
193 void fadeOut();
194
195 // Gradually decrease the master volume to zero
196 void mute() { volL.mute(); volR.mute(); }
197 void mute(isize steps) { volL.mute(steps); volR.mute(steps); }
198
199 // Gradually inrease the master volume to max
200 void unmute() { volL.unmute(); volR.unmute(); }
201 void unmute(isize steps) { volL.unmute(steps); volR.unmute(steps); }
202
203 // Checks whether the volume settings result in a zeroed-out audio stream
204 // bool zeroMasterVolume() const { return volL.current == 0.0 && volR.current == 0.0; }
205
206private:
207
208 // Generates samples from the audio source with a single active SID
209 template <bool fading> void mixSingleSID(isize numSamples);
210
211 // Generates samples from the audio source with multiple active SIDs
212 template <bool fading> void mixMultiSID(isize numSamples);
213
214
215 //
216 // Reading audio samples
217 //
218
219public:
220
221 /* Copies n audio samples into a memory buffer. These functions mark the
222 * final step in the audio pipeline. They are used to copy the generated
223 * sound samples into the buffers of the native sound device. The function
224 * returns the number of copied samples.
225 */
226 isize copyMono(float *buffer, isize n);
227 isize copyStereo(float *left, float *right, isize n);
228 isize copyInterleaved(float *buffer, isize n);
229};
230
231}
VirtualC64 project namespace.
Definition CmdQueue.cpp:16
@ OPT_AUD_VOL_L
Master volume (left channel)
Definition OptionTypes.h:125
@ OPT_AUD_PAN0
Channel 0 panning.
Definition OptionTypes.h:121
@ OPT_AUD_VOL2
Channel 2 volume.
Definition OptionTypes.h:119
@ OPT_AUD_PAN1
Channel 1 panning.
Definition OptionTypes.h:122
@ OPT_AUD_VOL3
Channel 3 volume.
Definition OptionTypes.h:120
@ OPT_AUD_VOL1
Channel 1 volume.
Definition OptionTypes.h:118
@ OPT_AUD_VOL_R
Master volume (right channel)
Definition OptionTypes.h:126
@ OPT_AUD_PAN3
Channel 3 panning.
Definition OptionTypes.h:124
@ OPT_AUD_PAN2
Channel 2 panning.
Definition OptionTypes.h:123
@ OPT_AUD_VOL0
Channel 0 volume.
Definition OptionTypes.h:117