Adding Mean Pitch Intervals to TextGrid with Praat in Python

Answered using GPT 40 by GPT40

Question:

praat add to textgrid intervals with mean pitch for every phonem in python using praatio and parselmouth. praatio 6.0 verison

Answer:

To add intervals with mean pitch for every phoneme in a TextGrid using praatio and parselmouth, you can follow these steps. Below is a Python script that demonstrates how to achieve this. Make sure you have both praatio and parselmouth installed in your Python environment.

Step 1: Install Required Libraries

If you haven't already installed praatio and parselmouth, you can do so using pip:

pip install praatio parselmouth

Step 2: Python Script

Here's a sample script that reads a TextGrid file, calculates the mean pitch for each phoneme, and adds this information as new intervals in the TextGrid.

import parselmouth
from praatio import tgio
import numpy as np

def calculate_mean_pitch(sound_file, textgrid_file):
    # Load the sound file
    sound = parselmouth.Sound(sound_file)
    
    # Load the TextGrid file
    tg = tgio.openTextgrid(textgrid_file)
    
    # Get the phoneme tier (assuming the first tier is phonemes)
    phoneme_tier = tg.tiers[0]  # Adjust index if necessary
    
    # Prepare a list to hold mean pitch intervals
    mean_pitch_intervals = []
    
    for interval in phoneme_tier:
        start_time, end_time, phoneme = interval
        # Extract the segment of the sound corresponding to the phoneme
        segment = sound.extract_part(start_time, end_time)
        
        # Calculate the pitch
        pitch = segment.to_pitch()
        
        # Get the mean pitch (in Hz)
        mean_pitch = np.mean(pitch.selected_array['frequency'][pitch.selected_array['frequency'] > 0])
        
        # Append the mean pitch as a new interval
        mean_pitch_intervals.append((start_time, end_time, f'Mean Pitch: {mean_pitch:.2f} Hz'))
    
    # Create a new tier for mean pitch
    mean_pitch_tier = tgio.IntervalTier('Mean Pitch', mean_pitch_intervals)
    
    # Add the new tier to the TextGrid
    tg.addTier(mean_pitch_tier)
    
    # Save the modified TextGrid
    tg.save(textgrid_file.replace('.TextGrid', '_with_mean_pitch.TextGrid'))

# Example usage
sound_file = 'path/to/your/sound.wav'
textgrid_file = 'path/to/your/textgrid.TextGrid'
calculate_mean_pitch(sound_file, textgrid_file)

Explanation of the Code:

  1. Load Sound and TextGrid: The script loads the sound file and the TextGrid file using parselmouth and praatio.

  2. Extract Phoneme Intervals: It assumes that the first tier of the TextGrid contains phoneme intervals. You may need to adjust the index if your phoneme tier is located elsewhere.

  3. Calculate Mean Pitch: For each phoneme interval, it extracts the corresponding sound segment and calculates the mean pitch using parselmouth.

  4. Create New Tier: It creates a new interval tier for mean pitch and adds it to the TextGrid.

  5. Save the Modified TextGrid: Finally, it saves the modified TextGrid with a new filename.

Note:

  • Make sure to adjust the paths for sound_file and textgrid_file to point to your actual files.
  • The script assumes that the first tier of the TextGrid contains phoneme intervals. If your TextGrid structure is different, you may need to modify the code