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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/*
* Gearboy - Nintendo Game Boy Emulator
* Copyright (C) 2012 Ignacio Sanchez
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
*
*/
#include "Audio.h"
#include "Memory.h"
Audio::Audio()
{
m_bCGB = false;
m_ElapsedCycles = 0;
m_SampleRate = 44100;
InitPointer(m_pApu);
InitPointer(m_pBuffer);
InitPointer(m_pSampleBuffer);
}
Audio::~Audio()
{
SafeDelete(m_pApu);
SafeDelete(m_pBuffer);
SafeDeleteArray(m_pSampleBuffer);
}
void Audio::Init()
{
m_pSampleBuffer = new blip_sample_t[AUDIO_BUFFER_SIZE];
m_pApu = new Gb_Apu();
m_pBuffer = new Stereo_Buffer();
m_pBuffer->clock_rate(4194304);
m_pBuffer->set_sample_rate(m_SampleRate);
//m_pApu->treble_eq(-15.0);
//m_pBuffer->bass_freq(100);
m_pApu->set_output(m_pBuffer->center(), m_pBuffer->left(), m_pBuffer->right());
}
void Audio::Reset(bool bCGB)
{
m_bCGB = bCGB;
Gb_Apu::mode_t mode = m_bCGB ? Gb_Apu::mode_cgb : Gb_Apu::mode_dmg;
m_pApu->reset(mode);
m_pBuffer->clear();
for (int reg = 0xFF10; reg <= 0xFF3F; reg++)
{
u8 value = m_bCGB ? kInitialValuesForColorFFXX[reg - 0xFF00] : kInitialValuesForFFXX[reg - 0xFF00];
m_pApu->write_register(0, reg, value);
}
m_ElapsedCycles = 0;
}
void Audio::SetSampleRate(int rate)
{
if (rate != m_SampleRate)
{
m_SampleRate = rate;
m_pBuffer->set_sample_rate(m_SampleRate);
}
}
void Audio::SetVolume(float volume)
{
m_pApu->volume(volume);
}
void Audio::EndFrame(s16* pSampleBuffer, int* pSampleCount)
{
m_pApu->end_frame(m_ElapsedCycles);
m_pBuffer->end_frame(m_ElapsedCycles);
int count = static_cast<int>(m_pBuffer->read_samples(m_pSampleBuffer, AUDIO_BUFFER_SIZE));
if (IsValidPointer(pSampleBuffer) && IsValidPointer(pSampleCount))
{
*pSampleCount = count;
for (int i=0; i<count; i++)
{
pSampleBuffer[i] = m_pSampleBuffer[i];
}
}
m_ElapsedCycles = 0;
}
void Audio::SaveState(std::ostream& stream)
{
using namespace std;
gb_apu_state_t apu_state;
m_pApu->save_state(&apu_state);
stream.write(reinterpret_cast<const char*> (&m_ElapsedCycles), sizeof(m_ElapsedCycles));
stream.write(reinterpret_cast<const char*> (m_pSampleBuffer), sizeof(blip_sample_t) * AUDIO_BUFFER_SIZE);
stream.write(reinterpret_cast<const char*> (&apu_state), sizeof(apu_state));
}
void Audio::LoadState(std::istream& stream)
{
using namespace std;
gb_apu_state_t apu_state;
stream.read(reinterpret_cast<char*> (&m_ElapsedCycles), sizeof(m_ElapsedCycles));
stream.read(reinterpret_cast<char*> (m_pSampleBuffer), sizeof(blip_sample_t) * AUDIO_BUFFER_SIZE);
stream.read(reinterpret_cast<char*> (&apu_state), sizeof(apu_state));
Gb_Apu::mode_t mode = m_bCGB ? Gb_Apu::mode_cgb : Gb_Apu::mode_dmg;
m_pApu->reset(mode);
m_pApu->load_state(apu_state);
m_pBuffer->clear();
}
Gb_Apu* Audio::GetApu()
{
return m_pApu;
}
|