Sound works

This commit is contained in:
tudnai
2020-05-17 07:49:05 -07:00
parent aaf7c0ca83
commit cf23cc7983
19 changed files with 9411323 additions and 61 deletions

View File

@@ -39,6 +39,14 @@
<ContextState
contextName = "addr_ind_Y:Apple2_mmio.h">
</ContextState>
<ContextState
contextName = "spkr_playUpd:speaker.c">
<PersistentStrings>
<PersistentString
value = "freeBuffers">
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
contextName = "m6502_ColdReset:6502.c">
<PersistentStrings>
@@ -245,6 +253,9 @@
<PersistentString
value = "txtArr">
</PersistentString>
<PersistentString
value = "clkfrm">
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
@@ -259,7 +270,7 @@
contextName = "HiRes.draw(_:):HiRes.swift">
<PersistentStrings>
<PersistentString
value = "(blockH7 | ( block &amp; bitMask ))">
value = "ctx?.height">
</PersistentString>
<PersistentString
value = "ctx?.data">
@@ -267,9 +278,6 @@
<PersistentString
value = "ctx?.width">
</PersistentString>
<PersistentString
value = "ctx?.bitmapInfo">
</PersistentString>
<PersistentString
value = "ctx?.bytesPerRow">
</PersistentString>
@@ -279,20 +287,23 @@
<PersistentString
value = "HiResLineAddrTbl">
</PersistentString>
<PersistentString
value = "ctx?.bitmapInfo">
</PersistentString>
<PersistentString
value = "(blockH7 | ( block &amp; bitMask ))">
</PersistentString>
<PersistentString
value = "HiRes.blockCols">
</PersistentString>
<PersistentString
value = "shadowScreen">
</PersistentString>
<PersistentString
value = "ctx?.height">
value = "ctx?.bitsPerComponent">
</PersistentString>
<PersistentString
value = "linAddr">
</PersistentString>
<PersistentString
value = "ctx?.bitsPerComponent">
value = "shadowScreen">
</PersistentString>
</PersistentStrings>
</ContextState>
@@ -320,6 +331,23 @@
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
contextName = "woz_read:woz.c">
<PersistentStrings>
<PersistentString
value = "m6502.clktime">
</PersistentString>
<PersistentString
value = "m6502.clktime + clkfrm - m6502.clklast">
</PersistentString>
<PersistentString
value = "m6502.clktime + clkfrm">
</PersistentString>
<PersistentString
value = "m6502.clklast">
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
contextName = "m6502_Run:6502.c">
<PersistentStrings>
@@ -472,6 +500,23 @@
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
contextName = "spkr_switch:mmio.h">
<PersistentStrings>
<PersistentString
value = "freeBuffers">
</PersistentString>
<PersistentString
value = "BUFFER_COUNT">
</PersistentString>
<PersistentString
value = "clk_6502_per_frm_set">
</PersistentString>
<PersistentString
value = "clkfrm">
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
contextName = "ViewController.setCPUClockSpeed(freq:):ViewController.swift">
<PersistentStrings>

View File

