用python生成显示wav文件波形和增加可以在时间轴裁剪波形后保存波形文件的PYQT程序

时间: 2024-05-01 14:19:04 浏览: 11
下面是一个简单的PYQT程序,可以生成显示wav文件波形,并且可以在时间轴上裁剪波形后保存裁剪后的波形文件。 ```python import sys import wave import struct import numpy as np from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog from PyQt5.QtCore import Qt from PyQt5.QtGui import QPainter, QPen, QColor class WaveformViewer(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Waveform Viewer') self.setGeometry(100, 100, 800, 400) self.filename = None self.data = None self.width = None self.height = None self.start_time = 0 self.end_time = None self.mouse_pressed = False self.initUI() def initUI(self): self.openAction = self.createAction('Open', self.openFile, 'Ctrl+O') self.saveAction = self.createAction('Save', self.saveFile, 'Ctrl+S') self.quitAction = self.createAction('Quit', self.close, 'Ctrl+Q') menubar = self.menuBar() fileMenu = menubar.addMenu('File') fileMenu.addAction(self.openAction) fileMenu.addAction(self.saveAction) fileMenu.addSeparator() fileMenu.addAction(self.quitAction) self.statusBar() def createAction(self, text, slot, shortcut=None, icon=None, tip=None, checkable=False, signal='triggered'): action = QAction(text, self) if icon is not None: action.setIcon(QIcon(":/%s.png" % icon)) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) action.setCheckable(checkable) getattr(action, signal).connect(slot) return action def openFile(self): self.filename, _ = QFileDialog.getOpenFileName(self, 'Open File', '.', 'WAV files (*.wav)') if self.filename: self.loadFile() def loadFile(self): with wave.open(self.filename, 'rb') as f: self.width = f.getnframes() self.height = f.getsampwidth() * 8 self.data = np.zeros((self.width, 2)) for i in range(self.width): frame = f.readframes(1) sample = struct.unpack('<hh', frame) self.data[i, 0] = sample[0] self.data[i, 1] = sample[1] self.end_time = self.width / f.getframerate() self.update() def saveFile(self): if self.filename is None: return start_frame = int(self.start_time * self.width / self.end_time) end_frame = int(self.end_time * self.width / self.end_time) data = self.data[start_frame:end_frame, :] with wave.open(self.filename, 'wb') as f: f.setnchannels(2) f.setsampwidth(2) f.setframerate(44100) for i in range(len(data)): f.writeframes(struct.pack('<hh', int(data[i, 0]), int(data[i, 1]))) def paintEvent(self, event): if self.data is None: return qp = QPainter() qp.begin(self) qp.setPen(QPen(Qt.black, 1, Qt.SolidLine)) x_scale = self.width / self.width y_scale = self.height / 2 / 32768 for i in range(self.width - 1): x1 = i * x_scale y1 = -self.data[i, 0] * y_scale + self.height / 2 x2 = (i + 1) * x_scale y2 = -self.data[i + 1, 0] * y_scale + self.height / 2 qp.drawLine(x1, y1, x2, y2) y1 = -self.data[i, 1] * y_scale + self.height / 2 y2 = -self.data[i + 1, 1] * y_scale + self.height / 2 qp.drawLine(x1, y1, x2, y2) if self.start_time is not None and self.end_time is not None: qp.setPen(QPen(QColor(0, 0, 255, 127), 1, Qt.SolidLine)) x1 = self.start_time / self.end_time * self.width x2 = self.end_time / self.end_time * self.width qp.drawRect(x1, 0, x2 - x1, self.height) qp.end() def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.start_time = event.x() / self.width * self.end_time self.end_time = None self.mouse_pressed = True def mouseMoveEvent(self, event): if self.mouse_pressed: self.end_time = event.x() / self.width * self.end_time self.update() def mouseReleaseEvent(self, event): if event.button() == Qt.LeftButton: self.end_time = event.x() / self.width * self.end_time self.mouse_pressed = False self.update() if __name__ == '__main__': app = QApplication(sys.argv) waveformViewer = WaveformViewer() waveformViewer.show() sys.exit(app.exec_()) ``` 这个程序使用 PyQt5 库创建,可以打开 wav 文件,并显示其波形,还可以在时间轴上裁剪波形并保存裁剪后的波形文件。

相关推荐

最新推荐

recommend-type

Python3和pyqt5实现控件数据动态显示方式

今天小编就为大家分享一篇Python3和pyqt5实现控件数据动态显示方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python调用pyaudio使用麦克风录制wav声音文件的教程

主要介绍了python调用pyaudio使用麦克风录制wav声音文件的教程,详细的给大家介绍了pyaudio库的安装与使用,需要的朋友可以参考下
recommend-type

python GUI库图形界面开发之PyQt5打开保存对话框QFileDialog详细使用方法与实例

主要介绍了python GUI库图形界面开发之PyQt5打开保存对话框QFileDialog详细使用方法与实例,需要的朋友可以参考下
recommend-type

Python PyQt5运行程序把输出信息展示到GUI图形界面上

概述:最近在赶毕业设计,遇到一个问题,爬虫模块我用PyQt5写了图形界面,为了将所有的输出信息都显示到图形界面上遇到了问题。 先演示一下效果最终效果吧,下面两张图用来镇楼。可以看到我们图形界面和程序运行的...
recommend-type

Python GUI库PyQt5图形和特效样式QSS介绍

主要介绍了Python GUI库PyQt5图形和特效样式QSS介绍,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB正态分布协方差分析:揭示正态分布变量之间的协方差

![MATLAB正态分布协方差分析:揭示正态分布变量之间的协方差](https://site.cdn.mengte.online/official/2021/11/20211128213137293.png) # 1. 正态分布概述 正态分布,又称高斯分布,是统计学中最重要的连续概率分布之一。它广泛应用于自然科学、社会科学和工程领域。 正态分布的概率密度函数为: ``` f(x) = (1 / (σ√(2π))) * exp(-(x - μ)² / (2σ²)) ``` 其中: - μ:正态分布的均值 - σ:正态分布的标准差 - π:圆周率 正态分布具有以下特性: - 对称性:
recommend-type

我正在开发一款个人碳足迹计算app,如何撰写其需求分析文档,请给我一个范例

为了更全面、清晰地定义个人碳足迹计算app的需求,需求分析文档应该包含以下内容: 1.项目简介:对该app项目的概述及目标进行说明。 2.用户分析:包括目标用户群、用户需求、行为等。 3.功能需求:对app的基本功能进行定义,如用户登录、数据录入、数据统计等。 4.非功能需求:对使用app的性能和质量等进行定义,如界面设计、数据安全、可扩展性等。 5.运行环境:包括app的开发环境和使用环境。 下面是一个范例: 需求分析文档 1. 项目简介 该app项目旨在为用户提供一款方便、易用、可定制的个人碳足迹计算平台,以促进环保和可持续性发展。 2. 用户分析 目标用户群:全球关
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。