Skip to content

Commit 4fc09d1

Browse files
authored
docs: examples for various decoding (#573)
1 parent 8be7908 commit 4fc09d1

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

packages/audiodocs/docs/core/base-audio-context.mdx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,30 @@ The above method lets you create `BiquadFilterNode`.
136136

137137
#### Returns [`BiquadFilterNode`](/effects/biquad-filter-node).
138138

139+
:::caution
140+
Supported file formats:
141+
- mp3
142+
- wav
143+
- flac
144+
:::
145+
139146
### `decodeAudioData`
140147

148+
<details>
149+
<summary>Example</summary>
150+
```tsx
151+
const url = ... // url to an audio
152+
153+
const buffer = await fetch(url)
154+
.then((response) => response.arrayBuffer())
155+
.then((arrayBuffer) => this.audioContext.decodeAudioData(arrayBuffer))
156+
.catch((error) => {
157+
console.error('Error decoding audio data source:', error);
158+
return null;
159+
});
160+
```
161+
</details>
162+
141163
The above method lets you decode audio data. It decodes with in memory audio data block.
142164

143165
| Parameters | Type | Description |
@@ -148,7 +170,23 @@ The above method lets you decode audio data. It decodes with in memory audio dat
148170

149171
### `decodeAudioDataSource`
150172

151-
The above method lets you decode audio data file. It saves file in the device file system.
173+
<details>
174+
<summary>Example using expo-asset library</summary>
175+
```tsx
176+
import { Asset } from 'expo-asset';
177+
178+
const buffer = await Asset.fromModule(require('@/assets/music/example.mp3'))
179+
.downloadAsync()
180+
.then((asset) => {
181+
if (!asset.localUri) {
182+
throw new Error('Failed to load audio asset');
183+
}
184+
return this.audioContext.decodeAudioDataSource(asset.localUri);
185+
})
186+
```
187+
</details>
188+
189+
The above method lets you decode audio data file.
152190

153191
| Parameters | Type | Description |
154192
| :---: | :---: | :---- |
@@ -158,11 +196,20 @@ The above method lets you decode audio data file. It saves file in the device fi
158196

159197
### `decodePCMInBase64Data` <MobileOnly />
160198

199+
<details>
200+
<summary>Example</summary>
201+
```tsx
202+
const data = ... // data encoded in base64 string
203+
const buffer = await this.audioContext.decodePCMInBase64Data(data);
204+
```
205+
</details>
206+
161207
The above method lets you decode audio data. It decodes with PCM data in Base64.
162208

163209
| Parameters | Type | Description |
164210
| :---: | :---: | :---- |
165211
| `base64` | `string` | Base64 string with audio data. |
212+
| `playbackRate` <Optional /> | `number` | Number that represents audio speed, which will be applied during decoding. |
166213

167214
#### Returns `Promise<AudioBuffer>`.
168215

0 commit comments

Comments
 (0)