Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sound support via POKE? #16

Open
driscoll opened this issue Feb 2, 2017 · 3 comments
Open

Add sound support via POKE? #16

driscoll opened this issue Feb 2, 2017 · 3 comments

Comments

@driscoll
Copy link

driscoll commented Feb 2, 2017

Some of the historical code I've been working with uses POKE/CALL routines for sound effects. I see that the ASCII bell is simulated by playing an audio file so this feature may require significant development work. Take a look at the example code below. Is this within the scope of your plans for this project? I'm happy to take a shot at implementation if supporting this sort of feature seems feasible.

2010 POKE 768,173: POKE 769,48: POKE 770,192: POKE 771,136: POKE 772,208: POKE 773,4: POKE 774,198: POKE 775,1: POKE 776,240
2015 POKE 777,8: POKE 778,202: POKE 779,208: POKE 780,246: POKE 781,166: POKE 782,0: POKE 783,76: POKE 784,0: POKE 785, 3: POKE 786, 96
2030 FOR I = 1 TO 6: READ N,D: POKE 0,N: POKE 1,D: CALL 768: NEXT I: DATA 110,75,70,75,55,75,45,200,55,100,45,255

Thanks so much for creating such a useful tool.

@inexorabletash
Copy link
Owner

inexorabletash commented Feb 2, 2017

That code is creating a machine code routine at location 768 (POKE 768,...) then calling it to play notes with POKE 0,N:POKE 1,D:CALL 768 - presumably that's setting the note and duration.

Supporting that would require a full 6502 emulator which is possible but not the goal of this project. You can take a look at some of the linked projects such as https://www.scullinsteel.com/apple//e which do emulate down to the CPU level and have sound support.

(It's possible to manipulate the speaker from BASIC on an Apple with just a POKE but the language is so slow you can only make a dull buzz, not any useful tones. Machine code is required for actual music.)

A plausible intermediate approach would be for the page to act as if such a routine was already loaded and implement support for just the POKE 0,N: POKE 1,D: CALL 768 part. It would allow playing music from the page but would not allow custom sound generation routines. This deviates from what Applesoft BASIC has available but this is not a pure emulator. If you wanted to tackle that as a project I'd be supportive. The best way to do it would be via the Web Audio API which is supported in modern browsers. A good introductory step to become familiar with the API would be to replace the current bell mechanism.

@inexorabletash
Copy link
Owner

Note to self: to perform the beep using Web Audio, this seems to do the trick:

var ctx = new AudioContext();
var osc = new OscillatorNode(ctx);
var gain = ctx.createGain();
osc.type = 'square';
osc.frequency.value = 935;
gain.gain.value = 0.75;
osc.connect(gain).connect(ctx.destination);
osc.start();
osc.stop(0.1);

@inexorabletash
Copy link
Owner

An alternative would be to add support for a handful of "ampersand routines". Back in the olden days, Applesoft could be extended by BRUNing a binary routine that would add hooks that intercepted the use of & in the language, adding support for custom syntax such as &NOTE N,D to the language.

There was no standard set of ampersand routines, but I'd be supportive of adding some starting with this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants