Skip to content

Commit d664791

Browse files
committed
Update README.md
1 parent 28a1894 commit d664791

File tree

1 file changed

+52
-24
lines changed

1 file changed

+52
-24
lines changed

packages/synth/README.md

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# 💨 Pfxr
22

3-
Pfxr is a lightweight JavaScript library inspired by DrPetter's iconic **sfxr** sound generator. It allows developers to easily create retro-style sound effects for games and applications directly in the browser using the powerful WebAudio API.
3+
Pfxr is a lightweight JavaScript library inspired by DrPetter's iconic **sfxr** sound generator. It allows developers to easily create retro-style sound effects for games and applications directly in the browser using the WebAudio API.
4+
5+
You can also try out and experiment with sound effects using the **[Pfxr UI](https://achtaitaipai.github.io/pfxr/)**, which provides an intuitive interface for generating and tweaking sounds, and even sharing them via URL.
46

57
## Getting Started
68

@@ -15,12 +17,10 @@ Once installed, you can import and use the library in your JavaScript or TypeScr
1517
```typescript
1618
import { playSound, getSoundFromTemplate, TEMPLATES } from 'pfxr'
1719

18-
const audioContext = new (window.AudioContext || window.webkitAudioContext)()
20+
const audioContext = new AudioContext()
1921
const sound = getSoundFromTemplate(TEMPLATES.laser)
2022

21-
playSound(sound, audioContext, audioContext.destination).then(() => {
22-
console.log('Sound played!')
23-
})
23+
playSound(sound, audioContext, audioContext.destination)
2424
```
2525

2626
### Using Pfxr via CDN
@@ -36,9 +36,7 @@ If you prefer to use **Pfxr** directly in the browser without installing via npm
3636
const audioContext = new AudioContext()
3737
const sound = getSoundFromTemplate(TEMPLATES.laser)
3838
39-
playSound(sound, audioContext, audioContext.destination).then(() => {
40-
console.log('Sound played!')
41-
})
39+
playSound(sound, audioContext, audioContext.destination)
4240
</script>
4341
```
4442

@@ -80,12 +78,12 @@ playSound(soundSettings, audioContext, audioContext.destination)
8078

8179
## `getSoundFromTemplate`
8280

83-
The `getSoundFromTemplate` function generates a complete sound object based on a predefined sound template and an optional random seed. The function uses a template to structure the sound settings and introduces randomness for variety or uniqueness in the generated sound.
81+
The `getSoundFromTemplate` function generates a complete [sound](#sound) object based on a predefined sound template and an optional random seed. The function uses a template to structure the sound settings and introduces randomness for variety or uniqueness in the generated sound.
8482

8583
### Parameters
8684

8785
- **`template: SoundTemplate`**
88-
A function representing a sound template. The template function defines the structure of the sound by specifying key sound parameters like waveform, pitch, envelope, and effects. You can choose from predefined templates (e.g., "laser," "explosion," etc.) or create your own.
86+
A function representing a sound template. The template function defines the structure of the sound by specifying key sound parameters like waveform, pitch, envelope, and effects. You can choose from predefined templates (e.g., "default", "pickup", "laser", "jump", "fall", "powerup", "explosion", "blip", "hit", "fart", "random") or [create your own](#custom-template).
8987

9088
- **`seed?: number`** _(Optional)_
9189
A random seed value to control the random generation of the sound properties. This allows for reproducibility, meaning if you pass the same seed and template, the sound will always be generated the same way. If no seed is provided, the random generator will create a new, unpredictable sound.
@@ -117,7 +115,7 @@ The `getSoundFromUrl` function extracts sound parameters from a provided URL and
117115
#### Returns
118116

119117
- **`Sound`**
120-
Returns a fully formed `Sound` object that includes all the necessary parameters (e.g., waveform, frequency, attack time, etc.) derived from the URL. The resulting sound object can be directly used in functions like `playSound` for playback or further customization.
118+
Returns a fully formed [sound](#sound) object that includes all the necessary parameters (e.g., waveform, frequency, attack time, etc.) derived from the URL. The resulting sound object can be directly used in functions like `playSound` for playback or further customization.
121119

122120
#### Usage
123121

@@ -153,24 +151,52 @@ The `getUrlFromSound` function generates a URL from a given sound object, encodi
153151

154152
In addition to the predefined templates, you can create your own custom sound templates to fit specific needs or generate unique sound effects. A custom template works by specifying the sound parameters you want to randomize or control, such as waveform, pitch, envelope, and effects.
155153

156-
Here's an example of a custom template:
154+
The custom templates use a `rand` object, which provides various methods for generating random values. These methods help add variation and randomness to sound properties, making the sounds more dynamic and unpredictable.
155+
156+
### `rand` Methods:
157+
158+
- **`number(min?: number, max?: number)`**
159+
Generates a random floating-point number between `min` and `max`. If no arguments are provided, it returns a number between 0 and 1.
160+
Example:
161+
162+
```typescript
163+
rand.number(200, 800) // Returns a number between 200 and 800
164+
```
165+
166+
- **`boolean(trueProbability?: number)`**
167+
Generates a random boolean (`true` or `false`). You can specify the probability of `true` with `trueProbability` (default is 0.5).
168+
Example:
169+
170+
```typescript
171+
rand.boolean(0.7) // 70% chance of returning true
172+
```
173+
174+
- **`fromArray<T>(array: T[])`**
175+
Selects a random element from an array.
176+
Example:
177+
```typescript
178+
rand.fromArray([1, 2, 3, 4]) // Randomly selects one of the array elements
179+
```
180+
181+
Here's an example of a custom template that uses these methods to randomize the sound parameters:
157182

158183
### Example: Custom Template
159184

160185
```typescript
161-
import { playSound, getSoundFromTemplate, type SoundTemplate } from 'pfxr'
186+
import { playSound, createSoundFromTemplate, type SoundTemplate } from 'pfxr'
187+
162188
const customTemplate: SoundTemplate = (rand) => ({
163-
waveForm: 1,
164-
frequency: rand.number(200, 800),
165-
sustainTime: rand.number(0.2, 0.5),
166-
decayTime: rand.number(0.3, 0.6),
167-
pitchDelta: -rand.number(200, 500),
168-
pitchDuration: 1,
169-
pitchDelay: 0,
170-
vibratoRate: rand.number(0, 10),
171-
vibratoDepth: rand.number(0, 5),
172-
lowPassCutoff: rand.number(1000, 3000),
173-
lowPassResonance: rand.number(0, 10),
189+
waveForm: rand.fromArray([0, 1, 2, 3]), // Randomly selects a waveform type
190+
frequency: rand.number(200, 800), // Random frequency between 200 and 800 Hz
191+
sustainTime: rand.number(0.2, 0.5), // Random sustain time between 0.2 and 0.5 seconds
192+
decayTime: rand.number(0.3, 0.6), // Random decay time between 0.3 and 0.6 seconds
193+
pitchDelta: -rand.number(200, 500), // Falling pitch over time
194+
pitchDuration: 1, // Static pitch duration
195+
pitchDelay: 0, // No pitch delay
196+
vibratoRate: rand.number(0, 10), // Random vibrato rate
197+
vibratoDepth: rand.number(0, 5), // Random vibrato depth
198+
lowPassCutoff: rand.number(1000, 3000), // Random low-pass filter cutoff
199+
lowPassResonance: rand.number(0, 10), // Random low-pass filter resonance
174200
})
175201

176202
const sound = createSoundFromTemplate(customTemplate)
@@ -201,9 +227,11 @@ Here is a list of all the parameters included in the `Sound` type :
201227
### Envelope
202228

203229
2. **attackTime** (`range`)
230+
204231
- Time for the sound to reach its peak after being triggered.
205232
- Range: `0` to `2` (seconds)
206233
- Default: `0`
234+
207235
3. **sustainTime** (`range`)
208236

209237
- Duration the sound sustains at peak level after the attack phase.

0 commit comments

Comments
 (0)