@@ -913,8 +913,37 @@
</customSpacing>
</stackView>
<stackView distribution="fill" orientation="vertical" alignment="leading" horizontalStackHuggingPriority="249.99998474121094" verticalStackHuggingPriority="249.99998474121094" detachesHiddenViews="YES" translatesAutoresizingMaskIntoConstraints="NO" id="lVP-FP-u6x" userLabel="CPU Speed Selection">
<rect key="frame" x="8" y="8" width="104" height="186"/>
<rect key="frame" x="8" y="8" width="104" height="217"/>
<subviews>
<splitView dividerStyle="thin" vertical="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Ge3-qD-JO6">
<rect key="frame" x="0.0" y="194" width="104" height="23"/>
<subviews>
<textField verticalHuggingPriority="750" fixedFrame="YES" id="SNE-lV-JXn" userLabel="SoundGap">
<rect key="frame" x="0.0" y="0.0" width="90" height="23"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" borderStyle="bezel" title="13" drawsBackground="YES" id="ZKd-sJ-O4U">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<stepper horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" id="uQO-BN-oqa">
<rect key="frame" x="88" y="-2" width="19" height="27"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<stepperCell key="cell" continuous="YES" alignment="left" maxValue="256" doubleValue="13" id="h0r-W8-egl"/>
<connections>
<action selector="SoundGapChanged:" target="XfG-lQ-9wD" id="Y1Z-lh-l8u"/>
</connections>
</stepper>
</subviews>
<constraints>
<constraint firstAttribute="height" constant="23" id="X5P-d2-aYo"/>
</constraints>
<holdingPriorities>
<real value="250"/>
<real value="250"/>
</holdingPriorities>
</splitView>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="FZk-VW-alq">
<rect key="frame" x="-2" y="169" width="41" height="17"/>
<constraints>
@@ -998,6 +1027,10 @@
</connections>
</button>
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="Ge3-qD-JO6" secondAttribute="trailing" id="CjH-r2-t3y"/>
<constraint firstItem="Ge3-qD-JO6" firstAttribute="leading" secondItem="lVP-FP-u6x" secondAttribute="leading" id="Z0F-GF-dgy"/>
</constraints>
<visibilityPriorities>
<integer value="1000"/>
<integer value="1000"/>
@@ -1007,6 +1040,7 @@
<integer value="1000"/>
<integer value="1000"/>
<integer value="1000"/>
<integer value="1000"/>
</visibilityPriorities>
<customSpacing>
<real value="3.4028234663852886e+38"/>
@@ -1017,6 +1051,7 @@
<real value="3.4028234663852886e+38"/>
<real value="3.4028234663852886e+38"/>
<real value="3.4028234663852886e+38"/>
<real value="3.4028234663852886e+38"/>
</customSpacing>
</stackView>
</subviews>
@@ -1054,6 +1089,7 @@
</constraints>
</view>
<connections>
<outlet property="SoundGap" destination="ZKd-sJ-O4U" id="IrM-7f-7Nb"/>
<outlet property="display" destination="pIk-RC-s5g" id="Hvd-DI-h6z"/>
<outlet property="displayField" destination="SEL-hl-0c0" id="4Pc-hG-qQf"/>
<outlet property="hires" destination="LlM-EV-ruZ" id="E60-pA-HM1"/>

View File

