mercury-engine 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -23,7 +23,7 @@ Mercury currently has 2 versions:
23
23
 
24
24
  [**💬 Join the Mercury Community on Discord**](https://discord.gg/vt59NYU)
25
25
 
26
- ![The Mercury playground in the browser](media/screenshot.png)
26
+ <!-- ![The Mercury playground in the browser](media/screenshot.png) -->
27
27
 
28
28
  # 🚀 Install
29
29
 
@@ -41,13 +41,23 @@ const Engine = new Mercury();
41
41
 
42
42
  ## Include in html
43
43
 
44
- Include latest or specific version of bundled es5 version through url in index.html
44
+ Include latest or a specific version of distribution (minified, es5) through url in script index.html
45
+
46
+ Recommended for most cases:
45
47
 
46
48
  ```html
47
- <script src="https://unpkg.com/mercury-engine@1.0.0/build/mercury.min.js"></script>
49
+ <script src="https://unpkg.com/mercury-engine/dist/mercury.min.es5.js"></script>
48
50
  ```
49
51
 
50
- Use in a html `<script>` like so:
52
+ Other options:
53
+
54
+ ```html
55
+ <script src="https://unpkg.com/mercury-engine/dist/mercury.js"></script>
56
+ <script src="https://unpkg.com/mercury-engine@1.0.0/dist/mercury.min.js"></script>
57
+ <script src="https://unpkg.com/mercury-engine@1.0.0/dist/mercury.min.es5.js"></script>
58
+ ```
59
+
60
+ Load the engine in the `<script>` code like so:
51
61
 
52
62
  ```js
53
63
  const { Mercury } = MercuryEngine;
@@ -55,61 +65,216 @@ const { Mercury } = MercuryEngine;
55
65
  const Engine = new Mercury();
56
66
  ```
57
67
 
68
+ ## Examples
69
+
70
+ See the `examples` folder for a few `.html` files that demonstrate the usage. Run them locally by cloning this repository with `git clone https://github.com/tmhglnd/mercury-engine`. Then `cd mercury-engine` and run a simple http-server, for example with the `http-server` node package (install globally with `npm i -g http-server`). Navigate to `localhost:8080/examples` and try them out.
71
+
58
72
  # Usage
59
73
 
74
+ ### Include and initialize
75
+
76
+ Include the package
77
+
60
78
  ```js
61
- // include the package
62
79
  const { Mercury } = require('mercury-engine');
80
+ ```
63
81
 
64
- // initialize the engine and included a callback function
65
- const Engine = Mercury(() => {
66
- console.log('This callback is called when samples are loaded!');
82
+ Initialize the engine and include a callback function through { onload: }, this will be executed when samples are loaded.
83
+
84
+ ```js
85
+ const Engine = Mercury({
86
+ onload: () => {
87
+ console.log('This callback is called when samples are loaded!');
88
+ console.log('The loaded samples:', Engine.getBuffers());
89
+ }
67
90
  });
91
+ ```
68
92
 
69
- // resume the transport and start the webaudio
70
- // this has to be done from a user interaction (click/key) to allow sound
71
- // to play from the browser window
72
- Engine.resume()
93
+ ### Resume, evaluate and silence
73
94
 
74
- // Evaluate a mercury code file by providing a string of code
75
- // This also resumes the transport if .resume() was not called yet
95
+ Resume the transport and start the webaudio. This has to be done from a user interaction (click or keypress) to allow sound to play from the browser window.
96
+
97
+ ```js
98
+ Engine.resume();
99
+ ```
100
+
101
+ Evaluate a mercury code file by providing a string of code. This also resumes the transport if .resume() was not called yet.
102
+
103
+ ```js
76
104
  Engine.code(`
77
105
  set tempo 100
78
106
  new sample kick_909 time(1/4)
79
107
  new sample hat_909 time(1/4 1/8) gain(0.6)
80
108
  new synth saw note(0 0) time(1/16) shape(1 80)
81
109
  `);
110
+ ```
82
111
 
83
- // stop the transport and silence the audio
112
+ Stop the transport and silence the audio
113
+
114
+ ```js
84
115
  Engine.silence();
116
+ ```
117
+
118
+ Return the last succesfully evaluated code. If code is evaluated that resulted in an error it is not stored.
119
+
120
+ ```js
121
+ Engine.getCode();
122
+ ```
123
+
124
+ ### Samples
125
+
126
+ Add your own samples from for example a url like raw github or freesound. The url can also contain a .json file that references multiple samples and the sample name.
127
+
128
+ ```js
129
+ Engine.addBuffers(files, callback);
130
+ ```
131
+
132
+ For example use a json file containing sample names and urls
133
+
134
+ ```js
135
+ Engine.addBuffers('https://raw.githubusercontent.com/tmhglnd/mercury-engine/main/examples/samples/freesound-samples.json');
136
+ ```
137
+
138
+ Or load samples directly by creating an array of urls
139
+
140
+ ```js
141
+ let s1 = 'https://cdn.freesound.org/previews/671/671221_3797507-lq.mp3';
142
+ let s2 = 'https://cdn.freesound.org/previews/145/145778_2101444-lq.mp3';
143
+
144
+ Engine.addBuffers([s1, s2]);
145
+ ```
146
+
147
+ Add a callback function, this is called when all samples are loaded
148
+
149
+ ```js
150
+ Engine.addBuffers('https://someurl.json', () => {
151
+ console.log('This callback is called when samples are loaded!');
152
+ console.log('The loaded samples:', Engine.getBuffers());
153
+ });
154
+ ```
155
+
156
+ Get the content of all the loaded Buffers, this is returned as a `ToneAudioBuffers` class
157
+
158
+ ```js
159
+ Engine.getBuffers();
160
+ ```
161
+
162
+ ### Recording
85
163
 
86
- // start the recording of the sound
164
+ Start the recording of the sound
165
+
166
+ ```js
87
167
  Engine.record(true);
168
+ ```
169
+
170
+ Returns 'started' if the recording is on
171
+
172
+ ```js
173
+ Engine.isRecording();
174
+ ```
88
175
 
89
- // returns 'started' if the recording is on
90
- Engine.isRecording()
176
+ Stop the recording and download the file `myRecording.webm`
91
177
 
92
- // stop the recording and download the file myRecording.webm
178
+ ```js
93
179
  Engine.record(false, 'myRecording');
180
+ ```
181
+
182
+ ### MIDI
183
+
184
+ WebMIDI is included and started if the browser is compatible with it. If not, an error will be printed to the console. You can provide a callback function `onmidi` to execute some code when the WebMIDI enabling was succesful.
94
185
 
95
- // set the BPM without adding `set tempo` to the Mercury code
186
+ ```js
187
+ const Engine = Mercury({
188
+ onmidi: () => {
189
+ console.log('The WebMIDI status is:', Engine.midi.status);
190
+ console.log('With inputs:', Engine.midi.inputs);
191
+ console.log('And outputs:', Engine.midi.outputs);
192
+ }
193
+ });
194
+ ```
195
+
196
+ ### Settings
197
+
198
+ Set the BPM without using `set tempo` in the Mercury code
199
+
200
+ ```js
96
201
  Engine.setBPM(140);
202
+ ```
203
+
204
+ Set a randomized BPM
97
205
 
98
- // set the volume without adding `set volume` to the Mercury code
206
+ ```js
207
+ Engine.randomBPM();
208
+ ```
209
+
210
+ Set the volume without using `set volume` in the Mercury code. Volume in floating amplitude 0 to 1.
211
+
212
+ ```js
99
213
  Engine.setVolume(0.5);
214
+ ```
215
+
216
+ Set the crossFade between 2 code evaluations without using `set crossFade` in the Mercury code. Time in milliseconds.
217
+
218
+ ```js
219
+ Engine.setCrossFade(500);
220
+ ```
221
+
222
+ Set the HighPass Filter cutoff frequency without using `set highPass` in the Mercury code.
223
+
224
+ ```js
225
+ Engine.setHighPass(400);
226
+ ```
227
+
228
+ Set the LowPass Filter cutoff frequency without using `set lowPass` in the Mercury code.
229
+
230
+ ```js
231
+ Engine.setLowPass(5000);
232
+ ```
233
+
234
+ Get the value for any of the settings
235
+
236
+ ```js
237
+ console.log(Engine.bpm);
238
+ console.log(Engine.volume);
239
+ console.log(Engine.crossFade);
240
+ console.log(Engine.highPass);
241
+ console.log(Engine.lowPass);
242
+ ```
243
+
244
+ ### Log function listener
245
+
246
+ Logs that are important for the Mercury coder are also emitted as a custom event in the browser. You can create an event listener for `mercuryLog`. The `e.detail` contains the printed message. This can be used to for example print things to custom html elements instead of the javascript console.
247
+
248
+ ```js
249
+ window.addEventListener('mercuryLog', (e) => {
250
+ let p = JSON.stringify(e.detail).replace(/\,/g, ' ').replace(/\"/g, '');
251
+ document.getElementById('logger').innerHTML += `${p}<br>`;
252
+ console.log('customprint:', e.detail);
253
+ });
254
+ ```
255
+
256
+ ### DOM elements from P5js
257
+
258
+ It is possible to control parameters from instruments in the Mercury code by writing a string that contains `{}` with the js code inside to get the dom value. For example when using DOM elements from the P5js library, such as sliders, they can be used in the code.
259
+
260
+ ```js
261
+ let slider;
262
+
263
+ // p5js setup function
264
+ function setup() {
265
+ noCanvas();
266
+ // create a slider with range 50 - 5000, initial 1000
267
+ slider = createSlider(50, 5000, 1000, 0);
268
+ }
100
269
 
101
- // add your own samples from for example a url like raw github or freesound
102
- // the url can also contain a .json file that references multiple samples and
103
- // the sample name
104
- Engine.addSamples();
270
+ // use the slider value as a cutoff frequency for the filter
271
+ Engine.code(`new synth saw note(0 0) fx(filter low '{slider.value()}' 0.4)`);
105
272
  ```
106
273
 
107
274
  ## 📋 To Do
108
275
 
109
276
  - [ ] Include OSC communcation options via socket.io
110
277
  - [ ] Use engine in the Mercury-playground instead of the other code-base
111
- - [ ] Create examples for the engine
112
- - [ ] Allow control of parameters via DOM elements
113
278
 
114
279
  ## 🔋 Powered By
115
280