杰瑞科技汇

SoundTouch Python如何安装使用?

Of course! The SoundTouch library is a powerful open-source audio processing library for changing tempo, pitch, and playback rate of audio streams and files. It's widely used in music production, DJ software, and audio applications.

SoundTouch Python如何安装使用?-图1
(图片来源网络,侵删)

Here's a comprehensive guide on how to use SoundTouch with Python, covering the installation, basic concepts, and practical code examples.


What is SoundTouch?

SoundTouch is not a simple audio player. It's an audio processing engine. Its core function is to manipulate the audio content itself:

  • Tempo Change: Changes the speed of the audio without affecting the pitch. This is great for changing the BPM (Beats Per Minute) of a song.
  • Pitch Change: Changes the pitch of the audio (e.g., making a singer sound higher or lower) without affecting the tempo.
  • Playback Rate (Master Speed): Changes both the tempo and the pitch together, just like playing a tape faster or slower.

It's written in C++ for high performance, which is essential for real-time audio processing.


Installation

There are two main ways to use SoundTouch in Python:

SoundTouch Python如何安装使用?-图2
(图片来源网络,侵删)
  1. pySoundTouch (Recommended for most users): A pure Python wrapper that provides a high-level, easy-to-use interface. It's the simplest way to get started.
  2. soundtouch-python (Advanced): A C-based extension that wraps the original C++ SoundTouch library. It offers more direct control and potentially better performance but is more complex to install.

Option A: pySoundTouch (Easiest)

This is the recommended approach for most use cases. It handles the file I/O and processing for you.

pip install pySoundTouch

Option B: soundtouch-python (Advanced)

This package requires the native SoundTouch library to be installed on your system first.

Step 1: Install Native SoundTouch Library

  • On macOS (using Homebrew):
    brew install soundtouch
  • On Debian/Ubuntu:
    sudo apt-get update
    sudo apt-get install libsoundtouch-dev
  • On Windows (using vcpkg): This can be more involved. You might need to compile it from source or find a pre-compiled binary. pySoundTouch is often a simpler choice on Windows.

Step 2: Install the Python Package

SoundTouch Python如何安装使用?-图3
(图片来源网络,侵删)
pip install soundtouch-python

Basic Usage with pySoundTouch

The pySoundTouch library is designed to be very straightforward. You typically work with an AudioProcessor object.

Key Concepts in pySoundTouch

  • SoundProcessor: The main class that holds the audio data and applies the effects.
  • tempo: A float value. 0 is normal speed. 5 is 50% faster, 8 is 20% slower.
  • pitch: A float value representing the semitone shift. 0 is no change. 0 is one octave higher, -12.0 is one octave lower.
  • rate: A float value. 0 is normal. 0 is twice as fast (both tempo and pitch increase).

Example 1: Changing Tempo and Pitch

Let's take an audio file, change its tempo, and save the result.

import soundtouch
import os
# --- Setup ---
input_file = 'my_music.mp3'
output_file_tempo = 'output_tempo_changed.mp3'
output_file_pitch = 'output_pitch_changed.mp3'
# Check if input file exists
if not os.path.exists(input_file):
    print(f"Error: Input file '{input_file}' not found.")
    # Create a dummy file for demonstration if it doesn't exist
    # In a real scenario, you would have your own audio file.
    # For this example, we'll assume the file is there.
    # exit()
# --- 1. Change Tempo ---
print(f"Processing '{input_file}' to change tempo...")
# Create a processor instance
processor = soundtouch.SoundProcessor()
# Set the tempo (e.g., 25% faster)
processor.tempo = 1.25
# Process the file and save the result
# The process_file method handles reading, processing, and writing.
processor.process_file(input_file, output_file_tempo)
print(f"Tempo-changed audio saved to '{output_file_tempo}'")
# --- 2. Change Pitch ---
print(f"\nProcessing '{input_file}' to change pitch...")
# Create a new processor for a new effect
processor_pitch = soundtouch.SoundProcessor()
# Set the pitch (e.g., 3 semitones higher)
processor_pitch.pitch = 3.0
# Process the file and save the result
processor_pitch.process_file(input_file, output_file_pitch)
print(f"Pitch-changed audio saved to '{output_file_pitch}'")

Example 2: Combining Effects

You can combine tempo and pitch changes. The order doesn't matter as they are applied simultaneously.

