Skip to content

Commit 9474f04

Browse files
committed
docs: examples for various decoding
1 parent fba4835 commit 9474f04

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
@@ -130,8 +130,30 @@ The above method lets you create `StereoPannerNode`.
130130

131131
#### Returns [`StereoPannerNode`](/effects/stereo-panner-node).
132132

133+
:::caution
134+
Supported file formats:
135+
- mp3
136+
- wav
137+
- flac
138+
:::
139+
133140
### `decodeAudioData`
134141

142+
<details>
143+
<summary>Example</summary>
144+
```tsx
145+
const url = ... // url to an audio
146+
147+
const buffer = await fetch(url)
148+
.then((response) => response.arrayBuffer())
149+
.then((arrayBuffer) => this.audioContext.decodeAudioData(arrayBuffer))
150+
.catch((error) => {
151+
console.error('Error decoding audio data source:', error);
152+
return null;
153+
});
154+
```
155+
</details>
156+
135157
The above method lets you decode audio data. It decodes with in memory audio data block.
136158

137159
| Parameters | Type | Description |
@@ -142,7 +164,23 @@ The above method lets you decode audio data. It decodes with in memory audio dat
142164

143165
### `decodeAudioDataSource`
144166

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

147185
| Parameters | Type | Description |
148186
| :---: | :---: | :---- |
@@ -152,11 +190,20 @@ The above method lets you decode audio data file. It saves file in the device fi
152190

153191
### `decodePCMInBase64Data` <MobileOnly />
154192

193+
<details>
194+
<summary>Example</summary>
195+
```tsx
196+
const data = ... // data encoded in base64 string
197+
const buffer = await this.audioContext.decodePCMInBase64Data(data);
198+
```
199+
</details>
200+
155201
The above method lets you decode audio data. It decodes with PCM data in Base64.
156202

157203
| Parameters | Type | Description |
158204
| :---: | :---: | :---- |
159205
| `base64` | `string` | Base64 string with audio data. |
206+
| `playbackRate` <Optional /> | `number` | Number that represents audio speed, which will be applied during decoding. |
160207

161208
#### Returns `Promise<AudioBuffer>`.
162209

0 commit comments

Comments
 (0)