mirror of
https://github.com/trudnai/Steve2.git
synced 2025-03-15 21:30:54 +00:00
All SFX moved to one queue buffer array
This commit is contained in:
parent
9e6aebeaa8
commit
f09a78c0c8
@ -683,7 +683,7 @@ void m6502_ColdReset( const char * bundlePath, const char * romFileName ) {
|
||||
// wait 100ms to be sure simulation has been halted
|
||||
usleep(100000);
|
||||
|
||||
printf("Bundlepath: %s\n", bundlePath);
|
||||
// printf("Bundlepath: %s\n", bundlePath);
|
||||
|
||||
// epoch = rdtsc();
|
||||
// sleep(1);
|
||||
|
@ -51,14 +51,13 @@ const char* al_err_str(ALenum err) {
|
||||
|
||||
ALCdevice *dev = NULL;
|
||||
ALCcontext *ctx = NULL;
|
||||
ALuint spkr_buf = 0;
|
||||
ALuint spkr_src [4] = { 0, 0, 0, 0 };
|
||||
|
||||
ALuint spkr_src [4] = { 0, 0, 0, 0 };
|
||||
|
||||
int spkr_level = SPKR_LEVEL_ZERO;
|
||||
|
||||
|
||||
#define BUFFER_COUNT 10
|
||||
#define BUFFER_COUNT 256
|
||||
#define SOURCES_COUNT 1
|
||||
|
||||
ALuint spkr_buffers[BUFFER_COUNT];
|
||||
@ -70,7 +69,7 @@ ALuint spkr_disk_ioerr_buf = 0;
|
||||
const int spkr_fps = fps;
|
||||
const int spkr_seconds = 1;
|
||||
const unsigned spkr_sample_rate = 44100;
|
||||
const unsigned sfx_sample_rate = 22050;
|
||||
const unsigned sfx_sample_rate = 29400; // 22050;
|
||||
unsigned spkr_extra_buf = 800 / spkr_fps;
|
||||
const unsigned spkr_buf_size = spkr_seconds * spkr_sample_rate * 2 / spkr_fps;
|
||||
int16_t spkr_samples [ spkr_buf_size * spkr_fps * BUFFER_COUNT * 2]; // stereo
|
||||
@ -173,8 +172,8 @@ void spkr_init() {
|
||||
al_check_error();
|
||||
alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
|
||||
al_check_error();
|
||||
alListener3f(AL_ORIENTATION, 0.0, -16.0, 0.0);
|
||||
al_check_error();
|
||||
// alListener3f(AL_ORIENTATION, 0.0, -16.0, 0.0);
|
||||
// al_check_error();
|
||||
|
||||
|
||||
// Set-up disk motor sound source and play buffer
|
||||
@ -186,12 +185,12 @@ void spkr_init() {
|
||||
al_check_error();
|
||||
alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
|
||||
al_check_error();
|
||||
alListener3f(AL_ORIENTATION, 0.0, -16.0, 0.0);
|
||||
al_check_error();
|
||||
// alListener3f(AL_ORIENTATION, 0.0, -16.0, 0.0);
|
||||
// al_check_error();
|
||||
|
||||
|
||||
// Set-up disk arm sound source and play buffer
|
||||
alSourcei(spkr_src[2], AL_LOOPING, AL_TRUE);
|
||||
alSourcei(spkr_src[2], AL_LOOPING, AL_FALSE);
|
||||
al_check_error();
|
||||
alSourcef(spkr_src[2], AL_ROLLOFF_FACTOR, 0);
|
||||
al_check_error();
|
||||
@ -199,8 +198,8 @@ void spkr_init() {
|
||||
al_check_error();
|
||||
alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
|
||||
al_check_error();
|
||||
alListener3f(AL_ORIENTATION, 0.0, -16.0, 0.0);
|
||||
al_check_error();
|
||||
// alListener3f(AL_ORIENTATION, 0.0, -16.0, 0.0);
|
||||
// al_check_error();
|
||||
|
||||
|
||||
// Set-up disk io error sound source and play buffer
|
||||
@ -212,8 +211,8 @@ void spkr_init() {
|
||||
al_check_error();
|
||||
alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
|
||||
al_check_error();
|
||||
alListener3f(AL_ORIENTATION, 0.0, -16.0, 0.0);
|
||||
al_check_error();
|
||||
// alListener3f(AL_ORIENTATION, 0.0, -16.0, 0.0);
|
||||
// al_check_error();
|
||||
|
||||
|
||||
// start from the beginning
|
||||
@ -313,153 +312,160 @@ void spkr_toggle() {
|
||||
ALint freeBuffers = BUFFER_COUNT;
|
||||
//ALuint alBuffers[BUFFER_COUNT];
|
||||
|
||||
int spkr_unqueue( ALuint src ) {
|
||||
ALint processed = 0;
|
||||
|
||||
alGetSourcei ( src, AL_BUFFERS_PROCESSED, &processed );
|
||||
al_check_error();
|
||||
|
||||
if ( processed ) {
|
||||
alSourceUnqueueBuffers( src, processed, &spkr_buffers[freeBuffers]);
|
||||
al_check_error();
|
||||
}
|
||||
|
||||
return processed;
|
||||
}
|
||||
|
||||
void spkr_update() {
|
||||
if ( spkr_play_time ) {
|
||||
|
||||
ALint processed = 0;
|
||||
|
||||
// printf("freeBuffers:%d queued:%d processed:%d\n", freeBuffers, queued,processed);
|
||||
|
||||
do {
|
||||
alGetSourcei (spkr_src[0], AL_BUFFERS_PROCESSED, &processed);
|
||||
// al_check_error();
|
||||
|
||||
if ( processed ) {
|
||||
alSourceUnqueueBuffers( spkr_src[0], processed, &spkr_buffers[freeBuffers]);
|
||||
// al_check_error();
|
||||
freeBuffers += processed;
|
||||
}
|
||||
|
||||
} while( freeBuffers <= 0 );
|
||||
|
||||
// free up unused buffers
|
||||
freeBuffers += spkr_unqueue( spkr_src[0] );
|
||||
freeBuffers = clamp( 1, freeBuffers, BUFFER_COUNT );
|
||||
|
||||
// printf("freeBuffers2: %d processed: %d\n", freeBuffers, processed);
|
||||
|
||||
ALenum state;
|
||||
alGetSourcei( spkr_src[0], AL_SOURCE_STATE, &state );
|
||||
// al_check_error();
|
||||
|
||||
////////// check if there is no sound generated for long time, and fade out speaker level to avoid pops and crackles
|
||||
if ( freeBuffers ) {
|
||||
|
||||
if ( --spkr_play_time == 0 ) {
|
||||
// we need to soft mute the speaker to eliminate clicking noise
|
||||
// simple linear mute
|
||||
const int steepness = 64;
|
||||
|
||||
float fadeLevel = spkr_level - SPKR_LEVEL_ZERO;
|
||||
|
||||
if ( spkr_level != SPKR_LEVEL_ZERO ) {
|
||||
spkr_sample_idx = 0;
|
||||
ALenum state;
|
||||
alGetSourcei( spkr_src[0], AL_SOURCE_STATE, &state );
|
||||
// al_check_error();
|
||||
|
||||
while ( ( fadeLevel < -1 ) || ( fadeLevel > 1 ) ) {
|
||||
spkr_samples[ spkr_sample_idx++ ] = SPKR_LEVEL_ZERO + fadeLevel;
|
||||
spkr_samples[ spkr_sample_idx++ ] = SPKR_LEVEL_ZERO + fadeLevel;
|
||||
|
||||
// how smooth we want the speeker to decay, so we will hear no pops and crackles
|
||||
fadeLevel *= 0.999;
|
||||
if ( --spkr_play_time == 0 ) {
|
||||
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;
|
||||
spkr_samples[ spkr_sample_idx++ ] = SPKR_LEVEL_ZERO + fadeLevel;
|
||||
|
||||
// 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;
|
||||
memset(spkr_samples + spkr_sample_idx, SPKR_LEVEL_ZERO, spkr_extra_buf * sizeof(spkr_samples[0]));
|
||||
|
||||
freeBuffers--;
|
||||
alBufferData(spkr_buffers[freeBuffers], AL_FORMAT_STEREO16, spkr_samples, spkr_sample_idx * sizeof(spkr_samples[0]), spkr_sample_rate);
|
||||
al_check_error();
|
||||
alSourceQueueBuffers(spkr_src[0], 1, &spkr_buffers[freeBuffers]);
|
||||
al_check_error();
|
||||
}
|
||||
spkr_level = SPKR_LEVEL_ZERO;
|
||||
|
||||
//spkr_samples[sample_idx] = spkr_level;
|
||||
memset(spkr_samples + spkr_sample_idx, SPKR_LEVEL_ZERO, spkr_extra_buf * sizeof(spkr_samples[0]));
|
||||
|
||||
}
|
||||
else {
|
||||
freeBuffers--;
|
||||
alBufferData(spkr_buffers[freeBuffers], AL_FORMAT_STEREO16, spkr_samples, spkr_sample_idx * sizeof(spkr_samples[0]), spkr_sample_rate);
|
||||
alBufferData(spkr_buffers[freeBuffers], AL_FORMAT_STEREO16, spkr_samples, (spkr_buf_size + spkr_extra_buf) * sizeof(spkr_samples[0]), spkr_sample_rate);
|
||||
al_check_error();
|
||||
alSourceQueueBuffers(spkr_src[0], 1, &spkr_buffers[freeBuffers]);
|
||||
al_check_error();
|
||||
}
|
||||
|
||||
switch (state) {
|
||||
case AL_PAUSED:
|
||||
alSourcePlay(spkr_src[0]);
|
||||
break;
|
||||
|
||||
case AL_PLAYING:
|
||||
// already playing
|
||||
break;
|
||||
|
||||
default:
|
||||
alSourcePlay(spkr_src[0]);
|
||||
alSourcePause(spkr_src[0]);
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
// start from the beginning
|
||||
spkr_sample_idx = 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void spkr_playqueue_sfx( ALuint src, uint8_t * sfx, int len ) {
|
||||
|
||||
// freeBuffers += spkr_unqueue( src );
|
||||
// freeBuffers = clamp( 1, freeBuffers, BUFFER_COUNT );
|
||||
|
||||
if ( freeBuffers ) {
|
||||
ALenum queued;
|
||||
alGetSourcei( src, AL_BUFFERS_QUEUED, &queued );
|
||||
// printf("Q:%u\n", queued);
|
||||
|
||||
if ( queued < 32 ) {
|
||||
freeBuffers--;
|
||||
alBufferData(spkr_buffers[freeBuffers], AL_FORMAT_STEREO16, spkr_samples, (spkr_buf_size + spkr_extra_buf) * sizeof(spkr_samples[0]), spkr_sample_rate);
|
||||
alBufferData( spkr_buffers[freeBuffers], AL_FORMAT_STEREO16, sfx, len, sfx_sample_rate );
|
||||
al_check_error();
|
||||
alSourceQueueBuffers(spkr_src[0], 1, &spkr_buffers[freeBuffers]);
|
||||
alSourceQueueBuffers( src, 1, &spkr_buffers[freeBuffers] );
|
||||
al_check_error();
|
||||
|
||||
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 ) {
|
||||
if ( freeBuffers ) {
|
||||
ALenum state;
|
||||
alGetSourcei( src, AL_SOURCE_STATE, &state );
|
||||
// al_check_error();
|
||||
|
||||
switch (state) {
|
||||
case AL_PAUSED:
|
||||
alSourcePlay(spkr_src[0]);
|
||||
alSourcePlay( src );
|
||||
break;
|
||||
|
||||
|
||||
case AL_PLAYING:
|
||||
// already playing
|
||||
break;
|
||||
|
||||
default:
|
||||
alSourcePlay(spkr_src[0]);
|
||||
alSourcePause(spkr_src[0]);
|
||||
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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
// memset(spkr_samples, spkr_level, (spkr_buf_size + spkr_extra_buf) * sizeof(spkr_samples[0]));
|
||||
|
||||
// start from the beginning
|
||||
spkr_sample_idx = 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void spkr_playqueue_sfx( ALuint src, ALuint * buf, uint8_t * sfx, int len ) {
|
||||
|
||||
alBufferData( *buf, AL_FORMAT_STEREO16, sfx, len, sfx_sample_rate );
|
||||
al_check_error();
|
||||
alSourceQueueBuffers( src, 1, buf );
|
||||
al_check_error();
|
||||
|
||||
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, ALuint * buf, uint8_t * sfx, int len ) {
|
||||
|
||||
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:
|
||||
alBufferData( *buf, AL_FORMAT_STEREO16, sfx, len, sfx_sample_rate );
|
||||
al_check_error();
|
||||
alSourceQueueBuffers( src, 1, buf );
|
||||
al_check_error();
|
||||
|
||||
alSourcePlay( src );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void spkr_stop_sfx( ALuint src, ALuint * buf ) {
|
||||
void spkr_stop_sfx( ALuint src ) {
|
||||
|
||||
ALenum state;
|
||||
alGetSourcei( src, AL_SOURCE_STATE, &state );
|
||||
@ -469,7 +475,8 @@ void spkr_stop_sfx( ALuint src, ALuint * buf ) {
|
||||
case AL_PAUSED:
|
||||
case AL_PLAYING:
|
||||
alSourceStop( src );
|
||||
alSourceUnqueueBuffers( src, 1, buf );
|
||||
freeBuffers += spkr_unqueue( src );
|
||||
freeBuffers = clamp( 1, freeBuffers, BUFFER_COUNT );
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -480,7 +487,7 @@ void spkr_stop_sfx( ALuint src, ALuint * buf ) {
|
||||
|
||||
void spkr_play_disk_motor() {
|
||||
if ( diskAccelerator_count == 0 ) {
|
||||
spkr_play_sfx( spkr_src[1], &spkr_disk_motor_buf, diskmotor_sfx, diskmotor_sfx_len );
|
||||
spkr_play_sfx( spkr_src[1], diskmotor_sfx, diskmotor_sfx_len );
|
||||
}
|
||||
}
|
||||
|
||||
@ -494,7 +501,7 @@ void spkr_stop_disk_motor( int time ) {
|
||||
void spkr_play_disk_arm() {
|
||||
if ( diskAccelerator_count == 0 ) {
|
||||
if ( spkr_play_disk_ioerr_time == 0 ) {
|
||||
spkr_play_sfx( spkr_src[2], &spkr_disk_arm_buf, diskarm_sfx, diskarm_sfx_len );
|
||||
spkr_play_sfx( spkr_src[2], diskarm_sfx, diskarm_sfx_len );
|
||||
spkr_play_disk_arm_time = 2;
|
||||
// spkr_play_disk_ioerr_time = 2;
|
||||
}
|
||||
@ -505,8 +512,8 @@ void spkr_play_disk_arm() {
|
||||
void spkr_play_disk_ioerr() {
|
||||
// spkr_stop_sfx( spkr_src[3], &spkr_disk_ioerr_buf );
|
||||
if ( diskAccelerator_count == 0 ) {
|
||||
spkr_playqueue_sfx( spkr_src[3], &spkr_disk_ioerr_buf, diskioerr_sfx, diskioerr_sfx_len );
|
||||
spkr_play_disk_ioerr_time = 6;
|
||||
spkr_playqueue_sfx( spkr_src[3], diskioerr_sfx, diskioerr_sfx_len - 512);
|
||||
spkr_play_disk_ioerr_time = 4;
|
||||
}
|
||||
}
|
||||
|
||||
@ -515,7 +522,7 @@ void update_disk_sfx( unsigned * time, ALuint src, ALuint * buf ) {
|
||||
if ( diskAccelerator_count == 0 ) {
|
||||
if ( *time ) {
|
||||
if ( --*time == 0 ) {
|
||||
spkr_stop_sfx( src, buf );
|
||||
spkr_stop_sfx( src );
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -525,7 +532,10 @@ void spkr_update_disk_sfx() {
|
||||
if ( diskAccelerator_count == 0 ) {
|
||||
update_disk_sfx( &spkr_play_disk_motor_time, spkr_src[1], &spkr_disk_motor_buf );
|
||||
update_disk_sfx( &spkr_play_disk_arm_time, spkr_src[2], &spkr_disk_arm_buf );
|
||||
update_disk_sfx( &spkr_play_disk_ioerr_time, spkr_src[3], &spkr_disk_ioerr_buf );
|
||||
// update_disk_sfx( &spkr_play_disk_ioerr_time, spkr_src[3], &spkr_disk_ioerr_buf );
|
||||
if ( spkr_play_disk_ioerr_time ) {
|
||||
spkr_play_disk_ioerr_time--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ disk_t disk = {
|
||||
|
||||
const int diskAccelerator_frames = 2;
|
||||
int diskAccelerator_count = 0;
|
||||
int diskAccelerator_speed = 25 * M / fps; // if less than actual CPU speed means no acceleration
|
||||
int diskAccelerator_speed = 1 * M / fps; // if less than actual CPU speed means no acceleration
|
||||
|
||||
|
||||
// motor position from the magnet state
|
||||
|
Loading…
x
Reference in New Issue
Block a user