cacophony 0.12.4 → 0.12.6

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
@@ -1,18 +1,20 @@
1
1
 
2
- ## Cacophony: Advanced Browser Audio Library
2
+ # Cacophony: Advanced Browser Audio Library
3
3
 
4
- Cacophony is an intuitive and powerful audio library designed for the modern web. It's built to simplify audio management within browser-based applications, offering a straightforward interface to the Web Audio API. Cacophony is ideal for projects that require detailed audio control, from simple sound playback to complex audio processing and 3D audio positioning.
4
+ Cacophony is a powerful and intuitive audio library designed for modern web applications. It provides a high-level interface to the Web Audio API, simplifying complex audio operations while offering fine-grained control. Cacophony is perfect for projects ranging from simple sound playback to sophisticated audio processing and 3D audio positioning.
5
5
 
6
6
  ## Key Features
7
7
 
8
- - **Rich Audio Source Management**: Handle audio sources from `AudioBuffer`, URL strings, synthesizers, or user's microphone with ease.
9
- - **Detailed Audio Control**: Comprehensive control over audio playback including play, stop, pause, resume, and loop.
10
- - **3D Audio Positioning**: Create immersive audio experiences by positioning sounds in a three-dimensional space.
11
- - **Advanced Audio Effects**: Apply and manage a variety of audio filters for enhanced sound quality.
12
- - **Dynamic Volume and Muting**: Global and individual volume control, complete with smooth fade-in and fade-out effects.
13
- - **Live Audio Input**: Capture and process live audio input from the user's microphone.
14
- - **Synthesizer Functionality**: Create and manipulate synthesized sounds with customizable oscillator options.
15
- - **Group Management**: Organize and control multiple sounds or synthesizers as groups for efficient management.
8
+ - **Versatile Audio Source Handling**: Manage audio from various sources including `AudioBuffer`, URL strings, synthesizers, and live microphone input.
9
+ - **Comprehensive Playback Control**: Play, stop, pause, resume, loop, and seek within audio with ease.
10
+ - **3D Audio Positioning**: Create immersive soundscapes with precise spatial audio control.
11
+ - **Advanced Audio Processing**: Apply and manage a variety of audio filters for enhanced sound manipulation.
12
+ - **Dynamic Volume Control**: Adjust global and individual volume levels with support for smooth fading effects.
13
+ - **Synthesizer Integration**: Create and manipulate synthesized sounds with customizable oscillator options.
14
+ - **Efficient Group Management**: Organize and control multiple sounds or synthesizers as groups for streamlined audio management.
15
+ - **Live Microphone Input**: Capture and process real-time audio input from the user's microphone.
16
+ - **Audio Streaming**: Support for streaming audio directly from URLs.
17
+ - **Flexible Caching**: Implement efficient audio caching strategies for improved performance.
16
18
 
17
19
  ## Installation
18
20
 
@@ -25,96 +27,260 @@ npm install cacophony
25
27
  ```typescript
26
28
  import { Cacophony } from 'cacophony';
27
29
 
28
- async function playSampleSound() {
30
+ async function audioDemo() {
29
31
  const cacophony = new Cacophony();
30
32
 
31
- // Create and play a sound
33
+ // Create and play a sound with 3D positioning
32
34
  const sound = await cacophony.createSound('path/to/audio.mp3');
33
35
  sound.play();
34
- sound.position = [1, 1, 1]; // Set sound position in 3D space
36
+ sound.position = [1, 0, -1]; // Set sound position in 3D space
35
37
 
36
38
  // Create and play a synthesizer
37
39
  const synth = cacophony.createOscillator({ frequency: 440, type: 'sine' });
38
40
  synth.play();
39
41
 
42
+ // Apply a filter to the synth
43
+ const filter = cacophony.createBiquadFilter({ type: 'lowpass', frequency: 1000 });
44
+ synth.addFilter(filter);
45
+
40
46
  // Create a group of sounds
41
47
  const group = await cacophony.createGroupFromUrls(['sound1.mp3', 'sound2.mp3']);
42
48
  group.play(); // Play all sounds in the group
49
+
50
+ // Capture microphone input
51
+ const micStream = await cacophony.getMicrophoneStream();
52
+ micStream.play();
43
53
  }
44
54
 
45
- playSampleSound();
55
+ audioDemo();
46
56
  ```
