Debian Codegraph @

src/engine/platform/sound/tia/Audio.cpp

raw
1 //============================================================================
2 //
3 // SSSS tt lll lll
4 // SS SS tt ll ll
5 // SS tttttt eeee ll ll aaaa
6 // SSSS tt ee ee ll ll aa
7 // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
8 // SS SS tt ee ll ll aa aa
9 // SSSS ttt eeeee llll llll aaaaa
10 //
11 // Copyright (c) 1995-2021 by Bradford W. Mott, Stephen Anthony
12 // and the Stella Team
13 //
14 // See the file "License.txt" for information on usage and redistribution of
15 // this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 //============================================================================
17
18 #define _USE_MATH_DEFINES
19 #include "Audio.h"
20
21 #include <math.h>
22
23 namespace {
24 constexpr double R_MAX = 30.;
25 constexpr double R = 1.;
26
27 short mixingTableEntry(unsigned char v, unsigned char vMax)
28 {
29 return static_cast<short>(
30 floor(0x7fff * double(v) / double(vMax) * (R_MAX + R * double(vMax)) / (R_MAX + R * double(v)))
31 );
32 }
33 }
34
35 namespace TIA {
36
37 static const int phaseCounters[5]={
38 9,
39 28,
40 44,
41 68,
42 79
43 };
44
45 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46 Audio::Audio()
47 {
48 for (unsigned char i = 0; i <= 0x1e; ++i) myMixingTableSum[i] = mixingTableEntry(i, 0x1e);
49 for (unsigned char i = 0; i <= 0x0f; ++i) myMixingTableIndividual[i] = mixingTableEntry(i, 0x0f);
50
51 reset(false);
52 }
53
54 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
55 void Audio::reset(bool st)
56 {
57 myCounter = 9;
58 myPhase = 0;
59 mySampleIndex = 0;
60 stereo = st;
61
62 myCurrentSample[0]=0;
63 myCurrentSample[1]=0;
64
65 myChannelOut[0]=0;
66 myChannelOut[1]=0;
67
68 myChannel0.reset();
69 myChannel1.reset();
70 }
71
72 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73 // steps: (228 total)
74 // 0 - wait
75 // 1 - 9: phase0
76 // 2 - 37: phase1
77 // 3 - 81: phase0
78 // 4 - 149: phase1
79 // 5 - 228: go back to step 0
80 void Audio::tick(int len)
81 {
82 myCounter-=len;
83 while (myCounter<=0) {
84 switch (myPhase) {
85 case 0:
86 case 2:
87 myChannel0.phase0();
88 myChannel1.phase0();
89 break;
90
91 case 1:
92 case 3:
93 phase1();
94 break;
95 }
96 if (++myPhase>4) myPhase=0;
97 myCounter+=phaseCounters[myPhase];
98 }
99 }
100
101 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102 void Audio::write(unsigned char addr, unsigned char val) {
103 switch (addr&0x3f) {
104 case 0x15:
105 myChannel0.audc(val);
106 break;
107 case 0x16:
108 myChannel1.audc(val);
109 break;
110 case 0x17:
111 myChannel0.audf(val);
112 break;
113 case 0x18:
114 myChannel1.audf(val);
115 break;
116 case 0x19:
117 myChannel0.audv(val);
118 break;
119 case 0x1a:
120 myChannel1.audv(val);
121 break;
122 }
123 }
124
125 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
126 void Audio::phase1()
127 {
128 unsigned char sample0 = myChannel0.phase1();
129 unsigned char sample1 = myChannel1.phase1();
130
131 addSample(sample0, sample1);
132 }
133
134 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
135 void Audio::addSample(unsigned char sample0, unsigned char sample1)
136 {
137 if(stereo) {
138 myCurrentSample[0] = myMixingTableIndividual[sample0];
139 myCurrentSample[1] = myMixingTableIndividual[sample1];
140 }
141 else {
142 myCurrentSample[0] = myMixingTableSum[sample0 + sample1];
143 }
144
145 myChannelOut[0] = myMixingTableIndividual[sample0];
146 myChannelOut[1] = myMixingTableIndividual[sample1];
147 }
148
149 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
150 AudioChannel& Audio::channel0()
151 {
152 return myChannel0;
153 }
154
155 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
156 AudioChannel& Audio::channel1()
157 {
158 return myChannel1;
159 }
160
161 }