react-native-pitchy
v1.0.0
Published
A simple pitch detection module for React Native
Downloads
38
Maintainers
Readme
react-native-pitchy
A real-time pitch detection library for React Native.
Installation
- Install the library using a package manager of your choice:
npm install react-native-pitchy
- Linking (iOS only):
npx pod-install
- Rebuild your app after installing the package.
On iOS simulators, pitch detection may not work as expected due to the lack of microphone support. Make sure to test on a real device.
Permissions
Microphone permissions are required for pitch detection to work. Make sure to request the necessary permissions before starting pitch detection. You can use a library like react-native-permissions to request permissions.
iOS
Microphone
Android
RECORD_AUDIO
Usage
- Import the library:
import Pitchy, { PitchyConfig, PitchyEventCallback } from 'react-native-pitchy';
- (Optional) Configure Pitchy before initialization:
const config: PitchyConfig = {
bufferSize: 4096, // Adjust buffer size for performance vs accuracy
minVolume: -60, // Minimum volume threshold for pitch detection (in dB)
};
- Initialize Pitchy with the optional configuration:
Pitchy.init(config);
- Start pitch detection:
Pitchy.start().then(() => {
console.log('Pitch detection started!');
});
- Listen to pitch events:
const handlePitchDetected: PitchyEventCallback = (data) => {
console.log('Pitch detected:', data.pitch);
};
const subscription = Pitchy.addListener(handlePitchDetected);
// Remember to unsubscribe later to avoid memory leaks
subscription.remove();
- Stop pitch detection:
Pitchy.stop().then(() => {
console.log('Pitch detection stopped!');
});
- Check if pitch detection is running:
Pitchy.isRecording().then((isRecording) => {
console.log('Pitch detection is recording:', isRecording);
});
API
Types
PitchyAlgorithm
: String representing the pitch detection algorithm (currently only supports"ACF2+"
)PitchyConfig
: Configuration object for Pitchy.initbufferSize
: (optional) Number representing the audio buffer size (default: 4096)minVolume
: (optional) Number representing the minimum volume threshold (default: -60)algorithm
: (optional)PitchyAlgorithm
used for detection (default: "ACF2+")
PitchyEventCallback
: Function called when pitch is detected. Receives an object with apitch
property (number)
Methods
Pitchy.init(config?: PitchyConfig)
: Initializes Pitchy with an optional configuration.Pitchy.start()
: Starts pitch detection and returns a Promise.Pitchy.stop()
: Stops pitch detection and returns a Promise.Pitchy.isRecording()
: Checks if pitch detection is currently running and returns a Promise resolving to a boolean.Pitchy.addListener(callback: PitchyEventCallback)
: Adds a listener for theonPitchDetected
event.
Roadmap
- [ ] Fix example app (permissions issue related to builder-bob)
- [ ] Add FFT for ACF2+ algorithm (currently uses a naive implementation)
- [ ] Add more pitch detection algorithms
Examples
Check out the example app for a simple implementation of pitch detection using Pitchy.
Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
License
MIT