import soundtouch
input_file = 'my_music.mp3'
output_file_combined = 'output_combined_effects.mp3'
print(f"Processing '{input_file}' with combined tempo and pitch...")
processor = soundtouch.SoundProcessor()
# Make it 20% faster AND 5 semitones higher
processor.tempo = 1.2
processor.pitch = 5.0
processor.process_file(input_file, output_file_combined)
print(f"Combined-effects audio saved to '{output_file_combined}'")

Advanced Usage with soundtouch-python

This library gives you more direct control over the underlying SoundTouch engine. It's useful if you need to process audio in chunks (e.g., from a microphone stream) rather than just whole files.

The workflow is different:

  1. Create a SoundTouch instance.
  2. Create a SoundStream instance for input.
  3. Set parameters on the SoundTouch instance.
  4. Read data from the input stream.
  5. Pass the data to the SoundTouch instance for processing.
  6. Get the processed data from the SoundTouch instance.
  7. Write the processed data to an output file.

Example: Processing Audio in Chunks

This example demonstrates how you might process audio data as it becomes available, which is essential for real-time applications.

import soundtouch
import numpy as np
import wave # For creating a simple WAV file
# --- Helper function to create a dummy WAV file for the example ---
def create_dummy_wav(filename, duration_sec=5, frequency=440, sample_rate=44100):
    """Creates a mono WAV file with a sine wave."""
    samples = int(duration_sec * sample_rate)
    waves = np.sin(2 * np.pi * frequency * np.arange(samples) / sample_rate)
    with wave.open(filename, 'wb') as wf:
        wf.setnchannels(1)
        wf.setsampwidth(2) # 16-bit
        wf.setframerate(sample_rate)
        wf.writeframes((waves * 32767).astype(np.int16).tobytes())
# --- Setup ---
input_file = 'dummy_sine.wav'
output_file = 'output_processed_stream.wav'
create_dummy_wav(input_file) # Create a dummy input file
# --- 1. Setup SoundTouch and Streams ---
# The SoundTouch object holds the processing parameters
st = soundtouch.SoundTouch()
# Set parameters (e.g., 20% faster)
st.setTempo(1.2)
# The SoundStream object handles the low-level audio data
# We need to know the sample rate and number of channels of our input file
# For our dummy file, it's 44100 Hz, 1 channel
input_stream = soundtouch.SoundStream(st, sample_rate=44100, num_channels=1)
# --- 2. Process the file in chunks ---
print(f"Processing '{input_file}' in chunks...")
# Open input and output files
with open(input_file, 'rb') as fin, open(output_file, 'wb') as fout:
    # Read the WAV header (we'll just copy it for the output)
    header = fin.read(44)
    fout.write(header)
    # Process the audio data in chunks
    chunk_size = 4096 # Number of samples per chunk
    while True:
        # Read a chunk of raw PCM data (16-bit integers)
        raw_data = fin.read(chunk_size * 2) # 2 bytes per sample
        if not raw_data:
            break # End of file
        # Convert raw bytes to a numpy array of integers
        # This step is crucial for the C extension to work correctly
        pcm_data = np.frombuffer(raw_data, dtype=np.int16)
        # Put the data into the input stream
        input_stream.putSamples(pcm_data, num_samples=len(pcm_data))
        # Get the processed data from the stream
        # We might need to call getSamples multiple times to get all processed data
        processed_samples = input_stream.receiveSamples(chunk_size)
        if len(processed_samples) > 0:
            # Convert the processed samples back to bytes and write to output
            fout.write(processed_samples.astype(np.int16).tobytes())
print(f"Stream-processed audio saved to '{output_file}'")

Choosing the Right Library

Feature pySoundTouch soundtouch-python
Ease of Use Excellent. Very high-level, simple API. Fair. More complex, requires understanding of PCM data.
Performance Good. Handles file I/O internally. Excellent. More direct C interface, better for low-latency.
Use Case Simple batch processing of audio files. Real-time audio streams, custom audio applications, games.
Dependencies Minimal (just Python). Requires native libsoundtouch to be installed.
Recommendation Start here. It's perfect for 90% of use cases. Use only if you need fine-grained, real-time control.

Summary

For most Python users, pySoundTouch is the best place to start. It provides a clean, simple interface for common audio manipulation tasks like changing the tempo or pitch of a music file. If you find you need to process live audio streams or require maximum performance, then you can explore the more advanced soundtouch-python library.

分享:
扫描分享到社交APP
上一篇
下一篇