@@ -49,93 +49,9 @@ const amplitudeData = computeAmplitude('/path/to/audio.mp3', 1000);
4949console .log (' Amplitude data:' , amplitudeData );
5050```
5151
52- ### Creating Audio Waveforms
52+ ### Example
5353
54- ``` typescript
55- import React , { useCallback , useEffect , useState } from ' react' ;
56- import { View , StyleSheet , ScrollView } from ' react-native' ;
57- import { computeAmplitude } from ' react-native-audio-analyzer' ;
58-
59- export default function AudioWaveform() {
60- const [amplitudeData, setAmplitudeData] = useState <number []>([]);
61-
62- const analyzeAudio = useCallback (async () => {
63- try {
64- const path = await load (
65- ' https://file-examples.com/storage/fe180a8b03688f5559b9baf/2017/11/file_example_MP3_1MG.mp3'
66- );
67- const result = computeAmplitude (path , 100 );
68- setAmplitudeData (result );
69- } catch (error ) {
70- console .error (' Error analyzing audio:' , error );
71- }
72- }, []);
73-
74- useEffect (() => {
75- analyzeAudio ();
76- }, [analyzeAudio ]);
77-
78- return (
79- < ScrollView horizontal contentContainerStyle = {styles.container }>
80- < View style = {styles.waveform }>
81- {amplitudeData .map ((amplitude , index ) = > (
82- < View
83- key = {index }
84- style = {[
85- styles .bar ,
86- { height: 500 * amplitude } // Scale the height based on amplitude
87- ]}
88- />
89- ))}
90- </View>
91- </ScrollView>
92- );
93- }
94-
95- const styles = StyleSheet.create({
96- container: {
97- flexGrow: 1 ,
98- },
99- waveform: {
100- flex: 1 ,
101- flexDirection: ' row' ,
102- alignItems: ' center' ,
103- columnGap: 1 ,
104- },
105- bar: {
106- width: 3 ,
107- backgroundColor: ' #007AFF' ,
108- borderRadius: 1 ,
109- },
110- });
111- ```
112-
113- ### Advanced Usage
114-
115- ```typescript
116- import { computeAmplitude } from 'react -native -audio -analyzer ';
117-
118- // Customize the number of amplitude samples
119- const highResolution = computeAmplitude (' /audio.mp3' , 2000 ); // More detailed
120- const lowResolution = computeAmplitude (' /audio.mp3' , 100 ); // Less detailed
121-
122- // Process the amplitude data for different visualizations
123- const normalizedData = highResolution .map (amplitude =>
124- Math .min (amplitude * 100 , 1.0 ) // Normalize to 0-1 range
125- );
126-
127- // Create different visualization styles
128- const createWaveform = (data : number [], style : ' bars' | ' line' | ' area' ) => {
129- switch (style ) {
130- case ' bars' :
131- return data .map ((amp , i ) => ({ x: i , y: amp }));
132- case ' line' :
133- return data .map ((amp , i ) => ({ x: i , y: amp }));
134- case ' area' :
135- return data .map ((amp , i ) => ({ x: i , y: amp , height: amp }));
136- }
137- };
138- ```
54+ 📦 Full usage example can be found in the [ example] ( ./example ) directory.
13955
14056## API Reference 📚
14157
0 commit comments