VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
Volume.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#include "Serializable.h"
14
15#pragma once
16
17namespace vc64 {
18
19/* An object of this class stores a single volume value and provides the means
20 * to emulate a fading effect. Fading is utilized to avoid cracking noises if,
21 * e.g., the emulator is put in pause mode.
22 */
23template <typename T> struct AudioVolume : Serializable {
24
25 // Current volume
26 T current = 1.0;
27
28 // Maximum volume
29 T maximum = 1.0;
30
31 // Fading direction and speed
32 T delta = .0001f;
33
34
35 //
36 // Methods
37 //
38
39 AudioVolume<T>& operator= (const AudioVolume<T>& other) {
40
41 CLONE(current)
42 CLONE(maximum)
43 CLONE(delta)
44
45 return *this;
46 }
47
48 template <class W>
49 void serialize(W& worker)
50 {
51 worker
52
53 << maximum
54 << delta;
55
56 if (isChecker(worker)) return;
57
58 worker
59
60 << current;
61
62 } SERIALIZERS(serialize);
63
64 // Checks whether the volume is currently modulated
65 bool isFadingIn() const { return delta > 0 && current != maximum; }
66 bool isFadingOut() const { return delta < 0 && current != 0; }
67 bool isFading() const { return isFadingIn() || isFadingOut(); }
68
69 // Sets the volume to a fixed value
70 void set(float value) { current = value; delta = 0.0; }
71
72 // Gradually decrease the volume to zero
73 void mute(isize steps = 10000) {
74
75 if (steps == 0) {
76 current = delta = 0;
77 } else {
78 delta = -maximum / steps;
79 }
80 }
81
82 // Gradually increase the volume to max
83 void unmute(isize steps = 10000) {
84
85 if (steps == 0) {
86 current = maximum; delta = 0;
87 } else {
88 delta = maximum / steps;
89 }
90 }
91
92 // Shifts the current volume towards the target volume
93 void shift() {
94
95 if (delta < 0 && current != 0) {
96
97 if ((current += delta) > 0) return;
98 current = 0;
99 }
100 if (delta > 0 && current != maximum) {
101
102 if ((current += delta) < maximum) return;
103 current = maximum;
104 }
105 }
106};
107
108typedef AudioVolume<float> Volume;
109
110}
VirtualC64 project namespace.
Definition CmdQueue.cpp:16