39 | Check the developer console for more messages from the RNBO device
40 |
41 |
42 |
43 |
Presets
44 | No presets defined
45 |
46 |
47 |
48 |
Parameters
49 | No parameters
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/js/guardrails.js:
--------------------------------------------------------------------------------
1 | /**
2 | * This file just contains a bunch of checks to try to help you avoid some
3 | * common errors. You don't need to include this file in your project, but
4 | * you can if you want to.
5 | */
6 |
7 | const guardrailsErrors = [];
8 |
9 | const displayObtrusiveErrors = (errors) => {
10 | if (errors.length <= 0) return;
11 |
12 | const column = document.createElement('div');
13 | column.setAttribute('style',
14 | `max-width: 560px;
15 | display: flex;
16 | justify-content: center;
17 | align-items: center;
18 | margin: 0 auto;
19 | height: 100%;
20 | position: absolute;
21 | top: 0;
22 | flex-direction: column;
23 | `
24 | );
25 |
26 | errors.forEach(error => {
27 | const { header, description } = error;
28 | const container = document.createElement('div');
29 | container.setAttribute(
30 | 'style',
31 | `border-radius: 5px;
32 | border: gray solid 1px;
33 | width: 100%;
34 | padding: 25px;
35 | margin-bottom: 8px;
36 | background-color: white;
37 | background: #EA5050;
38 | color: white;`
39 | );
40 | const h1 = document.createElement('h1');
41 | h1.appendChild(document.createTextNode(header));
42 | h1.setAttribute(
43 | 'style',
44 | `margin-bottom: 15px;
45 | border-bottom: dashed 1px gray;
46 | padding-bottom: 15px;`
47 | );
48 | const explainer = document.createElement('p');
49 | explainer.appendChild(
50 | document.createTextNode(
51 | description
52 | )
53 | );
54 |
55 | column.appendChild(container);
56 | container.appendChild(h1);
57 | container.appendChild(explainer);
58 | });
59 |
60 | document.getElementById("rnbo-root").appendChild(column);
61 |
62 | const svg = document.getElementById('background');
63 |
64 | svg.innerHTML =
65 | `
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 | `;
77 | };
78 |
79 | const checkIfLocationIsFileURL = (errors) => {
80 | if (window.location.protocol === 'file:') {
81 | errors.push({
82 | header: 'file:// access not supported',
83 | description: 'In order for RNBO to work in the browser, the device has to be served' +
84 | ' from a web server. That means that you can\'t just double-click the' +
85 | ' index.html file. Check out README.md for instructions on how to run' +
86 | ' a static web server.'
87 | });
88 | }
89 | };
90 |
91 | const guardrails = (errorContext) => {
92 | if (errorContext) {
93 | const finalErrorContext = Object.assign({
94 | header: `Error during setup`,
95 | description: `${errorContext.error}`
96 | }, errorContext);
97 | guardrailsErrors.push(finalErrorContext);
98 | }
99 |
100 | checkIfLocationIsFileURL(guardrailsErrors);
101 | displayObtrusiveErrors(guardrailsErrors);
102 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # RNBO Webpage Example
2 |
3 | This example shows you how to add dynamic audio to a webpage, using the web export feature of RNBO, part of [Max 8](https://cycling74.com/products/max) made by [Cycling '74](https://cycling74.com).
4 |
5 | This repository uses [Node](https://nodejs.org/en/) to launch a simple web server to make your webpage available locally. For more on why this is necessary, see [Why a local server?](#why-a-local-server)
6 |
7 | ## Prerequisites
8 |
9 | In order to run this example, you'll need `node`, `npm`, and access to the command line. The `npx` binary ships with `node`, so just download and install that from the [Node.js downloads site](https://nodejs.org/en/download/). The recommended version is their latest `LTS`, which at the time of writing this document is version 16.
10 |
11 | If have heard about `node` and `npm` before but would like to know more about the included `npx` Package Runner please refer to the [Node.js Documentation](https://nodejs.dev/learn/the-npx-nodejs-package-runner).
12 |
13 | ## File structure
14 |
15 | The source code of the web application is in the `js/` directory. This directory contains the file `app.js`, which does all the work of loading and connecting your RNBO patch. There is also a file `guardrails.js`, which simply tries to provide some clear feedback if you're not running this example in the intended way.
16 |
17 | Some notable files/directories:
18 |
19 | | Location | Explanation |
20 | | --------------------------------- | ------------- |
21 | | export/ | The directory into which you should export your RNBO code |
22 | | js/ | Source for the project, edit it however you like |
23 | | index.html | The web page itself |
24 |
25 | ## Using this Template
26 |
27 | This Github repo is a template, which means you can use it to start your own git-based project using this repository as a starting point. The major difference between a template and a fork is that your new project won't include the commit history of this template--it will be an entirely new starting point. For more see [the official description](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template).
28 |
29 | ### Getting Started
30 |
31 | To get started, first create a new repository to hold your project using this repository as a template. If you're viewing this repo on Github, you should see a button at the top of the page that says `Use this template`.
32 |
33 | 
34 |
35 | You can also follow [the official steps](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template) on Github for creating a new repository from a template.
36 |
37 | Now you need to copy this repository locally. Follow [the official steps](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository) to clone your repository.
38 |
39 | ### Working with RNBO
40 |
41 | Next, open the RNBO patcher you'd like to work with, and navigate to the export sidebar. Find the "Web Export" target.
42 |
43 | 
44 |
45 | Export your project, making sure to export into the `export` folder in your repository directory. Your export directory should look something like this:
46 |
47 | ```
48 | export/
49 | ├─ patch.export.json
50 | ├─ README.md
51 | ```
52 |
53 | Whenever you make a change to your RNBO patch, remember to export the source code again to update this file. Now that you've exported your RNBO code, we're ready to open the webpage. From the repository root, run the following command to start the local web server
54 |
55 | ```sh
56 | npx http-server
57 | ```
58 |
59 | When asked to install the `http-server` in order to proceed simply agree by using `y`. You will only have to agree on that once.
60 | Once the server started up successfully you may see something like the following in the console:
61 |
62 | ```sh
63 | Available on:
64 | http://127.0.0.1:8080
65 | http://192.168.88.139:8080
66 | Hit CTRL-C to stop the server
67 | ```
68 | Open the shown URL, fe. `http://127.0.0.1:8080` in your default browser, if everything went well, you should see and hear your RNBO patch.
69 |
70 | ### Exporting a new patch
71 |
72 | This example looks in the `export` directory for a patch named `patch.export.json`. If you change the name of your export to something other than `patch.export.json`, you'll need to change the JavaScript as well. In `js/app.js`, the line:
73 |
74 | ```js
75 | const patchExportURL = "export/patch.export.json";
76 | ```
77 |
78 | can be changed to reflect the name of your export.
79 |
80 | ## Troubleshooting
81 |
82 | ### Why don't I see anything?
83 |
84 | First, check your developer console. On MacOS, you can bring this up in most browsers by pressing Command-Option-I on a Mac. Firefox puts developer tools under Tools > Browser Tools > Web Developer Tools. Other browsers may put this feature somewhere else, so check the documentation for your browser of choice. The important thing to do here is to make sure you don't see any error. If you see something in red, read the message carefully.
85 |
86 | ### Something doesn't seem to be working right
87 |
88 | It might be that the version of RNBO that you used to export your patch doesn't match the version of the RNBO library that `index.html` is downloading. Look for a message in the developer console talking about mismatched versions. To fix this, either export a version of your patch using a more up-to-date version of RNBO, or else change the `script` tags in `index.html` to download a different version of the RNBO libraries.
89 |
90 | ### My samples aren't loading correctly
91 |
92 | Again check the developer console, this time looking for error messages about a failure to decode audio data. Some browsers, like Chrome for example, don't support decoding `.aif` files. So if you're using `anton.aif` as a sample dependency, you should export again using `anton.wav`. Or maybe find another sample to use.
93 |
94 | ### Why isn't my patch changing in the browser?
95 |
96 | If you changed your exported patch in the `export` folder but your patch isn't changing in the browser, you might need to hard refresh the page (cmd+shift+R). This clears the cache to account for any changes to the page being served.
97 |
98 | ## Why a local server?
99 | We're recreating on a very small scale what happens whenever you load a website on your computer. When you run `npx http-server`, a Node process starts. This process binds to a port on your machine, defaulting to port 8080. When your browser tries to access the website `http://localhost:8080`, it connects to the server and tries to get the content for the given path, which is `/`. Given this path, the server returns the contents of the file `index.html`, which is what you see when you load the page.
100 |
101 | As part of loading that page, your web browser also asks the server for the JavaScript file at `js/app.js`. When the browser executes this script, it makes yet another request to fetch the file at the path `export/patch.export.json`. Finally, the script can use this exported patch to create a RNBO JavaScript object and connect it to the audio graph in the current page.
102 |
103 | The important takeaway here is that this is the kind of interaction that your browser is expecting: making HTTP and HTTPS requests to fetch resources from a remote server. It's technically possible to simply double-click on the `index.html` file and to load the page using the `file:` protocol instead of `http:` or `https:`. However, for security reasons this will block access to `WebAssembly` or `AudioWorklets`, which will keep our exported RNBO patch from working the way we want. Running a local server lets the browser treat the webpage as if it were pulled from the internet like any other page.
104 |
105 | The other reason that we run a server this way is because this brings us much closer to putting our RNBO patch on the publically accessible internet. If you want to build a public website containing a RNBO patch, it's helpful to keep this simple example in mind when you think about what resources to put where.
106 |
107 | ## Customizing your web page
108 |
109 | From this point, the sky is the limit. You can do anything and everything to your web page, adding custom graphics and interaction in whatever way you like. A full discussion of web programming is beyond the scope of this README, but some useful reading material would include:
110 |
111 | - [ReactJS](https://reactjs.org/)
112 | - [p5JS](https://p5js.org/)
113 | - [Canvas API](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial)
114 | - [Netlify](https://www.netlify.com/)
115 |
--------------------------------------------------------------------------------
/js/app.js:
--------------------------------------------------------------------------------
1 | async function setup() {
2 | const patchExportURL = "export/patch.export.json";
3 |
4 | // Create AudioContext
5 | const WAContext = window.AudioContext || window.webkitAudioContext;
6 | const context = new WAContext();
7 |
8 | // Create gain node and connect it to audio output
9 | const outputNode = context.createGain();
10 | outputNode.connect(context.destination);
11 |
12 | // Fetch the exported patcher
13 | let response, patcher;
14 | try {
15 | response = await fetch(patchExportURL);
16 | patcher = await response.json();
17 |
18 | if (!window.RNBO) {
19 | // Load RNBO script dynamically
20 | // Note that you can skip this by knowing the RNBO version of your patch
21 | // beforehand and just include it using a