47
57
 
48
58
  ## Detailed API Documentation
49
59
 
50
- For a complete overview of all functionality, classes, and methods, please refer to our [detailed documentation](https://cacophony.js.org).
60
+ For a comprehensive overview of all classes, methods, and features, please refer to our [detailed documentation](https://cacophony.js.org).
51
61
 
52
- ## Synthesizer Functionality
62
+ ## Audio Filters
53
63
 
54
- Cacophony provides powerful synthesizer capabilities through the `Synth` class:
64
+ Cacophony provides powerful audio filtering capabilities:
55
65
 
56
66
  ```typescript
57
67
  const cacophony = new Cacophony();
58
- const synth = cacophony.createOscillator({ frequency: 440, type: 'sine' });
68
+
69
+ // Create a lowpass filter
70
+ const lowpassFilter = cacophony.createBiquadFilter({
71
+ type: 'lowpass',
72
+ frequency: 1000,
73
+ Q: 1
74
+ });
75
+
76
+ // Apply filter to a Sound
77
+ const sound = await cacophony.createSound('path/to/audio.mp3');
78
+ sound.addFilter(lowpassFilter);
79
+ sound.play();
80
+
81
+ // Apply filter to a Synth
82
+ const synth = cacophony.createOscillator({ frequency: 440, type: 'sawtooth' });
83
+ synth.addFilter(lowpassFilter);
59
84
  synth.play();
60
- synth.frequency = 880; // Change frequency
61
- synth.type = 'square'; // Change waveform
85
+ ```
86
+
87
+ ## Synthesizer Functionality
88
+
89
+ Create and manipulate synthesized sounds with advanced control:
90
+
91
+ ```typescript
92
+ const cacophony = new Cacophony();
93
+
94
+ // Create a simple sine wave oscillator
95
+ const sineOsc = cacophony.createOscillator({ frequency: 440, type: 'sine' });
96
+ sineOsc.play();
97
+
98
+ // Create a complex sound with multiple oscillators
99
+ const complexSynth = cacophony.createOscillator({ frequency: 220, type: 'sawtooth' });
100
+ const subOsc = cacophony.createOscillator({ frequency: 110, type: 'sine' });
101
+ complexSynth.addFilter(cacophony.createBiquadFilter({ type: 'lowpass', frequency: 1000 }));
102
+ complexSynth.play();
103
+ subOsc.play();
104
+
105
+ // Modulate frequency over time
106
+ let time = 0;
107
+ setInterval(() => {
108
+ const frequency = 440 + Math.sin(time) * 100;
109
+ sineOsc.frequency = frequency;
110
+ time += 0.1;
111
+ }, 50);
112
+
113
+ // Apply envelope to volume
114
+ complexSynth.volume = 0;
115
+ complexSynth.fadeIn(0.5, 'linear');
116
+ setTimeout(() => complexSynth.fadeOut(1, 'exponential'), 2000);
62
117
  ```
63
118
 
64
119
  ## Group Functionality
65
120
 
66
- Organize and control multiple sounds or synthesizers with the `Group` and `SynthGroup` classes:
121
+ Efficiently manage and control multiple sounds or synthesizers:
67
122
 
68
123
  ```typescript
69
- const soundGroup = await cacophony.createGroupFromUrls(['sound1.mp3', 'sound2.mp3']);
70
- soundGroup.play(); // Play all sounds in the group
124
+ const cacophony = new Cacophony();
125
+
126
+ // Create a group of sounds
127
+ const soundGroup = await cacophony.createGroupFromUrls(['drum.mp3', 'bass.mp3', 'synth.mp3']);
71
128
 
129
+ // Play all sounds in the group
130
+ soundGroup.play();
131
+
132
+ // Control volume for all sounds
133
+ soundGroup.volume = 0.7;
134
+
135
+ // Apply 3D positioning to the entire group
136
+ soundGroup.position = [1, 0, -1];
137
+
138
+ // Create a group of synthesizers
72
139
  const synthGroup = new SynthGroup();
140
+ const synth1 = cacophony.createOscillator({ frequency: 440, type: 'sine' });
141
+ const synth2 = cacophony.createOscillator({ frequency: 660, type: 'square' });
73
142
  synthGroup.addSynth(synth1);
74
143
  synthGroup.addSynth(synth2);
75
- synthGroup.play(); // Play all synths in the group
144
+
145
+ // Play and control all synths in the group
146
+ synthGroup.play();
147
+ synthGroup.setVolume(0.5);
148
+ synthGroup.stereoPan = -0.3; // Pan slightly to the left
149
+
150
+ // Remove a synth from the group
151
+ synthGroup.removeSynth(synth2);
76
152
  ```
77
153
 
78
- ## Additional Highlights
154
+ ## Microphone Input
155
+
156
+ Capture, process, and manipulate live audio input:
79
157
 
80
- - **Streaming Audio**: Stream live audio directly from a URL.
81
- - **Volume Transitions**: Employ `fadeIn` and `fadeOut` for nuanced volume control.
82
- - **Flexible Synthesizer Options**: Create complex sounds with customizable oscillator settings.
83
- - **Efficient Group Management**: Control multiple audio sources simultaneously for complex audio scenarios.
84
- - **Audio Filters**: Apply various audio filters, such as lowpass, highpass, and bandpass, to Sounds or Synths and their playbacks.
158
+ ```typescript
159
+ const cacophony = new Cacophony();
160
+
161
+ async function setupMicrophone() {
162
+ try {
163
+ const micStream = await cacophony.getMicrophoneStream();
164
+ micStream.play();
165
+
166
+ // Apply a low-pass filter to the microphone input
167
+ const lowPassFilter = cacophony.createBiquadFilter({ type: 'lowpass', frequency: 1000 });
168
+ micStream.addFilter(lowPassFilter);
169
+
170
+ // Add a delay effect
171
+ const delayFilter = cacophony.createBiquadFilter({ type: 'delay', delayTime: 0.5 });
172
+ micStream.addFilter(delayFilter);
173
+
174
+ // Control microphone volume
175
+ micStream.volume = 0.8;
176
+
177
+ // Pause and resume microphone input
178
+ setTimeout(() => {
179
+ micStream.pause();
180
+ console.log("Microphone paused");
181
+ setTimeout(() => {
182
+ micStream.resume();
183
+ console.log("Microphone resumed");
184
+ }, 2000);
185
+ }, 5000);
186
+
187
+ } catch (error) {
188
+ console.error("Error accessing microphone:", error);
189
+ }
190
+ }
191
+
192
+ setupMicrophone();
193
+ ```
85
194
 
86
- ## Using Audio Filters
195
+ ## 3D Audio Positioning
87
196
 
88
- Cacophony supports applying audio filters to both Sounds and Synths. Here's an example of using a lowpass filter:
197
+ Create immersive soundscapes with precise spatial audio control:
89
198
 
90
199
  ```typescript
91
200
  const cacophony = new Cacophony();
92
201
 
93
- // Create a lowpass filter
94
- const lowpassFilter = cacophony.createBiquadFilter({
95
- type: 'lowpass',
96
- frequency: 1000,
97
- Q: 1
98
- });
202
+ async function create3DAudioScene() {
203
+ // Create sounds with HRTF panning
204
+ const ambience = await cacophony.createSound('forest_ambience.mp3', SoundType.Buffer, 'HRTF');
205
+ const birdSound = await cacophony.createSound('bird_chirp.mp3', SoundType.Buffer, 'HRTF');
206
+ const footsteps = await cacophony.createSound('footsteps.mp3', SoundType.Buffer, 'HRTF');
207
+
208
+ // Position sounds in 3D space
209
+ ambience.position = [0, 0, -5]; // Slightly behind the listener
210
+ birdSound.position = [10, 5, 0]; // To the right and above
211
+ footsteps.position = [-2, -1, 2]; // Slightly to the left and in front
212
+
213
+ // Play the sounds
214
+ ambience.play();
215
+ birdSound.play();
216
+ footsteps.play();
217
+
218
+ // Update listener position and orientation
219
+ cacophony.listenerPosition = [0, 0, 0];
220
+ cacophony.listenerOrientation = {
221
+ forward: [0, 0, -1],
222
+ up: [0, 1, 0]
223
+ };
224
+
225
+ // Animate bird sound position
226
+ let time = 0;
227
+ setInterval(() => {
228
+ const x = Math.sin(time) * 10;
229
+ birdSound.position = [x, 5, 0];
230
+ time += 0.05;
231
+ }, 50);
232
+
233
+ // Change listener position over time
234
+ setTimeout(() => {
235
+ cacophony.listenerPosition = [0, 0, 5];
236
+ console.log("Listener moved forward");
237
+ }, 5000);
238
+ }
99
239
 
100
- // Apply filter to a Sound
101
- const sound = await cacophony.createSound('path/to/audio.mp3');
102
- sound.addFilter(lowpassFilter);
103
- sound.play();
240
+ create3DAudioScene();
241
+ ```
104
242
 
105
- // Apply filter to a Synth
106
- const synth = cacophony.createOscillator({ frequency: 440, type: 'sawtooth' });
107
- synth.addFilter(lowpassFilter);
108
- synth.play();
243
+ ## Audio Streaming
109
244
 
110
- // Filters affect all playbacks of the Sound or Synth
111
- const playback1 = sound.play()[0];
112
- const playback2 = sound.play()[0];
113
- // Both playback1 and playback2 will have the lowpass filter applied
245
+ Stream audio content efficiently:
246
+
247
+ ```typescript
248
+ const cacophony = new Cacophony();
249
+
250
+ async function streamAudio() {
251
+ try {
252
+ const streamedSound = await cacophony.createStream('https://example.com/live_radio_stream');
253
+ streamedSound.play();
254
+
255
+ // Apply real-time effects to the stream
256
+ const highPassFilter = cacophony.createBiquadFilter({ type: 'highpass', frequency: 500 });
257
+ streamedSound.addFilter(highPassFilter);
258
+
259
+ // Control streaming playback
260
+ setTimeout(() => {
261
+ streamedSound.pause();
262
+ console.log("Stream paused");
263
+ setTimeout(() => {
264
+ streamedSound.play();
265
+ console.log("Stream resumed");
266
+ }, 5000);
267
+ }, 10000);
268
+
269
+ } catch (error) {
270
+ console.error("Error streaming audio:", error);
271
+ }
272
+ }
273
+
274
+ streamAudio();
114
275
  ```
115
276
 
116
- This example demonstrates how to create and apply a lowpass filter to both a Sound and a Synth. The filter affects all playbacks of the Sound or Synth, allowing for consistent audio processing across multiple instances.
277
+ ## Additional Highlights
278
+
279
+ - **Efficient Caching**: Implement smart caching strategies for improved performance and reduced bandwidth usage.
280
+ - **Flexible Sound Types**: Support for various sound types including buffered audio, HTML5 audio, and streaming.
281
+ - **Advanced Looping Control**: Fine-grained control over audio looping, including infinite loops and specific loop counts.
282
+ - **Detailed Playback Information**: Access and control various playback parameters such as current time, duration, and playback rate.
117
283
 
118
284
  ## License
119
285
 
120
- Cacophony is open-source software licensed under the [MIT License](LICENSE.txt)
286
+ Cacophony is open-source software licensed under the [MIT License](LICENSE.txt).