|
1 | | -# react-native-audio-analyzer |
| 1 | +# react-native-audio-analyzer 🎵 |
2 | 2 |
|
3 | | -Audio analyzer |
| 3 | +A powerful library designed for React Native to visualize audio tracks, extract amplitude data, and create stunning audio waveforms. |
4 | 4 |
|
5 | | -## Installation |
| 5 | +This package offers robust tools that enable developers to process audio files efficiently, obtaining amplitude arrays for in-depth analysis or creating visually appealing audio waveforms within React Native applications. |
6 | 6 |
|
| 7 | +## Create Stunning Audio Waveforms 🌊 |
7 | 8 |
|
8 | | -```sh |
| 9 | +With this library, unleash creativity by generating captivating audio waveforms from your audio tracks, providing an engaging visual representation of sound. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +## Features ✨ |
| 14 | + |
| 15 | +- **Audio Analysis**: Easily analyze audio tracks to obtain amplitude data for visualization or analysis purposes |
| 16 | +- **Flexible Visualization**: Visualize amplitude data in a customizable manner, allowing developers to create diverse representations based on audio characteristics, including stunning audio waveforms |
| 17 | +- **Sample and Scale Data**: Utilize functions to sample and scale amplitude data, providing more granular control over visualization output |
| 18 | +- **Platform Compatibility**: Compatible with both Android and iOS platforms, ensuring a consistent experience across devices |
| 19 | +- **High Performance**: Built with C++ and miniaudio library for optimal performance |
| 20 | +- **TypeScript Support**: Full TypeScript support with comprehensive type definitions |
| 21 | + |
| 22 | +## Installation 🚀 |
| 23 | + |
| 24 | +```bash |
9 | 25 | npm install react-native-audio-analyzer react-native-nitro-modules |
| 26 | +# or |
| 27 | +yarn add react-native-audio-analyzer react-native-nitro-modules |
| 28 | +``` |
| 29 | + |
| 30 | +> **Note**: `react-native-nitro-modules` is required as this library relies on [Nitro Modules](https://nitro.margelo.com/). |
10 | 31 |
|
11 | | -> `react-native-nitro-modules` is required as this library relies on [Nitro Modules](https://nitro.margelo.com/). |
| 32 | +### iOS Setup |
| 33 | + |
| 34 | +```bash |
| 35 | +cd ios && pod install |
12 | 36 | ``` |
13 | 37 |
|
| 38 | +## Usage 🎶 |
14 | 39 |
|
15 | | -## Usage |
| 40 | +### Basic Usage |
16 | 41 |
|
| 42 | +```typescript |
| 43 | +import { computeAmplitude } from 'react-native-audio-analyzer'; |
17 | 44 |
|
18 | | -```js |
19 | | -import { multiply } from 'react-native-audio-analyzer'; |
| 45 | +// Analyze audio file and get amplitude data |
| 46 | +const amplitudeData = computeAmplitude('/path/to/audio.mp3', 1000); |
| 47 | +console.log('Amplitude data:', amplitudeData); |
| 48 | +``` |
20 | 49 |
|
21 | | -// ... |
| 50 | +### Creating Audio Waveforms |
| 51 | + |
| 52 | +```typescript |
| 53 | +import React, { useCallback, useEffect, useState } from 'react'; |
| 54 | +import { View, StyleSheet, ScrollView } from 'react-native'; |
| 55 | +import { computeAmplitude } from 'react-native-audio-analyzer'; |
| 56 | + |
| 57 | +export default function AudioWaveform() { |
| 58 | + const [amplitudeData, setAmplitudeData] = useState<number[]>([]); |
| 59 | + |
| 60 | + const analyzeAudio = useCallback(async () => { |
| 61 | + try { |
| 62 | + const result = computeAmplitude( |
| 63 | + '/path/to/your/audio.mp3', |
| 64 | + 1000 // Number of amplitude samples to generate |
| 65 | + ); |
| 66 | + setAmplitudeData(result); |
| 67 | + } catch (error) { |
| 68 | + console.error('Error analyzing audio:', error); |
| 69 | + } |
| 70 | + }, []); |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + analyzeAudio(); |
| 74 | + }, [analyzeAudio]); |
| 75 | + |
| 76 | + return ( |
| 77 | + <ScrollView horizontal contentContainerStyle={styles.container}> |
| 78 | + <View style={styles.waveform}> |
| 79 | + {amplitudeData.map((amplitude, index) => ( |
| 80 | + <View |
| 81 | + key={index} |
| 82 | + style={[ |
| 83 | + styles.bar, |
| 84 | + { height: 500 * amplitude } // Scale the height based on amplitude |
| 85 | + ]} |
| 86 | + /> |
| 87 | + ))} |
| 88 | + </View> |
| 89 | + </ScrollView> |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +const styles = StyleSheet.create({ |
| 94 | + container: { |
| 95 | + flexGrow: 1, |
| 96 | + }, |
| 97 | + waveform: { |
| 98 | + flex: 1, |
| 99 | + flexDirection: 'row', |
| 100 | + alignItems: 'center', |
| 101 | + columnGap: 1, |
| 102 | + }, |
| 103 | + bar: { |
| 104 | + width: 3, |
| 105 | + backgroundColor: '#007AFF', |
| 106 | + borderRadius: 1, |
| 107 | + }, |
| 108 | +}); |
| 109 | +``` |
22 | 110 |
|
23 | | -const result = multiply(3, 7); |
| 111 | +### Advanced Usage |
| 112 | + |
| 113 | +```typescript |
| 114 | +import { computeAmplitude } from 'react-native-audio-analyzer'; |
| 115 | + |
| 116 | +// Customize the number of amplitude samples |
| 117 | +const highResolution = computeAmplitude('/audio.mp3', 2000); // More detailed |
| 118 | +const lowResolution = computeAmplitude('/audio.mp3', 100); // Less detailed |
| 119 | + |
| 120 | +// Process the amplitude data for different visualizations |
| 121 | +const normalizedData = highResolution.map(amplitude => |
| 122 | + Math.min(amplitude * 100, 1.0) // Normalize to 0-1 range |
| 123 | +); |
| 124 | + |
| 125 | +// Create different visualization styles |
| 126 | +const createWaveform = (data: number[], style: 'bars' | 'line' | 'area') => { |
| 127 | + switch (style) { |
| 128 | + case 'bars': |
| 129 | + return data.map((amp, i) => ({ x: i, y: amp })); |
| 130 | + case 'line': |
| 131 | + return data.map((amp, i) => ({ x: i, y: amp })); |
| 132 | + case 'area': |
| 133 | + return data.map((amp, i) => ({ x: i, y: amp, height: amp })); |
| 134 | + } |
| 135 | +}; |
24 | 136 | ``` |
25 | 137 |
|
| 138 | +## API Reference 📚 |
| 139 | + |
| 140 | +### `computeAmplitude(filePath: string, outputSampleCount: number): number[]` |
| 141 | +
|
| 142 | +Analyzes an audio file and returns an array of amplitude values. |
| 143 | +
|
| 144 | +#### Parameters |
| 145 | +
|
| 146 | +- `filePath` (string): Path to the audio file to analyze |
| 147 | +- `outputSampleCount` (number): Number of amplitude samples to generate |
| 148 | +
|
| 149 | +#### Returns |
| 150 | +
|
| 151 | +- `number[]`: Array of amplitude values between 0 and 1 |
| 152 | +
|
| 153 | +#### Example |
| 154 | +
|
| 155 | +```typescript |
| 156 | +const amplitudes = computeAmplitude('/path/to/song.mp3', 500); |
| 157 | +// Returns: [0.1, 0.3, 0.5, 0.2, ...] (500 values) |
| 158 | +``` |
| 159 | +
|
| 160 | +## Supported Audio Formats 🎼 |
| 161 | +
|
| 162 | +This library supports various audio formats through the miniaudio library: |
| 163 | +
|
| 164 | +- **Lossy**: MP3, AAC, OGG Vorbis, Opus |
| 165 | +- **Lossless**: FLAC, WAV, AIFF |
| 166 | +- **Other**: WMA, M4A, and more |
| 167 | +
|
| 168 | +## Performance Considerations ⚡ |
| 169 | +
|
| 170 | +- The library uses native C++ code for optimal performance |
| 171 | +- Audio processing is done efficiently with minimal memory usage |
| 172 | +- Large audio files are processed in chunks to maintain responsiveness |
| 173 | +- Consider using appropriate `outputSampleCount` values based on your visualization needs |
| 174 | +
|
| 175 | +## Platform Support 📱 |
| 176 | +
|
| 177 | +- ✅ iOS 12.0+ |
| 178 | +- ✅ Android API 21+ |
| 179 | +- ✅ React Native 0.70+ |
| 180 | +
|
| 181 | +## Contributing 🤝 |
| 182 | +
|
| 183 | +We welcome contributions! Please see our [contributing guide](CONTRIBUTING.md) to learn how to contribute to this repository and the development workflow. |
| 184 | +
|
| 185 | +### Development Setup |
| 186 | +
|
| 187 | +```bash |
| 188 | +# Clone the repository |
| 189 | +git clone https://github.com/exzos28/react-native-audio-analyzer.git |
| 190 | +cd react-native-audio-analyzer |
| 191 | + |
| 192 | +# Install dependencies |
| 193 | +yarn install |
| 194 | + |
| 195 | +# Run the example app |
| 196 | +yarn example |
| 197 | +``` |
26 | 198 |
|
27 | | -## Contributing |
| 199 | +## License 📝 |
28 | 200 |
|
29 | | -See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow. |
| 201 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
30 | 202 |
|
31 | | -## License |
| 203 | +## Related Projects 🔗 |
32 | 204 |
|
33 | | -MIT |
| 205 | +- [react-native-nitro-modules](https://github.com/margelo/react-native-nitro-modules) - The underlying native module system |
| 206 | +- [miniaudio](https://github.com/mackron/miniaudio) - The audio processing library |
34 | 207 |
|
35 | 208 | --- |
36 | 209 |
|
37 | | -Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob) |
| 210 | +Made with ❤️ by the React Native community |
0 commit comments