You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/synth/README.md
+52-24Lines changed: 52 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# 💨 Pfxr
2
2
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.
4
6
5
7
## Getting Started
6
8
@@ -15,12 +17,10 @@ Once installed, you can import and use the library in your JavaScript or TypeScr
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.
84
82
85
83
### Parameters
86
84
87
85
-**`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).
89
87
90
88
-**`seed?: number`**_(Optional)_
91
89
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
117
115
#### Returns
118
116
119
117
-**`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.
121
119
122
120
#### Usage
123
121
@@ -153,24 +151,52 @@ The `getUrlFromSound` function generates a URL from a given sound object, encodi
153
151
154
152
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.
155
153
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:
0 commit comments