用python生成能显示wav文件的波形,还可以在时间轴上进行裁剪截取部分波形并保存波形文件的PYQT程序
时间: 2024-05-14 20:16:01 浏览: 82
Python读取Wav文件、输出波形、播放wav文件、输出Wav文件
4星 · 用户满意度95%
下面是一个简单的PYQT程序,它可以用于显示WAV文件的波形,并允许用户在时间轴上裁剪和保存部分波形:
```python
import sys
import wave
import numpy as np
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QFileDialog
from PyQt5.QtGui import QIcon, QPainter
class WaveformWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setMinimumSize(400, 200)
self._data = None
self._pos = None
def set_data(self, data, pos):
self._data = data
self._pos = pos
self.update()
def paintEvent(self, event):
if self._data is None:
return
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(Qt.blue)
width, height = self.width(), self.height()
x_scale = width / len(self._data)
y_scale = height / 2**15
path = QPainterPath()
path.moveTo(0, height / 2)
for i in range(len(self._data)):
x = i * x_scale
y = self._data[i] * y_scale + height / 2
path.lineTo(x, y)
painter.drawPath(path)
if self._pos is not None:
painter.setPen(Qt.red)
x = self._pos * x_scale
painter.drawLine(x, 0, x, height)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Waveform Viewer')
self.setCentralWidget(WaveformWidget())
self.create_actions()
self.create_menus()
def create_actions(self):
self.open_action = QAction(QIcon('open.png'), 'Open', self)
self.open_action.setShortcut('Ctrl+O')
self.open_action.triggered.connect(self.open_file)
self.save_action = QAction(QIcon('save.png'), 'Save', self)
self.save_action.setShortcut('Ctrl+S')
self.save_action.triggered.connect(self.save_file)
self.quit_action = QAction('Quit', self)
self.quit_action.setShortcut('Ctrl+Q')
self.quit_action.triggered.connect(self.close)
def create_menus(self):
self.file_menu = self.menuBar().addMenu('File')
self.file_menu.addAction(self.open_action)
self.file_menu.addAction(self.save_action)
self.file_menu.addSeparator()
self.file_menu.addAction(self.quit_action)
def open_file(self):
filename, _ = QFileDialog.getOpenFileName(self, 'Open WAV file', '', 'WAV files (*.wav)')
if filename:
with wave.open(filename, 'rb') as f:
nframes = f.getnframes()
data = np.frombuffer(f.readframes(nframes), dtype=np.int16)
self.centralWidget().set_data(data, None)
def save_file(self):
filename, _ = QFileDialog.getSaveFileName(self, 'Save WAV file', '', 'WAV files (*.wav)')
if filename:
pos = self.centralWidget()._pos
if pos is None:
return
with wave.open(filename, 'wb') as f:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(44100)
start = int(pos * 44100)
end = int(len(self.centralWidget()._data) * pos)
f.writeframes(self.centralWidget()._data[start:end].tobytes())
def mousePressEvent(self, event):
width = self.centralWidget().width()
pos = event.x() / width
self.centralWidget()._pos = pos
self.centralWidget().update()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
注意:这个程序需要PyQt5和numpy库才能运行。在程序中,我们创建了一个名为WaveformWidget的QWidget子类,它用于显示WAV文件的波形。该类有一个名为set_data的方法,它接受一个numpy数组和一个浮点数,用于设置要显示的数据和当前位置。paintEvent方法用于绘制波形,mousePressEvent方法用于响应鼠标单击事件,并将当前位置设置为鼠标位置。MainWindow类用于创建应用程序的主窗口,并包含一个WaveformWidget实例。在这个类中,我们还创建了一些QAction和QMenu对象,用于打开和保存WAV文件。在open_file和save_file方法中,我们使用wave库来读取和写入WAV文件,使用numpy库来处理波形数据。
阅读全文