Skip to content

Commit 8ec753c

Browse files
Merge pull request #15 from KristofferStrube/master
Updated JSInterop for sending and playing audio data.
2 parents bea1c7e + 208cdc2 commit 8ec753c

2 files changed

Lines changed: 29 additions & 53 deletions

File tree

Platforms/ZXBox.Blazor/Components/EmulatorComponent.razor.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,20 @@ private async void GameLoop_Elapsed(object sender, ElapsedEventArgs e)
138138
IntPtr pinnedsound;
139139
WebAssemblyJSRuntime mono;
140140
byte[] soundbytes;
141+
141142
protected async Task BufferSound()
142143
{
143144
soundbytes = beeper.GetSoundBuffer();
144-
gchsound = GCHandle.Alloc(soundbytes, GCHandleType.Pinned);
145-
pinnedsound = gchsound.AddrOfPinnedObject();
146-
mono.InvokeUnmarshalled<IntPtr, string>("addAudioBuffer", pinnedsound);
147-
gchsound.Free();
145+
mono.InvokeVoid("addAudioBuffer", soundbytes);
148146
}
149147

150148
public double PercentLoaded = 0;
151149
protected async override void OnAfterRender(bool firstRender)
152150
{
153-
151+
if (firstRender)
152+
{
153+
await JSRuntime.InvokeAsync<bool>("InitCanvas");
154+
}
154155
base.OnAfterRender(firstRender);
155156
}
156157

Platforms/ZXBox.Blazor/wwwroot/index.html

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,62 +13,37 @@
1313

1414
<body>
1515
<script>
16-
//Sound
17-
var audiobuffer = [];
16+
// Variables
1817
var nextStartTime = 0;
19-
var lastcall = performance.now()
20-
var audiocontext = new (window.AudioContext || window.webkitAudioContext)();
21-
var gainNode = audiocontext.createGain();
22-
gainNode.gain.value = 0.01;
23-
24-
function addAudioBuffer
25-
(dataPtr) {
26-
var buffersize = 960;
27-
var myArrayBuffer = audiocontext.createBuffer(1, buffersize, audiocontext.sampleRate);
28-
var nowBuffering = myArrayBuffer.getChannelData(0);
29-
30-
for (var d = 0; d < buffersize; d++) {
31-
nowBuffering[d] = Module.HEAPU8[dataPtr + d];
32-
//console.log(nowBuffering[d]);
33-
}
34-
let source = audiocontext.createBufferSource();
35-
source.buffer = myArrayBuffer;
18+
var buffersize = 960;
3619

37-
gainNode.connect(audiocontext.destination);
38-
source.connect(gainNode);
39-
if (nextStartTime == 0) nextStartTime = audiocontext.currentTime + (myArrayBuffer.length / myArrayBuffer.sampleRate);
20+
// Create audioContext and gain node.
21+
// Gain is set to 10% as we will add data 256 times larger than the normal max to our audio buffer source node.
22+
var audioContext = new AudioContext();
23+
var gainNode = new GainNode(audioContext, { gain: 0.1 / 256 });
4024

41-
source.start(nextStartTime);
42-
nextStartTime += myArrayBuffer.length / myArrayBuffer.sampleRate;
43-
gainNode.gain.value = 0.0005;
44-
45-
//console.log(dataPtr);
46-
}
25+
// Connect the gain node.
26+
gainNode.connect(audioContext.destination);
4727

28+
function addAudioBuffer(byteData) {
29+
// Create audiobuffer
30+
var audioBuffer = audioContext.createBuffer(1, buffersize, audioContext.sampleRate);
4831

32+
// Create Float32Array from UInt8Array via constructor.
33+
var newChannelData = new Float32Array(byteData)
34+
audioBuffer.copyToChannel(newChannelData, 0);
4935

50-
//function play() {
51-
// console.log("Playing sound");
52-
// soundon = true;
53-
// //end of stream has been reached
54-
// if (audiobuffer.length === 0)
55-
// {
56-
// console.log("To slow");
57-
// soundon = false;
58-
// return;
59-
// }
60-
// let source = audiocontext.createBufferSource();
61-
// //get the latest buffer that should play next
62-
// source.buffer = audiobuffer.shift();
63-
// source.connect(audiocontext.destination);
64-
65-
// //add this function as a callback to play next buffer
66-
// //when current buffer has reached its end
67-
// source.onended = play;
68-
// source.start();
69-
//}
36+
// Create audio source from audio buffer
37+
let source = new AudioBufferSourceNode(audioContext, { buffer: audioBuffer })
7038

39+
// Connect the new source node to the main gain node.
40+
source.connect(gainNode);
7141

42+
// Update nextStartTime and start source node.
43+
if (nextStartTime == 0) nextStartTime = audioContext.currentTime + (audioBuffer.length / audioBuffer.sampleRate);
44+
source.start(nextStartTime);
45+
nextStartTime += audioBuffer.length / audioBuffer.sampleRate;
46+
}
7247

7348
var pressedKeys = [];
7449

0 commit comments

Comments
 (0)