-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathDigitalRenderer_audiounit.i
146 lines (116 loc) · 3.57 KB
/
DigitalRenderer_audiounit.i
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
140
141
142
143
144
145
146
/*
Frodo, Commodore 64 emulator for the iPhone
Copyright (C) 2007-2010 Stuart Carnie
See gpl.txt for license information.
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
(at your option) 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/>.
*/
void DigitalRenderer::init_sound() {
sid_filters = ThePrefs.SIDFilters;
_audioQueue = new CAudioUnitQueueManager(this, SAMPLE_FREQ, MonoSound);
_audioQueue->start();
if (!ThePrefs.SIDOn)
Pause();
ready = true;
#if AUDIO_UNIT_MODE == AUDIO_MODE_BIP_BUFFER
_soundBuffer.AllocateBuffer(SOUND_BUFFER_SIZE);
#elif AUDIO_UNIT_MODE == AUDIO_MODE_RING_Q
_soundQBuffer.AllocateBuffers(16, FRAGMENT_SIZE);
#endif
}
DigitalRenderer::~DigitalRenderer() {
if (_audioQueue) {
// default is to auto-delete
_audioQueue->stop();
}
}
#if AUDIO_UNIT_MODE == AUDIO_MODE_BIP_BUFFER
pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
void DigitalRenderer::VBlank() {
pthread_mutex_lock(&g_lock);
int committed = _soundBuffer.GetCommittedSize();
int reserved;
int16* buffer = (int16*)_soundBuffer.Reserve(FRAGMENT_SIZE*4, reserved);
if (buffer) {
calc_buffer(buffer, reserved>>1);
_soundBuffer.Commit(reserved);
}
pthread_mutex_unlock(&g_lock);
}
void DigitalRenderer::fill_buffer(uint8* buffer, uint32* size) {
pthread_mutex_lock(&g_lock);
int sizeInBytes;
uint8* sound = _soundBuffer.GetContiguousBlock(sizeInBytes);
if (*size > sizeInBytes) {
*size = sizeInBytes;
if (!sizeInBytes) {
goto cleanup;
}
}
memcpy(buffer, sound, *size);
_soundBuffer.DecommitBlock(*size);
cleanup:
pthread_mutex_unlock(&g_lock);
}
#elif AUDIO_UNIT_MODE == AUDIO_MODE_RING_Q
void DigitalRenderer::VBlank() {
int16* buffer = _soundQBuffer.DequeueFreeBuffer();
if (buffer) {
calc_buffer(buffer, FRAGMENT_SIZE);
_soundQBuffer.EnqueueSoundBuffer(buffer);
}
int queuedFragments = 16 - _soundQBuffer.FreeCount();
int missing = 8 - queuedFragments;
while (missing-- > 0) {
buffer = _soundQBuffer.DequeueFreeBuffer();
if (buffer) {
calc_buffer(buffer, FRAGMENT_SIZE);
_soundQBuffer.EnqueueSoundBuffer(buffer);
}
}
}
#define SOUND_LOGGING
void DigitalRenderer::fill_buffer(uint8* buffer, uint32* size) {
uint32 filled = 0;
int16 *sound = NULL;
do {
sound = _soundQBuffer.DequeueSoundBuffer();
if (sound) {
memcpy(buffer, sound, FRAGMENT_SIZE_IN_BYTES);
_soundQBuffer.EnqueueFreeBuffer(sound);
filled += FRAGMENT_SIZE_IN_BYTES;
buffer += FRAGMENT_SIZE_IN_BYTES;
}
} while (sound && filled < *size);
#ifdef SOUND_LOGGING
int missing = (*size - filled) >> 1;
if (missing) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog([NSString stringWithFormat:@"Warning: buffer needed %d bytes, supplied with %d bytes", *size, filled]);
[pool release];
}
#endif
*size = filled;
}
#elif AUDIO_UNIT_MODE == AUDIO_MODE_DIRECT
void DigitalRenderer::VBlank() {
}
void DigitalRenderer::fill_buffer(uint8* buffer, uint32* size) {
calc_buffer((int16*)buffer, *size >> 1);
}
#endif
void DigitalRenderer::Pause() {
_audioQueue->pause();
}
void DigitalRenderer::Resume() {
if (ThePrefs.SIDOn)
_audioQueue->resume();
}