2020-05-10 06:40:37 +00:00
|
|
|
//
|
|
|
|
// speaker.c
|
2020-07-13 17:23:33 +00:00
|
|
|
// Steve ][
|
2020-05-10 06:40:37 +00:00
|
|
|
//
|
|
|
|
// Created by Tamas Rudnai on 5/9/20.
|
2020-07-13 17:16:37 +00:00
|
|
|
// Copyright © 2019, 2020 Tamas Rudnai. All rights reserved.
|
2020-07-13 17:10:33 +00:00
|
|
|
//
|
|
|
|
// This file is part of Steve ][ -- The Apple ][ Emulator.
|
|
|
|
//
|
|
|
|
// Steve ][ 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.
|
|
|
|
//
|
|
|
|
// Steve ][ 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 Steve ][. If not, see <https://www.gnu.org/licenses/>.
|
2020-05-10 06:40:37 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "speaker.h"
|
2020-05-17 14:49:05 +00:00
|
|
|
#include "6502.h"
|
2020-05-28 00:27:11 +00:00
|
|
|
#include "disk.h" // to be able to disable disk acceleration
|
2020-05-17 14:49:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define min(x,y) (x) < (y) ? (x) : (y)
|
|
|
|
#define max(x,y) (x) > (y) ? (x) : (y)
|
|
|
|
#define clamp(min,num,max) (num) < (min) ? (min) : (num) > (max) ? (max) : (num)
|
2020-05-10 06:40:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define CASE_RETURN(err) case (err): return #err
|
|
|
|
const char* al_err_str(ALenum err) {
|
|
|
|
switch(err) {
|
|
|
|
CASE_RETURN(AL_NO_ERROR);
|
|
|
|
CASE_RETURN(AL_INVALID_NAME);
|
|
|
|
CASE_RETURN(AL_INVALID_ENUM);
|
|
|
|
CASE_RETURN(AL_INVALID_VALUE);
|
|
|
|
CASE_RETURN(AL_INVALID_OPERATION);
|
|
|
|
CASE_RETURN(AL_OUT_OF_MEMORY);
|
|
|
|
}
|
2020-05-17 14:49:05 +00:00
|
|
|
printf("alError: 0x%04X\n", err);
|
2020-05-10 06:40:37 +00:00
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
#undef CASE_RETURN
|
|
|
|
|
|
|
|
#define __al_check_error(file,line) \
|
|
|
|
do { \
|
|
|
|
ALenum err = alGetError(); \
|
|
|
|
for(; err != AL_NO_ERROR; err = alGetError()) { \
|
|
|
|
printf( "AL Error %s at %s:%d\n", al_err_str(err), file, line ); \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define al_check_error() \
|
|
|
|
__al_check_error(__FILE__, __LINE__)
|
|
|
|
|
|
|
|
|
|
|
|
ALCdevice *dev = NULL;
|
|
|
|
ALCcontext *ctx = NULL;
|
|
|
|
|
2020-05-18 02:32:45 +00:00
|
|
|
|
2020-07-02 06:08:33 +00:00
|
|
|
#define SPKR_MIN_VOL 0.0001 // OpenAL cannot change volume to 0.0 for some reason, so we just turn volume really low
|
2020-05-18 02:32:45 +00:00
|
|
|
int spkr_level = SPKR_LEVEL_ZERO;
|
|
|
|
|
|
|
|
|
2020-06-23 02:23:41 +00:00
|
|
|
#define BUFFER_COUNT 256
|
2020-06-12 06:37:18 +00:00
|
|
|
#define SOURCES_COUNT 4
|
2020-06-27 23:54:02 +00:00
|
|
|
#define SPKR_CHANNELS 2
|
2020-06-12 06:37:18 +00:00
|
|
|
|
|
|
|
ALuint spkr_src [SOURCES_COUNT] = { 0, 0, 0, 0 };
|
2020-05-18 02:32:45 +00:00
|
|
|
|
|
|
|
ALuint spkr_buffers[BUFFER_COUNT];
|
2020-06-12 01:29:26 +00:00
|
|
|
ALuint spkr_disk_motor_buf = 0;
|
|
|
|
ALuint spkr_disk_arm_buf = 0;
|
|
|
|
ALuint spkr_disk_ioerr_buf = 0;
|
2020-05-10 17:54:34 +00:00
|
|
|
|
|
|
|
|
2020-07-02 06:08:33 +00:00
|
|
|
float spkr_vol = 0.5;
|
|
|
|
|
|
|
|
|
2020-06-24 05:59:20 +00:00
|
|
|
const unsigned spkr_fps = DEFAULT_FPS;
|
|
|
|
unsigned spkr_fps_divider = 1;
|
|
|
|
unsigned spkr_frame_cntr = 0;
|
|
|
|
unsigned spkr_clk = 0;
|
|
|
|
|
2020-06-23 02:23:41 +00:00
|
|
|
const unsigned spkr_seconds = 1;
|
2020-05-10 06:40:37 +00:00
|
|
|
const unsigned spkr_sample_rate = 44100;
|
2020-06-12 05:08:48 +00:00
|
|
|
const unsigned sfx_sample_rate = 22050; // original sample rate
|
|
|
|
//const unsigned sfx_sample_rate = 26000; // bit higher pitch
|
2020-06-14 03:13:20 +00:00
|
|
|
int spkr_extra_buf = 0; // 800 / spkr_fps;
|
2020-07-04 17:13:12 +00:00
|
|
|
typedef int16_t spkr_sample_t;
|
|
|
|
const unsigned spkr_buf_alloc_size = spkr_seconds * spkr_sample_rate * SPKR_CHANNELS * sizeof(spkr_sample_t) / DEFAULT_FPS; // stereo
|
|
|
|
unsigned spkr_buf_size = spkr_buf_alloc_size / sizeof(spkr_sample_t);
|
|
|
|
spkr_sample_t spkr_samples [ spkr_buf_alloc_size * DEFAULT_FPS * BUFFER_COUNT]; // can store up to 1 sec of sound
|
2020-05-10 22:30:29 +00:00
|
|
|
unsigned spkr_sample_idx = 0;
|
2020-05-10 06:40:37 +00:00
|
|
|
|
2020-06-23 02:23:41 +00:00
|
|
|
unsigned spkr_play_timeout = 8; // increase to 32 for 240 fps, normally 8 for 30 fps
|
2020-05-17 23:01:15 +00:00
|
|
|
unsigned spkr_play_time = 0;
|
2020-06-12 01:29:26 +00:00
|
|
|
unsigned spkr_play_disk_motor_time = 0;
|
|
|
|
unsigned spkr_play_disk_arm_time = 0;
|
|
|
|
unsigned spkr_play_disk_ioerr_time = 0;
|
2020-05-17 23:01:15 +00:00
|
|
|
|
2020-06-12 01:29:26 +00:00
|
|
|
uint8_t * diskmotor_sfx = NULL;
|
|
|
|
int diskmotor_sfx_len = 0;
|
|
|
|
uint8_t * diskarm_sfx = NULL;
|
|
|
|
int diskarm_sfx_len = 0;
|
|
|
|
uint8_t * diskioerr_sfx = NULL;
|
|
|
|
int diskioerr_sfx_len = 0;
|
2020-05-17 14:49:05 +00:00
|
|
|
|
2020-06-27 23:54:02 +00:00
|
|
|
ALint freeBuffers = BUFFER_COUNT;
|
|
|
|
|
2020-06-12 01:29:26 +00:00
|
|
|
|
|
|
|
static int load_sfx( const char * bundlePath, const char * filename, uint8_t ** buf ) {
|
2020-06-09 07:54:36 +00:00
|
|
|
char fullPath[256];
|
|
|
|
|
|
|
|
strcpy( fullPath, bundlePath );
|
|
|
|
strcat( fullPath, "/");
|
|
|
|
strcat( fullPath, filename );
|
|
|
|
|
|
|
|
FILE * f = fopen(fullPath, "rb");
|
|
|
|
if (f == NULL) {
|
|
|
|
perror("Failed to read SFX: ");
|
2020-06-12 01:29:26 +00:00
|
|
|
return -1;
|
2020-06-09 07:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fseek(f, 0L, SEEK_END);
|
|
|
|
uint16_t flen = ftell(f);
|
|
|
|
fseek(f, 0L, SEEK_SET);
|
|
|
|
|
|
|
|
if (flen <= 0) {
|
|
|
|
printf("Failed to read SFX or 0 size\n");
|
2020-06-12 01:29:26 +00:00
|
|
|
return -1;
|
2020-06-09 07:54:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 01:29:26 +00:00
|
|
|
*buf = malloc(flen);
|
2020-06-09 07:54:36 +00:00
|
|
|
|
2020-06-12 01:29:26 +00:00
|
|
|
if ( *buf == NULL ) {
|
2020-06-09 07:54:36 +00:00
|
|
|
printf("Not enough memory for SFX\n");
|
2020-06-12 01:29:26 +00:00
|
|
|
return -1;
|
2020-06-09 07:54:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 01:29:26 +00:00
|
|
|
fread( *buf, 1, flen, f);
|
2020-06-09 07:54:36 +00:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if ( flen == 0 ) {
|
|
|
|
printf("Error loading SFX file\n");
|
2020-06-12 01:29:26 +00:00
|
|
|
free( *buf );
|
|
|
|
return -1; // there was an error
|
2020-06-09 07:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// everything seems to be ok
|
2020-06-12 01:29:26 +00:00
|
|
|
return flen;
|
2020-06-09 07:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void spkr_load_sfx( const char * bundlePath ) {
|
2020-06-27 06:24:01 +00:00
|
|
|
diskmotor_sfx_len = load_sfx(bundlePath, "disk_ii_motor_w_floppy.sfx", &diskmotor_sfx);
|
|
|
|
diskarm_sfx_len = load_sfx(bundlePath, "disk_ii_arm.sfx", &diskarm_sfx);
|
|
|
|
diskioerr_sfx_len = load_sfx(bundlePath, "disk_ii_io_error.sfx", &diskioerr_sfx);
|
2020-06-09 07:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-10 22:30:29 +00:00
|
|
|
// initialize OpenAL
|
2020-05-10 06:40:37 +00:00
|
|
|
void spkr_init() {
|
|
|
|
const char *defname = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
|
|
|
|
printf( "Default device: %s\n", defname );
|
2020-05-18 02:32:45 +00:00
|
|
|
|
|
|
|
// restart OpenAL when restarting the virtual machine
|
|
|
|
spkr_exit();
|
2020-05-10 06:40:37 +00:00
|
|
|
|
|
|
|
dev = alcOpenDevice(defname);
|
|
|
|
ctx = alcCreateContext(dev, NULL);
|
|
|
|
alcMakeContextCurrent(ctx);
|
|
|
|
|
|
|
|
// Fill buffer with zeros
|
2020-07-04 17:13:12 +00:00
|
|
|
memset( spkr_samples, spkr_level, spkr_buf_alloc_size + spkr_extra_buf);
|
2020-05-10 06:40:37 +00:00
|
|
|
|
2020-05-17 14:49:05 +00:00
|
|
|
// Create buffer to store samples
|
|
|
|
alGenBuffers(BUFFER_COUNT, spkr_buffers);
|
2020-06-12 01:29:26 +00:00
|
|
|
alGenBuffers(1, &spkr_disk_motor_buf);
|
|
|
|
alGenBuffers(1, &spkr_disk_arm_buf);
|
2020-05-17 14:49:05 +00:00
|
|
|
al_check_error();
|
|
|
|
|
|
|
|
// Set-up sound source and play buffer
|
2020-06-12 06:37:18 +00:00
|
|
|
alGenSources(SOURCES_COUNT, spkr_src);
|
2020-06-12 01:29:26 +00:00
|
|
|
al_check_error();
|
2020-06-25 04:05:55 +00:00
|
|
|
|
2020-07-02 06:08:33 +00:00
|
|
|
alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
|
|
|
|
al_check_error();
|
|
|
|
alListenerf(AL_GAIN, spkr_vol);
|
|
|
|
al_check_error();
|
|
|
|
// alListener3f(AL_ORIENTATION, 0.0, -16.0, 0.0);
|
|
|
|
// al_check_error();
|
|
|
|
|
2020-06-25 04:05:55 +00:00
|
|
|
alSourcei(spkr_src[SPKR_SRC_GAME_SFX], AL_SOURCE_RELATIVE, AL_TRUE);
|
|
|
|
al_check_error();
|
2020-06-12 06:37:18 +00:00
|
|
|
alSourcei(spkr_src[SPKR_SRC_GAME_SFX], AL_LOOPING, AL_FALSE);
|
2020-05-17 14:49:05 +00:00
|
|
|
al_check_error();
|
2020-06-12 06:37:18 +00:00
|
|
|
alSourcef(spkr_src[SPKR_SRC_GAME_SFX], AL_ROLLOFF_FACTOR, 0);
|
2020-06-12 01:29:26 +00:00
|
|
|
al_check_error();
|
2020-06-12 06:37:18 +00:00
|
|
|
alSource3f(spkr_src[SPKR_SRC_GAME_SFX], AL_POSITION, 0.0, 8.0, 0.0);
|
2020-06-12 01:29:26 +00:00
|
|
|
al_check_error();
|
|
|
|
|
|
|
|
|
|
|
|
// Set-up disk motor sound source and play buffer
|
2020-06-25 04:05:55 +00:00
|
|
|
alSourcei(spkr_src[SPKR_SRC_DISK_MOTOR_SFX], AL_SOURCE_RELATIVE, AL_TRUE);
|
|
|
|
al_check_error();
|
2020-06-12 06:37:18 +00:00
|
|
|
alSourcei(spkr_src[SPKR_SRC_DISK_MOTOR_SFX], AL_LOOPING, AL_TRUE);
|
2020-05-17 14:49:05 +00:00
|
|
|
al_check_error();
|
2020-06-12 06:37:18 +00:00
|
|
|
alSourcef(spkr_src[SPKR_SRC_DISK_MOTOR_SFX], AL_ROLLOFF_FACTOR, 0);
|
2020-05-17 20:48:35 +00:00
|
|
|
al_check_error();
|
2020-06-12 06:37:18 +00:00
|
|
|
alSource3f(spkr_src[SPKR_SRC_DISK_MOTOR_SFX], AL_POSITION, 0.0, 8.0, 0.0);
|
2020-05-17 20:48:35 +00:00
|
|
|
al_check_error();
|
|
|
|
|
2020-06-12 01:29:26 +00:00
|
|
|
|
|
|
|
// Set-up disk arm sound source and play buffer
|
2020-06-25 04:05:55 +00:00
|
|
|
alSourcei(spkr_src[SPKR_SRC_DISK_ARM_SFX], AL_SOURCE_RELATIVE, AL_TRUE);
|
|
|
|
al_check_error();
|
2020-06-12 06:37:18 +00:00
|
|
|
alSourcei(spkr_src[SPKR_SRC_DISK_ARM_SFX], AL_LOOPING, AL_FALSE);
|
2020-06-12 01:29:26 +00:00
|
|
|
al_check_error();
|
2020-06-12 06:37:18 +00:00
|
|
|
alSourcef(spkr_src[SPKR_SRC_DISK_ARM_SFX], AL_ROLLOFF_FACTOR, 0);
|
2020-06-12 01:29:26 +00:00
|
|
|
al_check_error();
|
2020-06-12 06:37:18 +00:00
|
|
|
alSource3f(spkr_src[SPKR_SRC_DISK_ARM_SFX], AL_POSITION, 0.0, 8.0, 0.0);
|
2020-06-12 01:29:26 +00:00
|
|
|
al_check_error();
|
|
|
|
|
|
|
|
|
|
|
|
// Set-up disk io error sound source and play buffer
|
2020-06-25 04:05:55 +00:00
|
|
|
alSourcei(spkr_src[SPKR_SRC_DISK_IOERR_SFX], AL_SOURCE_RELATIVE, AL_TRUE);
|
|
|
|
al_check_error();
|
2020-06-12 06:37:18 +00:00
|
|
|
alSourcei(spkr_src[SPKR_SRC_DISK_IOERR_SFX], AL_LOOPING, AL_FALSE);
|
2020-06-12 01:29:26 +00:00
|
|
|
al_check_error();
|
2020-06-12 06:37:18 +00:00
|
|
|
alSourcef(spkr_src[SPKR_SRC_DISK_IOERR_SFX], AL_ROLLOFF_FACTOR, 0);
|
2020-06-12 01:29:26 +00:00
|
|
|
al_check_error();
|
2020-06-12 06:37:18 +00:00
|
|
|
alSource3f(spkr_src[SPKR_SRC_DISK_IOERR_SFX], AL_POSITION, 0.0, 8.0, 0.0);
|
2020-06-12 01:29:26 +00:00
|
|
|
al_check_error();
|
|
|
|
|
|
|
|
|
2020-05-17 14:49:05 +00:00
|
|
|
// start from the beginning
|
|
|
|
spkr_sample_idx = 0;
|
|
|
|
|
2020-05-18 01:31:41 +00:00
|
|
|
// make sure we have free buffers initialized here
|
|
|
|
freeBuffers = BUFFER_COUNT;
|
2020-05-10 06:40:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-27 23:54:02 +00:00
|
|
|
|
2020-07-02 06:08:33 +00:00
|
|
|
void spkr_vol_up() {
|
|
|
|
spkr_vol += 0.1;
|
|
|
|
if ( spkr_vol > 1 ) {
|
|
|
|
spkr_vol = 1;
|
|
|
|
}
|
|
|
|
alListenerf(AL_GAIN, spkr_vol);
|
|
|
|
al_check_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
void spkr_vol_dn() {
|
|
|
|
spkr_vol -= 0.1;
|
|
|
|
if ( spkr_vol < 0.1 ) {
|
|
|
|
// use mute to make it completely silent
|
|
|
|
spkr_vol = 0.1;
|
|
|
|
}
|
|
|
|
alListenerf(AL_GAIN, spkr_vol);
|
|
|
|
al_check_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
void spkr_mute() {
|
|
|
|
ALfloat vol = 0;
|
|
|
|
alGetListenerf(AL_GAIN, &vol);
|
|
|
|
al_check_error();
|
|
|
|
|
|
|
|
if ( vol > SPKR_MIN_VOL ) {
|
|
|
|
alListenerf(AL_GAIN, SPKR_MIN_VOL);
|
|
|
|
al_check_error();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alListenerf(AL_GAIN, spkr_vol);
|
|
|
|
al_check_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-27 23:54:02 +00:00
|
|
|
int spkr_unqueue( ALuint src ) {
|
|
|
|
ALint processed = 0;
|
|
|
|
|
2020-06-28 03:29:53 +00:00
|
|
|
if ( src ) {
|
|
|
|
alGetSourcei ( src, AL_BUFFERS_PROCESSED, &processed );
|
2020-06-27 23:54:02 +00:00
|
|
|
al_check_error();
|
2020-06-28 03:29:53 +00:00
|
|
|
// printf("%s alGetSourcei(%d)\n", __FUNCTION__, src);
|
2020-07-04 17:13:12 +00:00
|
|
|
// printf("p:%d\n", processed);
|
2020-06-28 03:29:53 +00:00
|
|
|
|
2020-07-04 17:13:12 +00:00
|
|
|
if ( processed > 0 ) {
|
2020-06-28 03:29:53 +00:00
|
|
|
alSourceUnqueueBuffers( src, processed, &spkr_buffers[freeBuffers]);
|
|
|
|
al_check_error();
|
2020-07-04 17:13:12 +00:00
|
|
|
freeBuffers += processed;
|
|
|
|
freeBuffers = clamp( 1, freeBuffers, BUFFER_COUNT );
|
2020-06-28 03:29:53 +00:00
|
|
|
}
|
2020-06-27 23:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return processed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void spkr_unqueueAll() {
|
|
|
|
for ( int i = 0; i < SOURCES_COUNT; i++ ) {
|
|
|
|
spkr_unqueue( spkr_src[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-10 22:30:29 +00:00
|
|
|
// Dealloc OpenAL
|
2020-05-10 06:40:37 +00:00
|
|
|
void spkr_exit() {
|
2020-06-12 06:37:18 +00:00
|
|
|
if ( spkr_src[SPKR_SRC_GAME_SFX] ) {
|
2020-06-27 23:54:02 +00:00
|
|
|
spkr_stopAll();
|
|
|
|
spkr_unqueueAll();
|
2020-06-12 06:16:26 +00:00
|
|
|
|
2020-06-27 23:54:02 +00:00
|
|
|
// delete buffers
|
|
|
|
alDeleteBuffers(BUFFER_COUNT, spkr_buffers);
|
|
|
|
al_check_error();
|
|
|
|
alDeleteBuffers(1, &spkr_disk_motor_buf);
|
|
|
|
al_check_error();
|
|
|
|
alDeleteBuffers(1, &spkr_disk_arm_buf);
|
|
|
|
al_check_error();
|
|
|
|
|
|
|
|
// delete sound source and play buffer
|
|
|
|
alDeleteSources(SOURCES_COUNT, spkr_src);
|
|
|
|
al_check_error();
|
|
|
|
|
2020-07-04 17:13:12 +00:00
|
|
|
ALCdevice *dev = alcGetContextsDevice(ctx);
|
|
|
|
ALCcontext *ctx = alcGetCurrentContext();
|
2020-05-18 02:32:45 +00:00
|
|
|
|
|
|
|
alcMakeContextCurrent(NULL);
|
2020-07-04 17:13:12 +00:00
|
|
|
al_check_error();
|
2020-05-18 02:32:45 +00:00
|
|
|
alcDestroyContext(ctx);
|
2020-07-04 17:13:12 +00:00
|
|
|
al_check_error();
|
2020-05-18 02:32:45 +00:00
|
|
|
alcCloseDevice(dev);
|
|
|
|
al_check_error();
|
|
|
|
|
2020-06-12 05:38:03 +00:00
|
|
|
memset(spkr_src, 0, sizeof(spkr_src));
|
|
|
|
memset(spkr_buffers, 0, sizeof(spkr_buffers));
|
2020-06-12 06:16:26 +00:00
|
|
|
spkr_disk_motor_buf = 0;
|
|
|
|
spkr_disk_arm_buf = 0;
|
2020-05-18 02:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-24 20:07:34 +00:00
|
|
|
char spkr_state = 0;
|
|
|
|
|
2020-05-18 02:32:45 +00:00
|
|
|
void spkr_toggle() {
|
|
|
|
// TODO: This is very slow!
|
2020-05-10 06:40:37 +00:00
|
|
|
|
2020-06-23 02:23:41 +00:00
|
|
|
// spkr_play_time = 0;
|
2020-05-10 06:40:37 +00:00
|
|
|
|
2020-05-28 00:27:11 +00:00
|
|
|
if ( diskAccelerator_count ) {
|
|
|
|
// turn off disk acceleration immediately
|
|
|
|
diskAccelerator_count = 0;
|
|
|
|
clk_6502_per_frm = clk_6502_per_frm_max = clk_6502_per_frm_set;
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:16:49 +00:00
|
|
|
if ( clk_6502_per_frm_set < clk_6502_per_frm_max_sound ) {
|
|
|
|
|
|
|
|
spkr_play_time = spkr_play_timeout;
|
2020-05-24 20:07:34 +00:00
|
|
|
|
2020-05-28 00:16:49 +00:00
|
|
|
// push a click into the speaker buffer
|
|
|
|
// (we will play the entire buffer at the end of the frame)
|
2020-06-27 23:54:02 +00:00
|
|
|
spkr_sample_idx = ( (spkr_clk + clkfrm) / ( MHZ(default_MHz_6502) / spkr_sample_rate)) * SPKR_CHANNELS;
|
2020-05-24 20:07:34 +00:00
|
|
|
|
2020-06-28 03:29:53 +00:00
|
|
|
spkr_level = spkr_samples[ spkr_sample_idx ];
|
|
|
|
|
2020-05-28 00:16:49 +00:00
|
|
|
if ( spkr_state ) {
|
|
|
|
// down edge
|
|
|
|
spkr_state = 0;
|
2020-05-24 20:07:34 +00:00
|
|
|
|
2020-06-28 03:29:53 +00:00
|
|
|
float dumping = spkr_level - SPKR_LEVEL_MIN;
|
|
|
|
dumping *= SPKR_INITIAL_DUMPING;
|
|
|
|
|
|
|
|
while ( dumping > 1 ) {
|
|
|
|
spkr_samples[ spkr_sample_idx++ ] = SPKR_LEVEL_MIN + dumping;
|
|
|
|
spkr_samples[ spkr_sample_idx++ ] = SPKR_LEVEL_MIN + dumping; // stereo
|
2020-05-28 00:16:49 +00:00
|
|
|
|
|
|
|
// how smooth we want the speeker to decay, so we will hear no pops and crackles
|
|
|
|
// 0.9 gives you a kind of saw wave at 1KHz (beep)
|
|
|
|
// 0.7 is better, but Xonix gives you a bit distorted speech and Donkey Kong does not sound the same
|
2020-06-28 03:29:53 +00:00
|
|
|
dumping *= SPKR_FADE_TRAILING_EDGE;
|
2020-05-28 00:16:49 +00:00
|
|
|
}
|
|
|
|
spkr_level = SPKR_LEVEL_MIN;
|
2020-05-18 02:32:45 +00:00
|
|
|
}
|
2020-05-28 00:16:49 +00:00
|
|
|
else {
|
|
|
|
// up edge
|
|
|
|
spkr_state = 1;
|
|
|
|
|
2020-06-28 03:29:53 +00:00
|
|
|
float dumping = spkr_level - SPKR_LEVEL_MAX;
|
|
|
|
dumping *= SPKR_INITIAL_DUMPING;
|
|
|
|
|
|
|
|
while ( dumping < -1 ) {
|
|
|
|
spkr_samples[ spkr_sample_idx++ ] = SPKR_LEVEL_MAX + dumping;
|
|
|
|
spkr_samples[ spkr_sample_idx++ ] = SPKR_LEVEL_MAX + dumping; // stereo
|
2020-05-28 00:16:49 +00:00
|
|
|
|
|
|
|
// how smooth we want the speeker to decay, so we will hear no pops and crackles
|
|
|
|
// 0.9 gives you a kind of saw wave at 1KHz (beep)
|
|
|
|
// 0.7 is better, but Xonix gives you a bit distorted speech and Donkey Kong does not sound the same
|
2020-06-28 03:29:53 +00:00
|
|
|
dumping *= SPKR_FADE_LEADING_EDGE;
|
2020-05-28 00:16:49 +00:00
|
|
|
}
|
|
|
|
spkr_level = SPKR_LEVEL_MAX;
|
2020-05-18 02:32:45 +00:00
|
|
|
}
|
2020-05-24 20:07:34 +00:00
|
|
|
|
2020-05-28 00:16:49 +00:00
|
|
|
|
|
|
|
//spkr_samples[sample_idx] = spkr_level;
|
|
|
|
for ( int i = spkr_sample_idx; i < spkr_buf_size + spkr_extra_buf; i++ ) {
|
|
|
|
spkr_samples[i] = spkr_level;
|
|
|
|
}
|
2020-07-04 17:13:12 +00:00
|
|
|
// memset(spkr_samples + spkr_sample_idx, spkr_level, spkr_buf_size * sizeof(spkr_sample_t));
|
2020-05-28 00:16:49 +00:00
|
|
|
|
2020-05-24 19:14:09 +00:00
|
|
|
}
|
2020-05-10 06:40:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:49:05 +00:00
|
|
|
|
2020-06-27 23:54:02 +00:00
|
|
|
#define SPKR_PLAY_DELAY 2
|
|
|
|
int playDelay = SPKR_PLAY_DELAY;
|
2020-06-23 02:23:41 +00:00
|
|
|
|
2020-05-17 14:49:05 +00:00
|
|
|
|
2020-06-24 05:59:20 +00:00
|
|
|
void spkr_update() {
|
|
|
|
if ( ++spkr_frame_cntr >= spkr_fps_divider ) {
|
|
|
|
spkr_frame_cntr = 0;
|
2020-05-10 22:30:29 +00:00
|
|
|
|
2020-07-04 17:13:12 +00:00
|
|
|
// Fix: Unqueue was not working properly some cases, so we need to monitor
|
|
|
|
// queued elements and if there are too many, let's just wait
|
|
|
|
#define SPKR_MAX_QUEUED 10
|
|
|
|
ALint queued = 0;
|
|
|
|
alGetSourcei ( spkr_src[SPKR_SRC_GAME_SFX], AL_BUFFERS_QUEUED, &queued );
|
|
|
|
al_check_error();
|
|
|
|
// printf("q:%d clkfrm:%d frm:%llu max:%llu\n", queued, clkfrm, clk_6502_per_frm, clk_6502_per_frm_max);
|
|
|
|
|
|
|
|
if ( ( spkr_play_time ) && ( queued < SPKR_MAX_QUEUED ) ) {
|
2020-06-24 05:59:20 +00:00
|
|
|
if ( freeBuffers ) {
|
2020-06-25 04:05:55 +00:00
|
|
|
// in Game Mode do not fade out and stop playing
|
|
|
|
if ( ( cpuMode_game != cpuMode ) && ( --spkr_play_time == 0 ) ) {
|
2020-06-24 05:59:20 +00:00
|
|
|
float fadeLevel = spkr_level - SPKR_LEVEL_ZERO;
|
|
|
|
|
|
|
|
if ( spkr_level != SPKR_LEVEL_ZERO ) {
|
|
|
|
spkr_sample_idx = 0;
|
|
|
|
|
|
|
|
while ( ( fadeLevel < -1 ) || ( fadeLevel > 1 ) ) {
|
|
|
|
spkr_samples[ spkr_sample_idx++ ] = SPKR_LEVEL_ZERO + fadeLevel;
|
2020-06-27 23:54:02 +00:00
|
|
|
spkr_samples[ spkr_sample_idx++ ] = SPKR_LEVEL_ZERO + fadeLevel; // stereo
|
2020-06-24 05:59:20 +00:00
|
|
|
|
|
|
|
// how smooth we want the speeker to decay, so we will hear no pops and crackles
|
|
|
|
fadeLevel *= 0.999;
|
|
|
|
}
|
|
|
|
spkr_level = SPKR_LEVEL_ZERO;
|
|
|
|
|
|
|
|
//spkr_samples[sample_idx] = spkr_level;
|
2020-07-04 17:13:12 +00:00
|
|
|
memset(spkr_samples + spkr_sample_idx, SPKR_LEVEL_ZERO, spkr_extra_buf * sizeof(spkr_sample_t));
|
2020-06-24 05:59:20 +00:00
|
|
|
|
|
|
|
freeBuffers--;
|
2020-07-04 17:13:12 +00:00
|
|
|
alBufferData(spkr_buffers[freeBuffers], AL_FORMAT_STEREO16, spkr_samples, spkr_sample_idx * sizeof(spkr_sample_t), spkr_sample_rate);
|
2020-06-24 05:59:20 +00:00
|
|
|
al_check_error();
|
|
|
|
alSourceQueueBuffers(spkr_src[SPKR_SRC_GAME_SFX], 1, &spkr_buffers[freeBuffers]);
|
|
|
|
al_check_error();
|
2020-06-12 04:53:53 +00:00
|
|
|
}
|
2020-06-24 05:59:20 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-07-04 17:13:12 +00:00
|
|
|
// push a click into the speaker buffer
|
|
|
|
// (we will play the entire buffer at the end of the frame)
|
|
|
|
spkr_sample_idx = ( (spkr_clk + clkfrm) / ( MHZ(default_MHz_6502) / spkr_sample_rate)) * SPKR_CHANNELS;
|
|
|
|
|
2020-06-12 04:53:53 +00:00
|
|
|
freeBuffers--;
|
2020-06-24 05:59:20 +00:00
|
|
|
alBufferData(spkr_buffers[freeBuffers], AL_FORMAT_STEREO16, spkr_samples, (spkr_buf_size + spkr_extra_buf) * sizeof(spkr_samples[0]), spkr_sample_rate);
|
2020-07-04 17:13:12 +00:00
|
|
|
// alBufferData(spkr_buffers[freeBuffers], AL_FORMAT_STEREO16, spkr_samples, (spkr_sample_idx + spkr_extra_buf) * sizeof(spkr_sample_t), spkr_sample_rate);
|
|
|
|
// ALint bufSize = spkr_sample_idx + 20 < spkr_buf_size ? spkr_sample_idx * sizeof(spkr_sample_t) + 20 : spkr_buf_alloc_size;
|
|
|
|
// alBufferData(spkr_buffers[freeBuffers], AL_FORMAT_STEREO16, spkr_samples, bufSize + spkr_extra_buf, spkr_sample_rate);
|
2020-06-12 04:53:53 +00:00
|
|
|
al_check_error();
|
2020-06-12 06:37:18 +00:00
|
|
|
alSourceQueueBuffers(spkr_src[SPKR_SRC_GAME_SFX], 1, &spkr_buffers[freeBuffers]);
|
2020-06-12 04:53:53 +00:00
|
|
|
al_check_error();
|
2020-05-17 23:01:15 +00:00
|
|
|
}
|
2020-06-24 05:59:20 +00:00
|
|
|
|
|
|
|
ALenum state;
|
|
|
|
alGetSourcei( spkr_src[SPKR_SRC_GAME_SFX], AL_SOURCE_STATE, &state );
|
|
|
|
// al_check_error();
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case AL_PAUSED:
|
|
|
|
if ( --playDelay <= 0 ) {
|
|
|
|
alSourcePlay(spkr_src[SPKR_SRC_GAME_SFX]);
|
2020-06-27 23:54:02 +00:00
|
|
|
playDelay = SPKR_PLAY_DELAY;
|
2020-06-24 05:59:20 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_PLAYING:
|
|
|
|
// already playing
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-06-23 02:23:41 +00:00
|
|
|
alSourcePlay(spkr_src[SPKR_SRC_GAME_SFX]);
|
2020-07-04 17:13:12 +00:00
|
|
|
// this is so we will set state to AL_PAUSED immediately
|
|
|
|
// As a result there will be an extra queued buffer
|
|
|
|
// which gives us a glitch free sound
|
2020-06-24 05:59:20 +00:00
|
|
|
alSourcePause(spkr_src[SPKR_SRC_GAME_SFX]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear the slack buffer , so we can fill it up by new data
|
|
|
|
for ( int i = 0; i < spkr_buf_size + spkr_extra_buf; i++ ) {
|
|
|
|
spkr_samples[i] = spkr_level;
|
|
|
|
}
|
|
|
|
|
2020-06-12 04:53:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 05:59:20 +00:00
|
|
|
// start from the beginning
|
|
|
|
// spkr_sample_idx = 0;
|
|
|
|
|
2020-05-17 14:49:05 +00:00
|
|
|
}
|
2020-05-10 22:30:29 +00:00
|
|
|
|
2020-06-24 05:59:20 +00:00
|
|
|
spkr_clk = 0;
|
2020-06-27 23:54:02 +00:00
|
|
|
|
2020-06-24 05:59:20 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
spkr_clk += clkfrm;
|
2020-05-10 06:40:37 +00:00
|
|
|
}
|
2020-07-04 17:13:12 +00:00
|
|
|
|
|
|
|
// free up unused buffers
|
|
|
|
spkr_unqueue( spkr_src[SPKR_SRC_GAME_SFX] );
|
2020-06-12 01:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-12 04:53:53 +00:00
|
|
|
void spkr_playqueue_sfx( ALuint src, uint8_t * sfx, int len ) {
|
2020-06-12 01:29:26 +00:00
|
|
|
|
2020-06-27 23:54:02 +00:00
|
|
|
// printf("%s freeBuffers:%d\n", __FUNCTION__, freeBuffers);
|
|
|
|
|
2020-06-12 04:53:53 +00:00
|
|
|
if ( freeBuffers ) {
|
|
|
|
ALenum queued;
|
|
|
|
alGetSourcei( src, AL_BUFFERS_QUEUED, &queued );
|
|
|
|
// printf("Q:%u\n", queued);
|
2020-06-12 01:29:26 +00:00
|
|
|
|
2020-06-27 23:54:02 +00:00
|
|
|
// printf("%s queued:%d\n", __FUNCTION__, queued);
|
|
|
|
|
|
|
|
if ( queued < 16 ) {
|
2020-06-12 04:53:53 +00:00
|
|
|
freeBuffers--;
|
|
|
|
alBufferData( spkr_buffers[freeBuffers], AL_FORMAT_STEREO16, sfx, len, sfx_sample_rate );
|
2020-06-12 01:29:26 +00:00
|
|
|
al_check_error();
|
2020-06-12 04:53:53 +00:00
|
|
|
alSourceQueueBuffers( src, 1, &spkr_buffers[freeBuffers] );
|
2020-06-12 01:29:26 +00:00
|
|
|
al_check_error();
|
|
|
|
|
2020-06-12 04:53:53 +00:00
|
|
|
ALenum state;
|
|
|
|
alGetSourcei( src, AL_SOURCE_STATE, &state );
|
|
|
|
// al_check_error();
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case AL_PLAYING:
|
|
|
|
// already playing
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
alSourcePlay( src );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void spkr_play_sfx( ALuint src, uint8_t * sfx, int len ) {
|
2020-06-27 23:54:02 +00:00
|
|
|
|
|
|
|
// printf("%s freeBuffers:%d\n", __FUNCTION__, freeBuffers);
|
|
|
|
|
2020-06-12 04:53:53 +00:00
|
|
|
if ( freeBuffers ) {
|
|
|
|
ALenum state;
|
|
|
|
alGetSourcei( src, AL_SOURCE_STATE, &state );
|
|
|
|
// al_check_error();
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case AL_PAUSED:
|
|
|
|
alSourcePlay( src );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_PLAYING:
|
|
|
|
// already playing
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
freeBuffers--;
|
|
|
|
alBufferData( spkr_buffers[freeBuffers], AL_FORMAT_STEREO16, sfx, len, sfx_sample_rate );
|
|
|
|
al_check_error();
|
|
|
|
alSourceQueueBuffers( src, 1, &spkr_buffers[freeBuffers] );
|
|
|
|
al_check_error();
|
|
|
|
|
|
|
|
alSourcePlay( src );
|
|
|
|
break;
|
|
|
|
}
|
2020-06-12 01:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-12 04:53:53 +00:00
|
|
|
void spkr_stop_sfx( ALuint src ) {
|
2020-06-12 01:29:26 +00:00
|
|
|
ALenum state;
|
|
|
|
alGetSourcei( src, AL_SOURCE_STATE, &state );
|
|
|
|
// al_check_error();
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case AL_PAUSED:
|
|
|
|
case AL_PLAYING:
|
|
|
|
alSourceStop( src );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-06-27 23:54:02 +00:00
|
|
|
|
|
|
|
// free up unused buffers
|
2020-07-04 17:13:12 +00:00
|
|
|
spkr_unqueue( src );
|
2020-06-12 01:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void spkr_play_disk_motor() {
|
2020-06-14 03:13:20 +00:00
|
|
|
if ( ( disk_sfx_enabled ) && ( clk_6502_per_frm <= FRAME(iicplus_MHz_6502) ) ) {
|
2020-06-12 06:37:18 +00:00
|
|
|
spkr_play_sfx( spkr_src[SPKR_SRC_DISK_MOTOR_SFX], diskmotor_sfx, diskmotor_sfx_len );
|
2020-06-12 01:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void spkr_stop_disk_motor( int time ) {
|
2020-06-14 03:13:20 +00:00
|
|
|
if ( ( disk_sfx_enabled ) && ( clk_6502_per_frm <= FRAME(iicplus_MHz_6502) ) ) {
|
2020-06-12 01:29:26 +00:00
|
|
|
spkr_play_disk_motor_time = time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void spkr_play_disk_arm() {
|
2020-06-14 03:13:20 +00:00
|
|
|
if ( ( disk_sfx_enabled ) && ( clk_6502_per_frm <= FRAME(iicplus_MHz_6502) ) ) {
|
2020-06-12 01:29:26 +00:00
|
|
|
if ( spkr_play_disk_ioerr_time == 0 ) {
|
2020-06-12 06:37:18 +00:00
|
|
|
spkr_play_sfx( spkr_src[SPKR_SRC_DISK_ARM_SFX], diskarm_sfx, diskarm_sfx_len );
|
2020-06-12 01:29:26 +00:00
|
|
|
spkr_play_disk_arm_time = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void spkr_play_disk_ioerr() {
|
2020-06-14 03:13:20 +00:00
|
|
|
if ( ( disk_sfx_enabled ) && ( clk_6502_per_frm <= FRAME(iicplus_MHz_6502) ) ) {
|
2020-06-12 06:37:18 +00:00
|
|
|
spkr_playqueue_sfx( spkr_src[SPKR_SRC_DISK_IOERR_SFX], diskioerr_sfx, diskioerr_sfx_len);
|
2020-06-12 04:53:53 +00:00
|
|
|
spkr_play_disk_ioerr_time = 4;
|
2020-06-12 01:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-26 03:23:08 +00:00
|
|
|
void spkr_stopAll() {
|
|
|
|
for ( int i = 0; i < SOURCES_COUNT; i++ ) {
|
|
|
|
spkr_stop_sfx( spkr_src[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 05:08:48 +00:00
|
|
|
void update_disk_sfx( unsigned * time, ALuint src ) {
|
2020-06-12 05:38:03 +00:00
|
|
|
if ( *time ) {
|
|
|
|
if ( --*time == 0 ) {
|
|
|
|
spkr_stop_sfx( src );
|
2020-06-12 01:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void spkr_update_disk_sfx() {
|
2020-06-12 05:38:03 +00:00
|
|
|
// is user speeds up the machine, disk sfx needs to be stopped
|
2020-06-14 03:13:20 +00:00
|
|
|
if ( ( ! disk_sfx_enabled ) || ( clk_6502_per_frm > FRAME(iicplus_MHz_6502) ) ) {
|
2020-06-12 05:38:03 +00:00
|
|
|
if ( spkr_play_disk_motor_time ) {
|
|
|
|
spkr_play_disk_motor_time = 1; // rest will be taken care below
|
|
|
|
}
|
|
|
|
if ( spkr_play_disk_arm_time ) {
|
|
|
|
spkr_play_disk_arm_time = 1; // rest will be taken care below
|
2020-06-12 04:53:53 +00:00
|
|
|
}
|
2020-06-12 01:29:26 +00:00
|
|
|
}
|
2020-06-12 05:38:03 +00:00
|
|
|
|
2020-06-12 06:37:18 +00:00
|
|
|
update_disk_sfx( &spkr_play_disk_motor_time, spkr_src[SPKR_SRC_DISK_MOTOR_SFX] );
|
|
|
|
update_disk_sfx( &spkr_play_disk_arm_time, spkr_src[SPKR_SRC_DISK_ARM_SFX] );
|
2020-06-27 23:54:02 +00:00
|
|
|
update_disk_sfx( &spkr_play_disk_ioerr_time, spkr_src[SPKR_SRC_DISK_IOERR_SFX] );
|
2020-06-12 05:38:03 +00:00
|
|
|
|
2020-06-12 01:29:26 +00:00
|
|
|
}
|
|
|
|
|