Repository navigation
Expand file tree
/
Copy pathApp.tsx
More file actions
231 lines (202 loc) · 5.8 KB
/
Copy pathApp.tsx
File metadata and controls
231 lines (202 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import * as React from 'react';
import { useState, useRef, useEffect } from 'react';
import { StyleSheet, View, Text, Button, Platform } from 'react-native';
import {
useRenderer,
configureAudioSession,
loadAudioResource,
unloadAudioResource,
AudioResourceInfo,
} from 'react-native-elementary';
import RNFS from 'react-native-fs';
import { el } from '@elemaudio/core';
const SAMPLE_FILES = ['kick.wav', 'snare.wav', 'hihat.wav'] as const;
configureAudioSession({
iosCategory: 'playback',
iosMode: 'default',
iosOptions: ['mixWithOthers', 'allowBluetoothA2DP'],
});
const getSamplePath = async (filename: string): Promise<string> => {
if (Platform.OS === 'android') {
const dest = `${RNFS.DocumentDirectoryPath}/${filename}`;
const exists = await RNFS.exists(dest);
if (!exists) {
await RNFS.copyFileAssets(`samples/${filename}`, dest);
}
return dest;
}
return `${RNFS.MainBundlePath}/samples/${filename}`;
};
export default function App() {
const { core } = useRenderer();
const [loadedSamples, setLoadedSamples] = useState<
Record<string, AudioResourceInfo>
>({});
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const triggerRef = useRef<Record<string, number>>({
kick: 0,
snare: 0,
hihat: 0,
});
const lastPlayTime = useRef<Record<string, number>>({});
const loadSamples = async () => {
setLoading(true);
setError(null);
try {
const loaded: Record<string, AudioResourceInfo> = {};
for (const file of SAMPLE_FILES) {
const key = file.replace('.wav', '');
const path = await getSamplePath(file);
const info = await loadAudioResource(key, path);
loaded[key] = info;
}
setLoadedSamples(loaded);
} catch (e) {
console.error('Failed to load samples:', e);
setError(e instanceof Error ? e.message : String(e));
} finally {
setLoading(false);
}
};
useEffect(() => {
return () => {
Object.keys(loadedSamples).forEach((key) => {
unloadAudioResource(key).catch(console.error);
});
};
}, [loadedSamples]);
const playSample = (key: string) => {
if (!loadedSamples[key]) return;
const now = Date.now();
const lastTime = lastPlayTime.current[key] || 0;
if (now - lastTime < 100) return;
lastPlayTime.current[key] = now;
triggerRef.current[key] = (triggerRef.current[key] || 0) + 1;
const sample = el.sample(
{ path: key, mode: 'trigger', key: `${key}-sample` },
el.const({ key: `${key}-trig`, value: triggerRef.current[key] }),
1
);
core.render(sample, sample);
};
const playBeat = () => {
if (Object.keys(loadedSamples).length === 0) return;
const bpm = 126;
const beatFreq = bpm / 60;
const kick = el.mul(
el.sample({ path: 'kick', mode: 'trigger' }, el.train(beatFreq), 1),
0.9
);
const snare = el.mul(
el.sample({ path: 'snare', mode: 'trigger' }, el.train(beatFreq / 2), 1),
0.6
);
const hihat = el.mul(
el.sample({ path: 'hihat', mode: 'trigger' }, el.train(beatFreq * 2), 1),
0.35
);
const mix = el.add(kick, el.add(snare, hihat));
core.render(mix, mix);
};
const stop = () => {
core.render();
};
return (
<View style={styles.container}>
<Text style={styles.title}>VFS Audio Demo</Text>
{error && <Text style={styles.error}>Error: {error}</Text>}
<View style={styles.section}>
<Text style={styles.sectionTitle}>1. Load Samples</Text>
<Button
title={loading ? 'Loading...' : 'Load Samples'}
onPress={loadSamples}
disabled={loading}
/>
{Object.entries(loadedSamples).map(([key, info]) => (
<Text key={key} style={styles.sampleInfo}>
{key}: {info.channels}ch, {info.sampleRate}Hz,{' '}
{info.durationMs.toFixed(0)}ms
</Text>
))}
</View>
<View style={styles.section}>
<Text style={styles.sectionTitle}>2. Play Individual Samples</Text>
<View style={styles.buttonRow}>
<Button
title="Kick"
onPress={() => playSample('kick')}
disabled={!loadedSamples.kick}
/>
<Button
title="Snare"
onPress={() => playSample('snare')}
disabled={!loadedSamples.snare}
/>
<Button
title="HiHat"
onPress={() => playSample('hihat')}
disabled={!loadedSamples.hihat}
/>
</View>
</View>
<View style={styles.section}>
<Text style={styles.sectionTitle}>3. Play Beat</Text>
<Button
title="Play 4-on-the-floor (126 BPM)"
onPress={playBeat}
disabled={Object.keys(loadedSamples).length === 0}
/>
</View>
<View style={styles.section}>
<Button title="Stop" onPress={stop} />
</View>
<View style={styles.section}>
<Text style={styles.sectionTitle}>Legacy Test</Text>
<Button
title="Play 440Hz Tone"
onPress={() => core.render(el.cycle(440), el.cycle(440))}
/>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
padding: 20,
backgroundColor: '#1a1a2e',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 20,
color: '#eee',
},
section: {
marginVertical: 10,
alignItems: 'center',
width: '100%',
},
sectionTitle: {
fontSize: 16,
fontWeight: '600',
marginBottom: 8,
color: '#aaa',
},
buttonRow: {
flexDirection: 'row',
gap: 10,
},
sampleInfo: {
fontSize: 12,
color: '#6f6',
marginTop: 4,
},
error: {
color: '#f66',
marginBottom: 10,
},
});