### Create Real-time Audio Analysis Pipeline in Java Source: https://context7.com/jorensix/tarsosdsp/llms.txt This example demonstrates how to initialize an AudioDispatcher from the microphone and attach a chain of processors for high-pass filtering, pitch detection, onset detection, and level monitoring. It runs the processing in a background thread to ensure real-time performance. ```java import be.tarsos.dsp.*; import be.tarsos.dsp.io.jvm.*; import be.tarsos.dsp.pitch.*; import be.tarsos.dsp.onsets.*; import be.tarsos.dsp.filters.*; public class AudioAnalyzer { public static void main(String[] args) throws Exception { float sampleRate = 44100; int bufferSize = 2048; int overlap = bufferSize / 2; AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone( (int) sampleRate, bufferSize, overlap ); dispatcher.addAudioProcessor(new HighPass(80, sampleRate)); dispatcher.addAudioProcessor(new PitchProcessor( PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, sampleRate, bufferSize, (result, event) -> { if (result.isPitched()) { System.out.printf("[PITCH] %.2f Hz @ %.2fs%n", result.getPitch(), event.getTimeStamp()); } } )); ComplexOnsetDetector onsetDetector = new ComplexOnsetDetector(bufferSize, 0.3); onsetDetector.setHandler((time, salience) -> System.out.printf("[ONSET] %.3fs (strength: %.2f)%n", time, salience) ); dispatcher.addAudioProcessor(onsetDetector); dispatcher.addAudioProcessor(new AudioProcessor() { @Override public boolean process(AudioEvent event) { double dB = event.getdBSPL(); int bars = (int) Math.max(0, (dB + 70) / 2); System.out.printf("[LEVEL] %6.1f dB |%s%n", dB, "=".repeat(Math.min(bars, 40))); return true; } @Override public void processingFinished() {} }); dispatcher.addAudioProcessor(new AudioPlayer(dispatcher.getFormat())); System.out.println("Listening... Press Ctrl+C to stop."); new Thread(dispatcher, "Audio Analyzer").start(); } } ``` -------------------------------- ### Audio Effects: Delay and Flanger Source: https://context7.com/jorensix/tarsosdsp/llms.txt Demonstrates the implementation of delay (echo) and flanger audio effects using TarsosDSP. Includes examples of initializing and dynamically adjusting effect parameters. ```APIDOC ## Audio Effects - Delay and Flanger This section covers the implementation of delay (echo) and flanger audio effects within the TarsosDSP library. It provides code examples for initializing these effects and adjusting their parameters during runtime. ### Delay Effect Applies an echo effect to the audio stream. - **echoLength** (double) - The duration of the echo in seconds. - **decay** (double) - The decay factor of the echo, ranging from 0 (no echo) to 1 (infinite echo). ### Flanger Effect Applies a flanger effect to the audio stream. - **maxFlangerLength** (double) - The maximum delay for the flanger effect in seconds. - **wet** (double) - The wet/dry mix ratio, ranging from 0 to 1. - **lfoFrequency** (double) - The frequency of the Low-Frequency Oscillator (LFO) in Hz, controlling the modulation rate. ### Request Example (Java) ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.io.jvm.AudioPlayer; import be.tarsos.dsp.effects.DelayEffect; import be.tarsos.dsp.effects.FlangerEffect; import java.io.File; double sampleRate = 44100; AudioDispatcher dispatcher = AudioDispatcherFactory.fromFile( new File("audio.wav"), 2048, 1024 ); // Delay/Echo Effect DelayEffect delayEffect = new DelayEffect(0.5, 0.6, sampleRate); dispatcher.addAudioProcessor(delayEffect); // Adjust parameters dynamically delayEffect.setEchoLength(0.3); delayEffect.setDecay(0.4); // Flanger Effect FlangerEffect flangerEffect = new FlangerEffect( 0.01, 0.5, sampleRate, 2.0 ); dispatcher.addAudioProcessor(flangerEffect); // Adjust flanger parameters dynamically flangerEffect.setWet(0.7); flangerEffect.setLFOFrequency(3.0); flangerEffect.setFlangerLength(0.02); dispatcher.addAudioProcessor(new AudioPlayer(dispatcher.getFormat())); new Thread(dispatcher).start(); ``` ### Response Example (Conceptual) This is a conceptual representation as the effects modify the audio stream in real-time and do not return a specific JSON response. ```json { "status": "processing", "message": "Audio stream is being processed with delay and flanger effects." } ``` ``` -------------------------------- ### ComplexOnsetDetector - Musical Onset Detection API Source: https://context7.com/jorensix/tarsosdsp/llms.txt This API detects note onsets in audio signals using the Complex Domain Method, suitable for polyphonic music. It identifies the start of musical notes or events. ```APIDOC ## ComplexOnsetDetector - Musical Onset Detection ### Description Uses the Complex Domain Method to detect note onsets in audio signals, effective for complex polyphonic music. ### Method N/A (This is a library class, not a direct API endpoint) ### Endpoint N/A ### Parameters N/A (Class constructor parameters) - **fftSize** (int) - Required - The size of the FFT window. - **peakThreshold** (double) - Required - Threshold for peak detection (0.1-0.8). - **minimumIOI** (double) - Required - Minimum inter-onset interval in seconds. - **silenceThreshold** (double) - Required - dBSPL threshold for silence detection. ### Request Example ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.onsets.ComplexOnsetDetector; import be.tarsos.dsp.onsets.OnsetHandler; import java.io.File; int fftSize = 512; double peakThreshold = 0.3; double minimumIOI = 0.03; double silenceThreshold = -70.0; AudioDispatcher dispatcher = AudioDispatcherFactory.fromFile( new File("music.wav"), fftSize, fftSize / 2 ); ComplexOnsetDetector onsetDetector = new ComplexOnsetDetector( fftSize, peakThreshold, minimumIOI, silenceThreshold ); onsetDetector.setHandler(new OnsetHandler() { @Override public void handleOnset(double time, double salience) { System.out.printf("Onset detected at %.3f seconds (salience: %.2f)\n", time, salience); } }); dispatcher.addAudioProcessor(onsetDetector); new Thread(dispatcher).start(); ``` ### Response N/A (Results are handled by the `OnsetHandler` callback) #### Success Response (Handled by Callback) - **time** (double) - The time in seconds at which an onset was detected. - **salience** (double) - A measure of the salience or strength of the detected onset. #### Response Example (Callback Output) ``` Onset detected at 1.234 seconds (salience: 0.75) ``` ``` -------------------------------- ### Creating AudioDispatchers from Various Sources Source: https://context7.com/jorensix/tarsosdsp/llms.txt Shows how to initialize an AudioDispatcher using the AudioDispatcherFactory from diverse inputs including microphones, files, URLs, ffmpeg pipes, and raw float arrays. ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import java.io.File; import java.net.URL; AudioDispatcher micDispatcher = AudioDispatcherFactory.fromDefaultMicrophone(2048, 1024); AudioDispatcher fileDispatcher = AudioDispatcherFactory.fromFile(new File("song.wav"), 2048, 1024); AudioDispatcher urlDispatcher = AudioDispatcherFactory.fromURL(new URL("http://example.com/audio.wav"), 2048, 1024); AudioDispatcher pipeDispatcher = AudioDispatcherFactory.fromPipe("song.mp3", 44100, 2048, 1024); float[] samples = new float[44100]; for (int i = 0; i < samples.length; i++) { samples[i] = (float) Math.sin(2 * Math.PI * 440 * i / 44100); } AudioDispatcher arrayDispatcher = AudioDispatcherFactory.fromFloatArray(samples, 44100, 2048, 1024); ``` -------------------------------- ### Implementing a Custom AudioProcessor Pipeline Source: https://context7.com/jorensix/tarsosdsp/llms.txt Demonstrates how to create an AudioDispatcher from a file and attach a custom AudioProcessor to analyze audio data in real-time. The processor extracts RMS and dBSPL values from the audio buffer. ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.AudioEvent; import be.tarsos.dsp.AudioProcessor; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.io.jvm.AudioPlayer; import javax.sound.sampled.AudioFormat; import java.io.File; AudioDispatcher dispatcher = AudioDispatcherFactory.fromFile( new File("audio.wav"), 2048, 1024 ); dispatcher.addAudioProcessor(new AudioProcessor() { @Override public boolean process(AudioEvent audioEvent) { float[] buffer = audioEvent.getFloatBuffer(); double timeStamp = audioEvent.getTimeStamp(); double rms = audioEvent.getRMS(); double dBSPL = audioEvent.getdBSPL(); System.out.printf("Time: %.2fs, RMS: %.4f, dBSPL: %.2f%n", timeStamp, rms, dBSPL); return true; } @Override public void processingFinished() { System.out.println("Processing complete"); } }); dispatcher.addAudioProcessor(new AudioPlayer(dispatcher.getFormat())); new Thread(dispatcher, "Audio Dispatcher").start(); ``` -------------------------------- ### Perform Time-Scale Modification with WSOLA in Java Source: https://context7.com/jorensix/tarsosdsp/llms.txt Demonstrates how to use the WaveformSimilarityBasedOverlapAdd (WSOLA) class to change audio playback speed without altering pitch. It covers initialization with preset parameters and integration with an AudioDispatcher. ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.io.jvm.AudioPlayer; import be.tarsos.dsp.WaveformSimilarityBasedOverlapAdd; import be.tarsos.dsp.WaveformSimilarityBasedOverlapAdd.Parameters; import java.io.File; double sampleRate = 44100; double tempo = 1.5; WaveformSimilarityBasedOverlapAdd wsola = new WaveformSimilarityBasedOverlapAdd( Parameters.automaticDefaults(tempo, sampleRate) ); AudioDispatcher dispatcher = AudioDispatcherFactory.fromFile( new File("audio.wav"), wsola.getInputBufferSize(), wsola.getOverlap() ); wsola.setDispatcher(dispatcher); dispatcher.addAudioProcessor(wsola); dispatcher.addAudioProcessor(new AudioPlayer(dispatcher.getFormat())); new Thread(dispatcher).start(); ``` -------------------------------- ### Detect Pitch with PitchProcessor in Java Source: https://context7.com/jorensix/tarsosdsp/llms.txt Demonstrates how to use the PitchProcessor to identify fundamental frequency (f0) from a microphone input. It supports multiple algorithms like YIN and FFT_YIN, and includes logic to convert detected frequencies into musical note names. ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.pitch.PitchProcessor; import be.tarsos.dsp.pitch.PitchProcessor.PitchEstimationAlgorithm; import be.tarsos.dsp.pitch.PitchDetectionHandler; import be.tarsos.dsp.pitch.PitchDetectionResult; import be.tarsos.dsp.AudioEvent; float sampleRate = 44100; int bufferSize = 2048; int overlap = 0; AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone((int) sampleRate, bufferSize, overlap); PitchDetectionHandler handler = new PitchDetectionHandler() { @Override public void handlePitch(PitchDetectionResult result, AudioEvent e) { float pitch = result.getPitch(); float probability = result.getProbability(); boolean isPitched = result.isPitched(); if (isPitched && pitch > 0) { int midiNote = (int) Math.round(69 + 12 * Math.log(pitch / 440.0) / Math.log(2)); String[] noteNames = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"}; String noteName = noteNames[midiNote % 12] + (midiNote / 12 - 1); System.out.printf("Pitch: %.2f Hz (%s), Probability: %.2f%n", pitch, noteName, probability); } } }; PitchProcessor pitchProcessor = new PitchProcessor(PitchEstimationAlgorithm.YIN, sampleRate, bufferSize, handler); dispatcher.addAudioProcessor(pitchProcessor); new Thread(dispatcher).start(); ``` -------------------------------- ### Perform Spectral Analysis with FFT in Java Source: https://context7.com/jorensix/tarsosdsp/llms.txt Illustrates the use of the FFT class for forward and inverse Fourier transforms, including the application of window functions like Hamming and Hann to minimize spectral leakage. ```java import be.tarsos.dsp.util.fft.FFT; import be.tarsos.dsp.util.fft.HammingWindow; int fftSize = 1024; float sampleRate = 44100; FFT fft = new FFT(fftSize, new HammingWindow()); float[] audioData = new float[fftSize]; fft.forwardTransform(audioData); float[] amplitudes = new float[fftSize / 2]; fft.modulus(audioData, amplitudes); for (int i = 0; i < amplitudes.length; i++) { double frequency = fft.binToHz(i, sampleRate); System.out.printf("Bin %d: %.2f Hz, Amplitude: %.4f%n", i, frequency, amplitudes[i]); } fft.backwardsTransform(audioData); ``` -------------------------------- ### Perform Sample Rate Conversion with Resampler in Java Source: https://context7.com/jorensix/tarsosdsp/llms.txt Illustrates how to use the TarsosDSP Resampler for high-quality sample rate conversion. It covers initializing the Resampler with quality settings and factor ranges, processing audio data using FloatBuffers or arrays, and understanding the output. The Resampler is efficient for changing audio frequencies, and cloning an existing Resampler can optimize performance for repeated conversions. ```Java import be.tarsos.dsp.resample.Resampler; import java.nio.FloatBuffer; // Create resampler for conversion between 0.5x and 2.0x original rate boolean highQuality = true; double minFactor = 0.5; // Minimum resampling factor double maxFactor = 2.0; // Maximum resampling factor Resampler resampler = new Resampler(highQuality, minFactor, maxFactor); // Source audio at 44100Hz, target at 22050Hz double factor = 22050.0 / 44100.0; // 0.5 float[] inputSamples = new float[4096]; // ... fill with audio samples ... float[] outputSamples = new float[(int)(inputSamples.length * factor) + 100]; // Process with FloatBuffers FloatBuffer inputBuffer = FloatBuffer.wrap(inputSamples); FloatBuffer outputBuffer = FloatBuffer.wrap(outputSamples); boolean lastBatch = true; // Set to true for final batch resampler.process(factor, inputBuffer, lastBatch, outputBuffer); // Or use array interface Resampler.Result result = resampler.process( factor, inputSamples, 0, inputSamples.length, lastBatch, outputSamples, 0, outputSamples.length ); System.out.printf("Consumed %d input samples, generated %d output samples%n", result.inputSamplesConsumed, result.outputSamplesGenerated); // Clone resampler for parallel processing (faster than creating new) Resampler parallelResampler = new Resampler(resampler); ``` -------------------------------- ### Extract Mel-Frequency Cepstral Coefficients (MFCC) in Java Source: https://context7.com/jorensix/tarsosdsp/llms.txt Shows how to configure and use the MFCC processor to extract features from audio signals. It includes setting up filter banks and retrieving coefficients via an AudioProcessor callback. ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.AudioEvent; import be.tarsos.dsp.AudioProcessor; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.mfcc.MFCC; import java.io.File; int samplesPerFrame = 1024; int sampleRate = 44100; MFCC mfcc = new MFCC(samplesPerFrame, sampleRate, 13, 30, 133.33f, sampleRate / 2f); AudioDispatcher dispatcher = AudioDispatcherFactory.fromFile( new File("speech.wav"), samplesPerFrame, 0 ); dispatcher.addAudioProcessor(mfcc); dispatcher.addAudioProcessor(new AudioProcessor() { @Override public boolean process(AudioEvent audioEvent) { float[] mfccValues = mfcc.getMFCC(); return true; } @Override public void processingFinished() {} }); new Thread(dispatcher).start(); ``` -------------------------------- ### Synthesize Audio with SineGenerator and NoiseGenerator in Java Source: https://context7.com/jorensix/tarsosdsp/llms.txt Demonstrates programmatic audio synthesis using TarsosDSP. It shows how to create an AudioGenerator, add SineGenerator instances for different frequencies and amplitudes (including additive synthesis), and optionally a NoiseGenerator. The synthesized audio is then played back using an AudioPlayer. This functionality is useful for creating custom sound effects or musical elements. ```Java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.AudioGenerator; import be.tarsos.dsp.io.jvm.AudioPlayer; import be.tarsos.dsp.io.TarsosDSPAudioFormat; import be.tarsos.dsp.synthesis.SineGenerator; import be.tarsos.dsp.synthesis.NoiseGenerator; import javax.sound.sampled.AudioFormat; float sampleRate = 44100; int bufferSize = 1024; // Create audio format for synthesis TarsosDSPAudioFormat format = new TarsosDSPAudioFormat( sampleRate, // sample rate 16, // bits per sample 1, // channels (mono) true, // signed false // big endian ); // Create AudioGenerator (dispatcher for synthesis) AudioGenerator generator = new AudioGenerator(bufferSize, 0); // Sine wave generator double gain = 0.8; // Amplitude (0.0 to 1.0) double frequency = 440.0; // A4 note SineGenerator sineGen = new SineGenerator(gain, frequency); generator.addAudioProcessor(sineGen); // Add second sine for harmony (additive synthesis) SineGenerator sineGen2 = new SineGenerator(0.4, 554.37); // C#5 generator.addAudioProcessor(sineGen2); // White noise generator // NoiseGenerator noiseGen = new NoiseGenerator(0.1); // generator.addAudioProcessor(noiseGen); // Play the synthesized audio generator.addAudioProcessor(new AudioPlayer( new AudioFormat(sampleRate, 16, 1, true, false) )); // Run for a duration (generator produces infinite audio) new Thread(generator).start(); // Stop after 5 seconds Thread.sleep(5000); generator.stop(); ``` -------------------------------- ### Detect Musical Onsets with ComplexOnsetDetector in Java Source: https://context7.com/jorensix/tarsosdsp/llms.txt Utilizes the Complex Domain Method to detect note onsets in polyphonic audio files. It allows configuration of peak thresholds, minimum inter-onset intervals, and silence thresholds. ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.onsets.ComplexOnsetDetector; import be.tarsos.dsp.onsets.OnsetHandler; import java.io.File; int fftSize = 512; double peakThreshold = 0.3; double minimumIOI = 0.03; double silenceThreshold = -70.0; AudioDispatcher dispatcher = AudioDispatcherFactory.fromFile(new File("music.wav"), fftSize, fftSize / 2); ComplexOnsetDetector onsetDetector = new ComplexOnsetDetector(fftSize, peakThreshold, minimumIOI, silenceThreshold); onsetDetector.setHandler(new OnsetHandler() { @Override public void handleOnset(double time, double salience) { System.out.printf("Onset detected at %.3f seconds (salience: %.2f)%n", time, salience); } }); dispatcher.addAudioProcessor(onsetDetector); new Thread(dispatcher).start(); ``` -------------------------------- ### Detect Percussive Transients with PercussionOnsetDetector in Java Source: https://context7.com/jorensix/tarsosdsp/llms.txt Optimized for detecting drums and percussive transients using spectral energy rise detection. It provides parameters for sensitivity and energy threshold to fine-tune detection accuracy. ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.onsets.PercussionOnsetDetector; import be.tarsos.dsp.onsets.OnsetHandler; float sampleRate = 44100; int bufferSize = 1024; int overlap = 0; double sensitivity = 20; double threshold = 8; AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone((int) sampleRate, bufferSize, overlap); PercussionOnsetDetector percussionDetector = new PercussionOnsetDetector(sampleRate, bufferSize, new OnsetHandler() { @Override public void handleOnset(double time, double salience) { System.out.printf("Percussion hit at %.3f seconds%n", time); } }, sensitivity, threshold); dispatcher.addAudioProcessor(percussionDetector); new Thread(dispatcher).start(); ``` -------------------------------- ### TarsosDSP IIR Filters (Low Pass, High Pass, Band Pass) in Java Source: https://context7.com/jorensix/tarsosdsp/llms.txt This Java code snippet demonstrates the implementation of Infinite Impulse Response (IIR) filters, including Low Pass, High Pass, and Band Pass filters, using TarsosDSP. It shows how to set cutoff frequencies, bandwidth, and sample rates for audio processing. Requires TarsosDSP core and IO libraries. ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.io.jvm.AudioPlayer; import be.tarsos.dsp.filters.LowPassFS; import be.tarsos.dsp.filters.HighPass; import be.tarsos.dsp.filters.BandPass; import java.io.File; float sampleRate = 44100; AudioDispatcher dispatcher = AudioDispatcherFactory.fromFile( new File("audio.wav"), 2048, 0 ); // Low Pass Filter - passes frequencies below cutoff float lowPassCutoff = 1000; // Hz (minimum 60Hz) LowPassFS lowPass = new LowPassFS(lowPassCutoff, sampleRate); dispatcher.addAudioProcessor(lowPass); // High Pass Filter - passes frequencies above cutoff float highPassCutoff = 500; // Hz HighPass highPass = new HighPass(highPassCutoff, sampleRate); dispatcher.addAudioProcessor(highPass); // Band Pass Filter - passes frequencies within a band float centerFrequency = 1000; // Hz float bandwidth = 200; // Hz BandPass bandPass = new BandPass(centerFrequency, bandwidth, sampleRate); dispatcher.addAudioProcessor(bandPass); dispatcher.addAudioProcessor(new AudioPlayer(dispatcher.getFormat())); new Thread(dispatcher).start(); ``` -------------------------------- ### TarsosDSP Delay and Flanger Audio Effects in Java Source: https://context7.com/jorensix/tarsosdsp/llms.txt This Java code snippet demonstrates how to apply delay (echo) and flanger effects to an audio file using TarsosDSP. It shows initialization, parameter adjustment, and playback. Dependencies include TarsosDSP core and IO libraries. ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.io.jvm.AudioPlayer; import be.tarsos.dsp.effects.DelayEffect; import be.tarsos.dsp.effects.FlangerEffect; import java.io.File; double sampleRate = 44100; AudioDispatcher dispatcher = AudioDispatcherFactory.fromFile( new File("audio.wav"), 2048, 1024 ); // Delay/Echo Effect double echoLength = 0.5; // Echo delay in seconds double decay = 0.6; // Decay factor (0-1, 0=no echo, 1=infinite echo) DelayEffect delayEffect = new DelayEffect(echoLength, decay, sampleRate); dispatcher.addAudioProcessor(delayEffect); // Adjust parameters dynamically delayEffect.setEchoLength(0.3); delayEffect.setDecay(0.4); // Flanger Effect double maxFlangerLength = 0.01; // Max delay in seconds double wet = 0.5; // Wet/dry mix (0-1) double lfoFrequency = 2.0; // LFO rate in Hz FlangerEffect flangerEffect = new FlangerEffect( maxFlangerLength, wet, sampleRate, lfoFrequency ); dispatcher.addAudioProcessor(flangerEffect); // Adjust flanger parameters dynamically flangerEffect.setWet(0.7); flangerEffect.setLFOFrequency(3.0); flangerEffect.setFlangerLength(0.02); dispatcher.addAudioProcessor(new AudioPlayer(dispatcher.getFormat())); new Thread(dispatcher).start(); ``` -------------------------------- ### PitchProcessor - Pitch Detection API Source: https://context7.com/jorensix/tarsosdsp/llms.txt This API allows for pitch detection in audio signals using various algorithms like YIN, MPM, FFT_YIN, Dynamic Wavelet, and AMDF. It processes audio streams to identify the fundamental frequency (f0) and its confidence. ```APIDOC ## PitchProcessor - Pitch Detection ### Description Provides multiple pitch detection algorithms for identifying the fundamental frequency (f0) of audio signals. Supported algorithms include YIN, McLeod Pitch Method (MPM), FastYin (FFT-accelerated), Dynamic Wavelet, and AMDF. ### Method N/A (This is a library class, not a direct API endpoint) ### Endpoint N/A ### Parameters N/A (Class constructor parameters) - **algorithm** (PitchEstimationAlgorithm) - Required - The pitch estimation algorithm to use. - **sampleRate** (float) - Required - The sample rate of the audio. - **bufferSize** (int) - Required - The size of the audio buffer. - **handler** (PitchDetectionHandler) - Required - A callback to handle detected pitches. ### Request Example ```java // Example of initializing and using PitchProcessor float sampleRate = 44100; int bufferSize = 2048; int overlap = 0; AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone( (int) sampleRate, bufferSize, overlap ); PitchDetectionHandler handler = new PitchDetectionHandler() { @Override public void handlePitch(PitchDetectionResult result, AudioEvent e) { float pitch = result.getPitch(); float probability = result.getProbability(); boolean isPitched = result.isPitched(); if (isPitched && pitch > 0) { System.out.printf("Pitch: %.2f Hz, Probability: %.2f%%\n", pitch, probability * 100); } } }; PitchProcessor pitchProcessor = new PitchProcessor( PitchEstimationAlgorithm.YIN, sampleRate, bufferSize, handler ); dispatcher.addAudioProcessor(pitchProcessor); new Thread(dispatcher).start(); ``` ### Response N/A (Results are handled by the `PitchDetectionHandler` callback) #### Success Response (Handled by Callback) - **pitch** (float) - The detected fundamental frequency in Hz (-1 if not detected). - **probability** (float) - The confidence measure of the pitch detection (0.0 to 1.0). - **isPitched** (boolean) - Indicates if the algorithm determined a pitch was present. #### Response Example (Callback Output) ``` Pitch: 440.00 Hz, Probability: 95.00% ``` ``` -------------------------------- ### PercussionOnsetDetector - Drum/Percussion Detection API Source: https://context7.com/jorensix/tarsosdsp/llms.txt This API is optimized for detecting percussive sounds like drums and claps using spectral energy rise detection. ```APIDOC ## PercussionOnsetDetector - Drum/Percussion Detection ### Description Optimized for detecting percussive sounds like drums, claps, and other transients in audio signals using spectral energy rise detection. ### Method N/A (This is a library class, not a direct API endpoint) ### Endpoint N/A ### Parameters N/A (Class constructor parameters) - **sampleRate** (float) - Required - The sample rate of the audio. - **bufferSize** (int) - Required - The size of the audio buffer. - **handler** (OnsetHandler) - Required - A callback to handle detected onsets. - **sensitivity** (double) - Optional - Sensitivity for detection (0-100%). - **threshold** (double) - Optional - Energy rise threshold in dB (0-20). ### Request Example ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.onsets.PercussionOnsetDetector; import be.tarsos.dsp.onsets.OnsetHandler; float sampleRate = 44100; int bufferSize = 1024; int overlap = 0; double sensitivity = 20; double threshold = 8; AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone( (int) sampleRate, bufferSize, overlap ); PercussionOnsetDetector percussionDetector = new PercussionOnsetDetector( sampleRate, bufferSize, new OnsetHandler() { @Override public void handleOnset(double time, double salience) { System.out.printf("Percussion hit at %.3f seconds\n", time); } }, sensitivity, threshold ); dispatcher.addAudioProcessor(percussionDetector); new Thread(dispatcher).start(); ``` ### Response N/A (Results are handled by the `OnsetHandler` callback) #### Success Response (Handled by Callback) - **time** (double) - The time in seconds at which a percussion hit was detected. #### Response Example (Callback Output) ``` Percussion hit at 0.567 seconds ``` ``` -------------------------------- ### IIR Filters: Low Pass, High Pass, Band Pass Source: https://context7.com/jorensix/tarsosdsp/llms.txt Details the implementation of Infinite Impulse Response (IIR) filters, including Low Pass, High Pass, and Band Pass filters in TarsosDSP. Shows how to configure cutoff frequencies and bandwidths. ```APIDOC ## IIR Filters - Low Pass, High Pass, Band Pass This section explains how to use TarsosDSP's Infinite Impulse Response (IIR) filters for frequency-selective audio processing. Examples are provided for Low Pass, High Pass, and Band Pass filters. ### Low Pass Filter Allows frequencies below a specified cutoff frequency to pass through. - **lowPassCutoff** (float) - The cutoff frequency in Hz. Minimum value is 60Hz. ### High Pass Filter Allows frequencies above a specified cutoff frequency to pass through. - **highPassCutoff** (float) - The cutoff frequency in Hz. ### Band Pass Filter Allows frequencies within a specified band to pass through. - **centerFrequency** (float) - The center frequency of the passband in Hz. - **bandwidth** (float) - The bandwidth of the passband in Hz. ### Request Example (Java) ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.io.jvm.AudioPlayer; import be.tarsos.dsp.filters.LowPassFS; import be.tarsos.dsp.filters.HighPass; import be.tarsos.dsp.filters.BandPass; import java.io.File; float sampleRate = 44100; AudioDispatcher dispatcher = AudioDispatcherFactory.fromFile( new File("audio.wav"), 2048, 0 ); // Low Pass Filter LowPassFS lowPass = new LowPassFS(1000, sampleRate); dispatcher.addAudioProcessor(lowPass); // High Pass Filter HighPass highPass = new HighPass(500, sampleRate); dispatcher.addAudioProcessor(highPass); // Band Pass Filter BandPass bandPass = new BandPass(1000, 200, sampleRate); dispatcher.addAudioProcessor(bandPass); dispatcher.addAudioProcessor(new AudioPlayer(dispatcher.getFormat())); new Thread(dispatcher).start(); ``` ### Response Example (Conceptual) Similar to audio effects, filters modify the audio stream directly. ```json { "status": "processing", "message": "Audio stream is being processed with IIR filters." } ``` ``` -------------------------------- ### TarsosDSP GainProcessor and SilenceDetector in Java Source: https://context7.com/jorensix/tarsosdsp/llms.txt This Java code snippet illustrates the use of GainProcessor for volume control and SilenceDetector for identifying silent segments in an audio stream via TarsosDSP. It includes dynamic adjustment of gain and a custom processor to react to silence detection. Requires TarsosDSP core library. ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.AudioEvent; import be.tarsos.dsp.AudioProcessor; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.GainProcessor; import be.tarsos.dsp.SilenceDetector; AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(2048, 1024); // Gain Processor - volume adjustment double gain = 0.5; // 0.5 = half volume, 2.0 = double volume, 1.0 = unchanged GainProcessor gainProcessor = new GainProcessor(gain); dispatcher.addAudioProcessor(gainProcessor); // Adjust gain dynamically gainProcessor.setGain(1.5); // Silence Detector double silenceThreshold = -70.0; // dBSPL, typical range: -70 to -30 boolean breakOnSilence = false; // If true, stops processing chain when silent SilenceDetector silenceDetector = new SilenceDetector(silenceThreshold, breakOnSilence); dispatcher.addAudioProcessor(silenceDetector); // Custom processor to react to silence detection dispatcher.addAudioProcessor(new AudioProcessor() { @Override public boolean process(AudioEvent audioEvent) { float[] buffer = audioEvent.getFloatBuffer(); if (silenceDetector.isSilence(buffer)) { System.out.println("Silence detected at " + audioEvent.getTimeStamp()); } // Get current SPL level double currentSPL = silenceDetector.currentSPL(); System.out.printf("Current SPL: %.2f dB%n", currentSPL); return true; } @Override public void processingFinished() {} }); new Thread(dispatcher).start(); ``` -------------------------------- ### Volume Control and Silence Detection Source: https://context7.com/jorensix/tarsosdsp/llms.txt Covers the GainProcessor for volume adjustment and the SilenceDetector for identifying silent segments in an audio stream using TarsosDSP. ```APIDOC ## GainProcessor and SilenceDetector - Volume Control and Silence Detection This section details the utilities provided by TarsosDSP for managing audio volume and detecting silence. It includes the `GainProcessor` for volume adjustments and the `SilenceDetector` for identifying silent parts of an audio stream. ### Gain Processor Adjusts the volume level of the audio stream. - **gain** (double) - The gain factor. Values greater than 1.0 increase volume, less than 1.0 decrease volume, and 1.0 leaves it unchanged. ### Silence Detector Detects segments of the audio stream that fall below a specified loudness threshold. - **silenceThreshold** (double) - The loudness threshold in dBSPL below which audio is considered silent. Typical range is -70 to -30 dB. - **breakOnSilence** (boolean) - If true, the processing chain stops when silence is detected. ### Request Example (Java) ```java import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.AudioEvent; import be.tarsos.dsp.AudioProcessor; import be.tarsos.dsp.io.jvm.AudioDispatcherFactory; import be.tarsos.dsp.GainProcessor; import be.tarsos.dsp.SilenceDetector; AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(2048, 1024); // Gain Processor GainProcessor gainProcessor = new GainProcessor(0.5); dispatcher.addAudioProcessor(gainProcessor); // Adjust gain dynamically gainProcessor.setGain(1.5); // Silence Detector SilenceDetector silenceDetector = new SilenceDetector(-70.0, false); dispatcher.addAudioProcessor(silenceDetector); // Custom processor to react to silence detection dispatcher.addAudioProcessor(new AudioProcessor() { @Override public boolean process(AudioEvent audioEvent) { float[] buffer = audioEvent.getFloatBuffer(); if (silenceDetector.isSilence(buffer)) { System.out.println("Silence detected at " + audioEvent.getTimeStamp()); } double currentSPL = silenceDetector.currentSPL(); System.out.printf("Current SPL: %.2f dB%n", currentSPL); return true; } @Override public void processingFinished() {} }); new Thread(dispatcher).start(); ``` ### Response Example (Conceptual) These processors operate on audio streams and provide feedback via console output or custom logic. ```json { "status": "processing", "message": "Audio stream is being processed with gain adjustment and silence detection." } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.