@@ -234,7 +234,7 @@ class ViewController: NSViewController {
}
override func keyDown(with event: NSEvent) {
print("KBD Event")
print("keyDown")
// for i in 0...65536 {
// ddd = Int(event.keyCode) + i
@@ -318,7 +318,7 @@ class ViewController: NSViewController {
override func keyUp(with event: NSEvent) {
print("KBD Event")
print("keyUp")
// switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) {
// case [.command] where event.characters == "l",
// [.command, .shift] where event.characters == "l":
@@ -620,6 +620,10 @@ class ViewController: NSViewController {
}
// }
#endif
// stream speaker from a separate thread from the simulation
// TODO: Do we need to do this from here?
// spkr_update()
}
#if SPEEDTEST
@@ -739,5 +743,12 @@ class ViewController: NSViewController {
}
@IBOutlet weak var SoundGap: NSTextFieldCell!
@IBAction func SoundGapChanged(_ sender: NSStepper) {
SoundGap.integerValue = sender.integerValue
spkr_extra_buf = UInt32( sender.integerValue )
}
}

982794
DOS_3_3_boot_trace.txt Normal file

File diff suppressed because it is too large Load Diff

980459
DOS_3_3_boot_trace_filtered.txt Normal file

File diff suppressed because it is too large Load Diff

788216
a Normal file

File diff suppressed because it is too large Load Diff

0
disassembly_new.marked Normal file
View File

2299754
disksectorload.log Normal file

File diff suppressed because it is too large Load Diff

912974
disksectorload_2.log Normal file

File diff suppressed because it is too large Load Diff

830335
disksectorload_3.log Normal file

File diff suppressed because it is too large Load Diff

811000
disksectorload_4.log Normal file

File diff suppressed because it is too large Load Diff

822744
disksectorload_4.marked Normal file

File diff suppressed because it is too large Load Diff

982794
disksectorload_vii.log Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -774,8 +774,7 @@ void m6502_Run() {
}
// play the entire sound buffer for this frame
spkr_play();
spkr_update();
}
void read_rom( const char * bundlePath, const char * filename, uint8_t * rom, const uint16_t addr ) {
@@ -850,7 +849,7 @@ void m6502_ColdReset( const char * bundlePath, const char * romFileName ) {
mhz = (double)MHz_6502 / M;
spkr_init();
// spkr_play();
spkr_playStart();
unsigned long long saved_frm_set = clk_6502_per_frm_set;
clk_6502_per_frm =

View File

@@ -11,6 +11,7 @@
#include <stdint.h>
#include "../../src/util/common.h"
#include "../../src/dev/audio/speaker.h" // So we can access to speaker stuff from Swift
extern unsigned long long MHz_6502;

View File

@@ -15,6 +15,12 @@
#include <OpenAL/alc.h>
#include "speaker.h"
#include "6502.h"
#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)
#define CASE_RETURN(err) case (err): return #err
@@ -27,6 +33,7 @@ const char* al_err_str(ALenum err) {
CASE_RETURN(AL_INVALID_OPERATION);
CASE_RETURN(AL_OUT_OF_MEMORY);
}
printf("alError: 0x%04X\n", err);
return "unknown";
}
#undef CASE_RETURN
@@ -52,16 +59,23 @@ ALuint spkr_src = 0;
int spkr_level = SPKR_LEVEL_MAX;
const int spkr_fps = 20;
const int spkr_fps = fps;
const int spkr_seconds = 1;
const unsigned spkr_sample_rate = 44100;
const unsigned spkr_buf_size = spkr_seconds * spkr_sample_rate;
char spkr_samples [ spkr_buf_size ];
unsigned spkr_extra_buf = 13; // TODO: Should it be a dynamic value calculated by how many bytes we overshot by the edge curve generator?
const unsigned spkr_buf_size = spkr_seconds * spkr_sample_rate / spkr_fps;
char spkr_samples [ spkr_buf_size * spkr_fps * 100]; // 1s of sound
unsigned spkr_sample_idx = 0;
#define BUFFER_COUNT 10
#define SOURCES_COUNT 1
ALuint spkr_buffers[BUFFER_COUNT];
// initialize OpenAL
void spkr_init() {
const char *defname = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
printf( "Default device: %s\n", defname );
@@ -72,6 +86,23 @@ void spkr_init() {
// Fill buffer with zeros
memset( spkr_samples, spkr_level, spkr_buf_size );
// Create buffer to store samples
alGenBuffers(BUFFER_COUNT, spkr_buffers);
al_check_error();
// Set-up sound source and play buffer
alGenSources(1, &spkr_src);
al_check_error();
alSourcei(spkr_src, AL_LOOPING, AL_FALSE);
al_check_error();
alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
al_check_error();
// start from the beginning
spkr_sample_idx = 0;
}
// Dealloc OpenAL
@@ -88,55 +119,121 @@ void spkr_exit() {
al_check_error();
}
void spkr_play() {
void spkr_playStart() {
}
ALint freeBuffers = BUFFER_COUNT;
//ALuint alBuffers[BUFFER_COUNT];
void spkr_update() {
if ( spkr_sample_idx ) {
if ( spkr_src ) {
alSourceStop(spkr_src);
al_check_error();
alSourcei(spkr_src, AL_BUFFER, 0);
al_check_error();
// printf("freeBuffers: %d", freeBuffers);
// if ( spkr_src ) {
// alSourceStop(spkr_src);
// al_check_error();
// alSourcei(spkr_src, AL_BUFFER, 0);
// al_check_error();
//
// spkr_src = 0;
// }
//
// if ( spkr_buf ) {
// alDeleteBuffers(1, &spkr_buf);
// al_check_error();
// spkr_buf = 0;
// }
ALint processed = 0;
do {
alGetSourcei (spkr_src, AL_BUFFERS_PROCESSED, &processed);
// if ( processed )
freeBuffers += processed;
// al_check_error();
// usleep(100);
// if ( freeBuffers <= 0 ) printf("No Free Buffer\n");
} while( freeBuffers <= 0 );
freeBuffers = clamp( 1, freeBuffers, BUFFER_COUNT );
// printf("freeBuffers2: %d processed: %d\n", freeBuffers, processed);
ALenum state;
alGetSourcei( spkr_src, AL_SOURCE_STATE, &state );
// al_check_error();
if ( processed ) {
alSourceUnqueueBuffers( spkr_src, processed, &spkr_buffers[freeBuffers - processed]);
// al_check_error();
}
if ( spkr_buf ) {
alDeleteBuffers(1, &spkr_buf);
al_check_error();
}
// Create buffer to store samples
spkr_buf = 0;
alGenBuffers(1, &spkr_buf);
al_check_error();
// spkr_samples[0] = 0;
// spkr_samples[1] = 255;
// spkr_samples[2] = 0;
// spkr_samples[3] = 255;
// spkr_samples[4] = 0;
// Download buffer to OpenAL
alBufferData(spkr_buf, AL_FORMAT_MONO8, spkr_samples, spkr_buf_size / spkr_fps, spkr_sample_rate);
// memcpy(spkr_samples + spkr_buf_size, spkr_samples + spkr_buf_size - spkr_extra_buf, spkr_extra_buf);
alBufferData(spkr_buffers[freeBuffers - 1], AL_FORMAT_MONO8, spkr_samples, spkr_buf_size + spkr_extra_buf, spkr_sample_rate);
al_check_error();
// Set-up sound source and play buffer
spkr_src = 0;
alGenSources(1, &spkr_src);
alSourcei(spkr_src, AL_BUFFER, spkr_buf);
alSourcei(spkr_src, AL_LOOPING, 0);
alSourceQueueBuffers(spkr_src, 1, &spkr_buffers[freeBuffers - 1]);
// al_check_error();
// ALint secoffset = 0;
// alGetSourcei( spkr_src, AL_BYTE_OFFSET, &secoffset );
alSourcei( spkr_src, AL_BYTE_OFFSET, 0 );
al_check_error();
alSourcePlay(spkr_src);
switch (state) {
case AL_PAUSED:
alSourcePlay(spkr_src);
break;
case AL_PLAYING:
// already playing
break;
default:
alSourcePlay(spkr_src);
alSourcePause(spkr_src);
break;
}
ALint secoffset = 0;
alGetSourcei( spkr_src, AL_BYTE_OFFSET, &secoffset );
// ccopy slack buffer to the top, so we will not lose the edges
memcpy(spkr_samples, spkr_samples + secoffset, spkr_buf_size - spkr_buf_size / spkr_fps);
// // copy slack buffer to the top, so we will not lose the edges
// memcpy(spkr_samples, spkr_samples + secoffset, spkr_buf_size - secoffset);
// // clear the slack buffer , so we can fill it up by new data
// memset(spkr_samples + secoffset, spkr_level, spkr_buf_size - secoffset);
// clear the slack buffer , so we can fill it up by new data
memset(spkr_samples + secoffset, spkr_level, spkr_buf_size - spkr_buf_size / spkr_fps);
memset(spkr_samples, spkr_level, spkr_buf_size);
// start from the beginning
spkr_sample_idx = 0;
// if ( freeBuffers > 0 ) {
freeBuffers--;
// }
}
else {
alSourceStop(spkr_src);
}
// else {
// // TODO: Need better speaker turn off logic to avoid click noise
//// if ( spkr_src ) {
//// alSourceStop(spkr_src);
//// al_check_error();
//// alSourceUnqueueBuffers( spkr_src, BUFFER_COUNT, spkr_buffers);
//// al_check_error();
//// alSourcei(spkr_src, AL_BUFFER, 0);
//// al_check_error();
//// spkr_src = 0;
//// // clear the buffer
//// memset(spkr_samples, spkr_level, spkr_buf_size);
//// }
//// printf("freeBuffers_nosound: %d\n", freeBuffers);
// }
}
@@ -150,7 +247,7 @@ void spkr_Update() {
}
// Download buffer to OpenAL
alBufferData(spkr_buf, AL_FORMAT_MONO8, spkr_samples, spkr_buf_size / spkr_fps, spkr_sample_rate);
alBufferData(spkr_buf, AL_FORMAT_MONO8, spkr_samples, spkr_buf_size, spkr_sample_rate);
al_check_error();
alSourcei( spkr_src, AL_BYTE_OFFSET, 0 );

View File

@@ -11,20 +11,21 @@
#include <stdio.h>
#define SPKR_LEVEL_MIN 25
#define SPKR_LEVEL_MAX 200
#define SPKR_LEVEL_MIN 0
#define SPKR_LEVEL_MAX 255
extern void spkr_init(void);
extern void spkr_exit(void);
extern void spkr_play(void);
extern void spkr_Update(void);
extern void spkr_update(void);
extern const unsigned spkr_sample_rate;
extern const unsigned spkr_buf_size;
extern const int spkr_fps;
extern char spkr_samples [];
extern unsigned spkr_sample_idx;
extern int spkr_level;
extern int freeBuffers;
extern unsigned spkr_extra_buf;
#endif /* speaker_h */

View File

@@ -263,6 +263,7 @@ uint8_t woz_read() {
// printf("NEED SYNC : %llu\n", clkelpased);
bitOffset = (clkelpased >> 2) & 7;
trackOffset += ((clkelpased >> 5) + magicShiftOffset) % usedBytes;
trackOffset %= usedBytes;
WOZread.data = woz_trks[track].data[trackOffset];
}
@@ -286,6 +287,7 @@ uint8_t woz_read() {
// printf("%02X ", byte);
WOZread.shift = 0;
if (outdev) fprintf(outdev, "byte: %02X\n", byte);
return byte;
}
}

View File

@@ -423,7 +423,7 @@ INLINE void spkr_switch() {
spkr_level = SPKR_LEVEL_MAX;
}
//spkr_samples[sample_idx] = spkr_level;
memset(spkr_samples + spkr_sample_idx, spkr_level, spkr_buf_size - spkr_sample_idx);
memset(spkr_samples + spkr_sample_idx, spkr_level, spkr_buf_size);
//ViewController_spk_up_play();