- [NEW] mòdul 'zx_speaker' per a unificar la eixida de só
This commit is contained in:
47
zx_ula.cpp
47
zx_ula.cpp
@@ -68,11 +68,6 @@ namespace zx_ula
|
||||
static uint8_t ear = 0;
|
||||
static uint8_t mic = 0;
|
||||
|
||||
// this variable keeps track of the last "1" written to the sound buffer, so when streaming it in the audio callback, if there
|
||||
// are only zeros remaining in the buffer, we just discard it. It's a poor man's method of keeping the buffer size in control.
|
||||
// This is useless when there are music, sounds, tape loading, etc... as 1's and 0's keep alternating
|
||||
static uint16_t last_1 = 0;
|
||||
|
||||
void update_zx_keyboard()
|
||||
{
|
||||
const uint8_t *keys = SDL_GetKeyboardState(NULL);
|
||||
@@ -231,47 +226,9 @@ namespace zx_ula
|
||||
uint8_t get_ear() { return ear; }
|
||||
void set_ear(const uint8_t val) { ear = val; }
|
||||
|
||||
|
||||
#define SAMPLING_FREQ 44100
|
||||
#define AUDIO_BUFFER_SIZE 2048
|
||||
|
||||
float cycles_per_sample;
|
||||
SDL_AudioDeviceID sdlAudioDevice;
|
||||
uint8_t sound_buffer[AUDIO_BUFFER_SIZE];
|
||||
uint16_t sound_pos=0;
|
||||
uint16_t t_sound=0;
|
||||
|
||||
void sound_init()
|
||||
uint8_t get_sample()
|
||||
{
|
||||
SDL_AudioSpec audioSpec{SAMPLING_FREQ, AUDIO_U8, 1, 0, AUDIO_BUFFER_SIZE>>2, 0, 0, NULL, NULL};
|
||||
sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
|
||||
cycles_per_sample = z80::getClock() / SAMPLING_FREQ;
|
||||
sound_enable();
|
||||
}
|
||||
|
||||
void sound_enable()
|
||||
{
|
||||
SDL_PauseAudioDevice(sdlAudioDevice, 0);
|
||||
}
|
||||
|
||||
void sound_disable()
|
||||
{
|
||||
SDL_PauseAudioDevice(sdlAudioDevice, 1);
|
||||
}
|
||||
|
||||
void sound_update(const uint8_t dt)
|
||||
{
|
||||
t_sound += dt;
|
||||
if (t_sound>=cycles_per_sample) {
|
||||
t_sound-=cycles_per_sample;
|
||||
|
||||
sound_buffer[(sound_pos++)&(AUDIO_BUFFER_SIZE-1)] = ear*128;
|
||||
}
|
||||
if (sound_pos>=1000) {
|
||||
SDL_QueueAudio(sdlAudioDevice, sound_buffer, sound_pos);
|
||||
sound_pos = 0;
|
||||
while (SDL_GetQueuedAudioSize(sdlAudioDevice) > 4096 ) {}
|
||||
}
|
||||
return ear*128;
|
||||
}
|
||||
|
||||
const int getKey(const char key)
|
||||
|
||||
Reference in New Issue
Block a user