WavTools is a comprehensive JavaScript library for browser-based audio recording, streaming, and analysis. It provides powerful tools for capturing audio input, managing playback streams, and extracting frequency domain data, making it ideal for developers working on real-time audio applications. This is a fork of wavtools originally created by Keith Horwood - while the original repository is no longer available, the code was discovered in the openai/openai-realtime-console repository.
This repository is no longer maintained. The original author of WavTools, Keith Horwood, has resumed active development and now maintains the official repository at keithwhor/wavtools.
You can still find the latest version on npm.
Please transition to the official repository for the latest updates, improvements, and support. Thank you for using WavTools!
- π Frequency Domain Analysis: Extract and analyze frequency data from audio inputs.
- π§ Support for HTMLAudioElement: Seamlessly integrate with HTML audio elements for real-time analysis.
- π Flexible Frequency Parsing: Customize frequency parsing to suit your application's needs.
- πΌ Music and Voice Analysis Modes: Switch between different modes optimized for music and voice data.
Install WavTools using npm:
npm install wavtoolsThere is no documentation yet. But you can see an exmaple of usage in the next section.
You can import the necessary classes from wavtools:
import { WavRecorder, WavStreamPlayer } from 'wavtools';The WavRecorder class provides functionality for recording audio from the browser's microphone input. Key capabilities include:
- Recording PCM audio data at configurable sample rates
- Pausing and resuming recordings
- Real-time frequency analysis during recording
- Saving recordings as WAV files
- Chunked processing of audio data
- Device management (listing/selecting input devices)
- Permission handling for microphone access
The recorder provides both raw PCM data and a processed mono channel, making it suitable for both audio storage and real-time analysis.
The WavRecorder class is used for recording audio. Here's how you can use it:
-
Initialization: Create an instance of
WavRecorderwith a specified sample rate.const wavRecorder = new WavRecorder({ sampleRate: 24000 });
-
Begin Recording: Start the recording process.
await wavRecorder.begin(deviceId); // Pass the device ID if needed
To find the device ID, you can use the following code:
navigator.mediaDevices.enumerateDevices() .then(devices => { devices.forEach(device => { console.log(`${device.kind}: ${device.label} (ID: ${device.deviceId})`); }); }) .catch(error => { console.error('Error accessing media devices:', error); });
-
Recording Audio: Record audio data. You can provide a callback to handle the recorded audio data.
await wavRecorder.record((data) => { // Handle the recorded audio data console.log(data.mono); // Access mono channel data });
-
Pause/End Recording: Pause or end the recording session.
await wavRecorder.pause(); // To pause await wavRecorder.end(); // To end
-
Get Frequencies: Retrieve frequency data from the recording.
const frequencies = wavRecorder.getFrequencies('voice').values;
The WavStreamPlayer class handles playback of audio streams, specifically designed for PCM data. Features include:
- Streaming playback of 16-bit PCM audio data
- Support for multiple audio tracks
- Real-time frequency analysis during playback
- Track position tracking and interruption
- Sample rate conversion
- Automatic audio context management
The player is optimized for low-latency streaming scenarios where audio data arrives in chunks, making it ideal for applications like real-time audio communication or streaming playback.
The WavStreamPlayer class is used for playing audio streams. Here's how you can use it:
-
Initialization: Create an instance of
WavStreamPlayerwith a specified sample rate.const wavStreamPlayer = new WavStreamPlayer({ sampleRate: 24000 });
-
Connect: Connect the player to start playing audio.
await wavStreamPlayer.connect();
-
Add Audio Data: Add audio data to the player for playback.
wavStreamPlayer.add16BitPCM(audioData, trackId);
-
Interrupt Playback: Interrupt the current playback.
const trackSampleOffset = await wavStreamPlayer.interrupt();
-
Get Frequencies: Retrieve frequency data from the playback.
const frequencies = wavStreamPlayer.getFrequencies('voice').values;
Here's a simple example of how you might use wavtools in a React component:
import React, { useRef } from 'react';
import { WavRecorder, WavStreamPlayer } from 'wavtools';
const AudioComponent = () => {
const wavRecorderRef = useRef(new WavRecorder({ sampleRate: 24000 }));
const wavStreamPlayerRef = useRef(new WavStreamPlayer({ sampleRate: 24000 }));
const startRecording = async () => {
const wavRecorder = wavRecorderRef.current;
await wavRecorder.begin();
await wavRecorder.record((data) => {
console.log('Recording data:', data.mono);
});
};
const stopRecording = async () => {
const wavRecorder = wavRecorderRef.current;
await wavRecorder.end();
};
const playAudio = async (audioData) => {
const wavStreamPlayer = wavStreamPlayerRef.current;
await wavStreamPlayer.connect();
wavStreamPlayer.add16BitPCM(audioData, 'trackId');
};
return (
<div>
<button onClick={startRecording}>Start Recording</button>
<button onClick={stopRecording}>Stop Recording</button>
<button onClick={() => playAudio(someAudioData)}>Play Audio</button>
</div>
);
};
export default AudioComponent;This example demonstrates basic recording and playback functionality using wavtools. You can expand upon this by integrating it with other parts of your application, such as handling audio input devices or managing audio sessions.
We welcome contributions! To contribute:
- Fork the repository
- Create a new branch (
git checkout -b feature/improvement) - Make your changes
- Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/improvement) - Create a Pull Request
Please ensure your PR description clearly describes the changes and their benefits. Follow existing code style and include tests if applicable.
WavTools is licensed under the MIT License. See the LICENSE file for more details.