-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_plotter.py
106 lines (84 loc) · 3.01 KB
/
audio_plotter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
Audio Waveform Plotter
This Python script allows you to visualize and plot time-domain waveforms of audio files and play them back using the IPython library. It is specifically designed to handle heart and lung sound data but can be applied to any audio file.
## Features:
- Plot multiple waveforms in one figure.
- Automatically adjust for the number of audio files.
- Save the figure as an image file.
- Play the audio file directly using IPython.
## Citation:
If you use this code or the associated dataset in your research, please cite the following paper:
- Torabi, Y., Shirani, S., & Reilly, J. P. (2024),
Manikin-Recorded Cardiopulmonary Sounds Dataset Using Digital Stethoscope,
arXiv preprint, https://doi.org/10.48550/arXiv.2410.03280
## Copyright:
© 2024 by Yasaman Torabi. All rights reserved.
"""
import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np
import os
import IPython.display as ipd
def plot_audio_waveform(audio_path, ax, title):
"""
Plots the waveform of an audio file.
Parameters:
- audio_path: str, path to the audio file
- ax: matplotlib axis, axis to plot on
- title: str, title of the plot
"""
if not os.path.exists(audio_path):
print(f"Error: {audio_path} not found.")
return
# Load audio
y, sr = librosa.load(audio_path)
# Plot waveform
librosa.display.waveshow(y, sr=sr, color='black', ax=ax)
ax.set_title(title)
ax.set_ylim([-0.05, 0.05]) # Standardize y-limits
ax.set_xlim([0, len(y) / sr]) # Time in seconds
ax.set_xlabel('Time (s)')
ax.set_ylabel('Amplitude')
def plot_combined_waveforms(audio_paths, titles, output_file='combined_plots.png'):
"""
Plots waveforms for multiple audio files and saves the figure as an image.
Parameters:
- audio_paths: list of str, paths to the audio files
- titles: list of str, titles for each plot
- output_file: str, name of the output file for the figure
"""
num_plots = len(audio_paths)
# Create subplots
fig, axs = plt.subplots(num_plots, 1, figsize=(12, 4 * num_plots))
for i in range(num_plots):
plot_audio_waveform(audio_paths[i], axs[i], titles[i])
# Adjust layout and save figure
plt.tight_layout()
plt.savefig(output_file)
plt.show()
def play_audio(audio_path):
"""
Plays the audio using IPython's Audio player.
Parameters:
- audio_path: str, path to the audio file
"""
if not os.path.exists(audio_path):
print(f"Error: {audio_path} not found.")
return
# Load the audio
y, sr = librosa.load(audio_path)
# Play the audio
return ipd.Audio(y, rate=sr)
# Example usage
audio_files = [
'/content/M_AF_LC.wav', # Update paths if necessary
'/content/M_S3_C_RUSB.wav',
'/content/M_W_RLA.wav'
]
titles = ['AF_LC', 'S3_C_RUSB', 'W_RLA']
# Plot and save combined waveforms
plot_combined_waveforms(audio_files, titles)
# Play one of the audio files
audio_to_play = '/content/M_W_RLA.wav'
play_audio(audio_to_play)