Skip to content

Commit

Permalink
Fixed up the audio and graphics examples, more linting, renamed
Browse files Browse the repository at this point in the history
categories
  • Loading branch information
torch2424 committed Jun 24, 2019
1 parent c982b55 commit 06542fc
Show file tree
Hide file tree
Showing 22 changed files with 7,520 additions and 203 deletions.
8 changes: 8 additions & 0 deletions assets/Web_Assembly_Logo.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/favicon.ico
Binary file not shown.
6 changes: 3 additions & 3 deletions build-system/example-info.js
Expand Up @@ -9,11 +9,11 @@ const concepts = {
]
};

const lowLevelDemos = {
title: "Low Level Demos",
const applyingTheConcepts = {
title: "Applying the Concepts",
examples: ["graphics", "audio"]
};

module.exports = {
categories: [concepts, lowLevelDemos]
categories: [concepts, applyingTheConcepts]
};
6 changes: 5 additions & 1 deletion demo-util/instantiateWasm.js
Expand Up @@ -2,7 +2,11 @@ export const wasmBrowserInstantiate = async (wasmModuleUrl, importObject) => {
let response = undefined;

if (!importObject) {
importObject = {};
importObject = {
env: {
abort: () => console.log("Abort!")
}
};
}

if (WebAssembly.instantiateStreaming) {
Expand Down
69 changes: 43 additions & 26 deletions examples/audio/audio.assemblyscript.en-us.md
Expand Up @@ -23,19 +23,25 @@ As usual, let's get started with our `index.ts` file. You will notice here we gr
// By growing our Wasm Memory by 1 page (64KB)
memory.grow(1);

// Function to do the amplification
// inputPointer is where (memory index) we placed the input audio samples.
// inputLength is the number of samples in the audio buffer (that the pointer points to).
export function amplifyAudioInBuffer(inputPointer: i32, inputLength: i32): i32 {
// Create a pointer (memory index) of where
// We will place the output audio samples
// For this example, it will be right after the input
let outputPointer: i32 = inputPointer + inputLength;
// Create some buffer/pointers (array index and size) to where
// in memory we are storing the pixels.
// NOTE: Be sure to set a correct --memoryBase when
// when writing to memory directly like we are here.
// https://docs.assemblyscript.org/details/compiler
// Javascript writes to the INPUT_BUFFER,
// and Wasm will write the result in the OUTPUT_BUFFER
export const INPUT_BUFFER_POINTER: i32 = 0;
export const INPUT_BUFFER_SIZE: i32 = 1024;
export const OUTPUT_BUFFER_POINTER: i32 =
INPUT_BUFFER_POINTER + INPUT_BUFFER_SIZE;
export const OUTPUT_BUFFER_SIZE: i32 = INPUT_BUFFER_SIZE;

// Function to do the amplification
export function amplifyAudioInBuffer(): void {
// Loop over the samples
for (let i = 0; i < inputLength; i++) {
for (let i = 0; i < INPUT_BUFFER_SIZE; i++) {
// Load the sample at the index
let audioSample: u8 = load<u8>(inputPointer + i);
let audioSample: u8 = load<u8>(INPUT_BUFFER_POINTER + i);

// Amplify the sample. All samples
// Should be implemented as bytes.
Expand All @@ -49,11 +55,8 @@ export function amplifyAudioInBuffer(inputPointer: i32, inputLength: i32): i32 {
}

// Store the audio sample into our output buffer
store<u8>(outputPointer + i, audioSample);
store<u8>(OUTPUT_BUFFER_POINTER + i, audioSample);
}

// Return where we placed the output buffer
return outputPointer;
}
```

Expand Down Expand Up @@ -165,21 +168,20 @@ const runWasm = async () => {
);

// Fill our wasm memory with the converted Audio Samples,
// And store it at our inputPointer location (index)
const inputPointer = 0;
wasmByteMemoryArray.set(originalByteAudioSamples, inputPointer);
// And store it at our INPUT_BUFFER_POINTER (wasm memory index)
wasmByteMemoryArray.set(
originalByteAudioSamples,
exports.INPUT_BUFFER_POINTER.valueOf()
);

// Amplify our loaded samples with our export Wasm function
// This returns our outputPointer (index were the sample buffer was stored)
const outputPointer = exports.amplifyAudioInBuffer(
inputPointer,
numberOfSamples
);
exports.amplifyAudioInBuffer();

// Slice out the amplified byte audio samples
const outputBuffer = wasmByteMemoryArray.slice(
outputPointer,
outputPointer + numberOfSamples
exports.OUTPUT_BUFFER_POINTER.valueOf(),
exports.OUTPUT_BUFFER_POINTER.valueOf() +
exports.OUTPUT_BUFFER_SIZE.valueOf()
);

// Convert our amplified byte samples into float samples,
Expand All @@ -195,7 +197,7 @@ const runWasm = async () => {
runWasm();
```

Next, we need to provide a way to actually play the audio buffers. Thus, at the bottom of our `index.js` we will add:
Next, we need to provide a way to actually play/pause the audio buffers. Thus, at the bottom of our `index.js` we will add:

```javascript
function beforePlay() {
Expand All @@ -220,9 +222,19 @@ window.playAmplified = () => {
audioBuffer.getChannelData(0).set(amplifiedAudioSamples);
audioBuffer.getChannelData(1).set(amplifiedAudioSamples);
};

window.pause = () => {
beforePlay();
// Create/Set the buffer to silence
const silence = [];
silence.length = numberOfSamples;
silence.fill(0);
audioBuffer.getChannelData(0).set(silence);
audioBuffer.getChannelData(1).set(silence);
};
```

Finally, let's make sure we have the following to our `index.html` to provide buttons to call our play functions so we can actually play our audio:
Finally, let's make sure we have the following to our `index.html` to provide buttons to call our play/pause functions so we can actually play our audio:

```html
<!-- Other HTML here. -->
Expand All @@ -237,6 +249,11 @@ Finally, let's make sure we have the following to our `index.html` to provide bu

<h1>Amplified Sine Wave</h1>
<div><button onclick="playAmplified()">Play</button></div>

<hr />

<h1>Pause</h1>
<div><button onclick="pause()">Pause</button></div>
</body>

<!-- Other HTML here. -->
Expand Down
19 changes: 17 additions & 2 deletions examples/audio/audio.rust.en-us.md
Expand Up @@ -220,7 +220,7 @@ const runWasm = async () => {
runWasm();
```
Next, we need to provide a way to actually play the audio buffers. Thus, at the bottom of our `index.js` we will add:
Next, we need to provide a way to actually play/pause the audio buffers. Thus, at the bottom of our `index.js` we will add:
```javascript
function beforePlay() {
Expand All @@ -245,9 +245,19 @@ window.playAmplified = () => {
audioBuffer.getChannelData(0).set(amplifiedAudioSamples);
audioBuffer.getChannelData(1).set(amplifiedAudioSamples);
};

window.pause = () => {
beforePlay();
// Create/Set the buffer to silence
const silence = [];
silence.length = numberOfSamples;
silence.fill(0);
audioBuffer.getChannelData(0).set(silence);
audioBuffer.getChannelData(1).set(silence);
};
```
Finally, let's make sure we have the following to our `index.html` to provide buttons to call our play functions so we can actually play our audio:
Finally, let's make sure we have the following to our `index.html` to provide buttons to call our play/pause functions so we can actually play our audio:
```html
<!-- Other HTML here. -->
Expand All @@ -262,6 +272,11 @@ Finally, let's make sure we have the following to our `index.html` to provide bu

<h1>Amplified Sine Wave</h1>
<div><button onclick="playAmplified()">Play</button></div>

<hr />

<h1>Pause</h1>
<div><button onclick="pause()">Pause</button></div>
</body>

<!-- Other HTML here. -->
Expand Down
3 changes: 3 additions & 0 deletions examples/audio/demo/assemblyscript/index.html
Expand Up @@ -12,5 +12,8 @@ <h1>Original Sine Wave</h1>
<hr />
<h1>Amplified Sine Wave</h1>
<div><button onclick="playAmplified()">Play</button></div>
<hr />
<h1>Pause</h1>
<div><button onclick="pause()">Pause</button></div>
</body>
</html>
29 changes: 19 additions & 10 deletions examples/audio/demo/assemblyscript/index.js
Expand Up @@ -96,21 +96,20 @@ const runWasm = async () => {
);

// Fill our wasm memory with the converted Audio Samples,
// And store it at our inputPointer location (index)
const inputPointer = 0;
wasmByteMemoryArray.set(originalByteAudioSamples, inputPointer);
// And store it at our INPUT_BUFFER_POINTER (wasm memory index)
wasmByteMemoryArray.set(
originalByteAudioSamples,
exports.INPUT_BUFFER_POINTER.valueOf()
);

// Amplify our loaded samples with our export Wasm function
// This returns our outputPointer (index were the sample buffer was stored)
const outputPointer = exports.amplifyAudioInBuffer(
inputPointer,
numberOfSamples
);
exports.amplifyAudioInBuffer();

// Slice out the amplified byte audio samples
const outputBuffer = wasmByteMemoryArray.slice(
outputPointer,
outputPointer + numberOfSamples
exports.OUTPUT_BUFFER_POINTER.valueOf(),
exports.OUTPUT_BUFFER_POINTER.valueOf() +
exports.OUTPUT_BUFFER_SIZE.valueOf()
);

// Convert our amplified byte samples into float samples,
Expand Down Expand Up @@ -147,3 +146,13 @@ window.playAmplified = () => {
audioBuffer.getChannelData(0).set(amplifiedAudioSamples);
audioBuffer.getChannelData(1).set(amplifiedAudioSamples);
};

window.pause = () => {
beforePlay();
// Create/Set the buffer to silence
const silence = [];
silence.length = numberOfSamples;
silence.fill(0);
audioBuffer.getChannelData(0).set(silence);
audioBuffer.getChannelData(1).set(silence);
};
31 changes: 17 additions & 14 deletions examples/audio/demo/assemblyscript/index.ts
Expand Up @@ -2,19 +2,25 @@
// By growing our Wasm Memory by 1 page (64KB)
memory.grow(1);

// Function to do the amplification
// inputPointer is where (memory index) we placed the input audio samples.
// inputLength is the number of samples in the audio buffer (that the pointer points to).
export function amplifyAudioInBuffer(inputPointer: i32, inputLength: i32): i32 {
// Create a pointer (memory index) of where
// We will place the output audio samples
// For this example, it will be right after the input
let outputPointer: i32 = inputPointer + inputLength;
// Create some buffer/pointers (array index and size) to where
// in memory we are storing the pixels.
// NOTE: Be sure to set a correct --memoryBase when
// when writing to memory directly like we are here.
// https://docs.assemblyscript.org/details/compiler
// Javascript writes to the INPUT_BUFFER,
// and Wasm will write the result in the OUTPUT_BUFFER
export const INPUT_BUFFER_POINTER: i32 = 0;
export const INPUT_BUFFER_SIZE: i32 = 1024;
export const OUTPUT_BUFFER_POINTER: i32 =
INPUT_BUFFER_POINTER + INPUT_BUFFER_SIZE;
export const OUTPUT_BUFFER_SIZE: i32 = INPUT_BUFFER_SIZE;

// Function to do the amplification
export function amplifyAudioInBuffer(): void {
// Loop over the samples
for (let i = 0; i < inputLength; i++) {
for (let i = 0; i < INPUT_BUFFER_SIZE; i++) {
// Load the sample at the index
let audioSample: u8 = load<u8>(inputPointer + i);
let audioSample: u8 = load<u8>(INPUT_BUFFER_POINTER + i);

// Amplify the sample. All samples
// Should be implemented as bytes.
Expand All @@ -28,9 +34,6 @@ export function amplifyAudioInBuffer(inputPointer: i32, inputLength: i32): i32 {
}

// Store the audio sample into our output buffer
store<u8>(outputPointer + i, audioSample);
store<u8>(OUTPUT_BUFFER_POINTER + i, audioSample);
}

// Return where we placed the output buffer
return outputPointer;
}
Binary file modified examples/audio/demo/assemblyscript/index.wasm
Binary file not shown.

0 comments on commit 06542fc

Please sign in to comment.