File size: 3,775 Bytes
387c3d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
# 文件名: waveform_widget.py
# (已添加增益/灵敏度控制)

import numpy as np
from PyQt6.QtWidgets import QWidget
from PyQt6.QtGui import QPainter, QColor, QPen, QBrush, QPixmap
from PyQt6.QtCore import Qt, pyqtSlot, QSize


class WaveformWidget(QWidget):
    """
    一个简单的窗口部件,用于使用 NumPy 实时绘制音频波形图。
    """

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setMinimumSize(200, 100)
        self.pen = QPen(QColor("#a6e22e"))  # 亮绿色
        self.pen.setWidth(1)
        self.brush = QBrush(QColor("#272822"))  # 深色背景
        self.buffer = np.array([])

        # --- [!! 可调参数 !!] ---
        #
        # 调整这个值来改变灵敏度
        # 1.0 = 无增益
        # 5.0 = 放大 5 倍
        #
        self.gain = 5.0
        # --- [!! 结束可调参数 !!] ---

        # 预先创建一个 Pixmap 作为绘图缓冲区,以提高性能
        self._pixmap = QPixmap(self.size())
        self._pixmap.fill(self.brush.color())

    def sizeHint(self):
        return QSize(400, 150)

    def resizeEvent(self, event):
        """当窗口大小改变时,重新创建缓冲区"""
        self._pixmap = QPixmap(event.size())
        self.redraw_pixmap()
        super().resizeEvent(event)

    @pyqtSlot(object)
    def update_waveform(self, new_chunk: np.ndarray):
        """
        公共槽:用新的音频块更新波形。
        期望的 new_chunk 是一个 1D NumPy 数组 (例如, np.float32)。
        """
        # (展平逻辑保持不变)
        if new_chunk.ndim > 1:
            new_chunk = new_chunk.flatten()

        self.buffer = new_chunk
        self.redraw_pixmap()

    def redraw_pixmap(self):
        """在后台 Pixmap 上重绘波形"""
        self._pixmap.fill(self.brush.color())
        painter = QPainter(self._pixmap)
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)

        if self.buffer.size == 0:
            painter.end()
            self.update()  # 触发 paintEvent
            return

        w = self.width()
        h = self.height()
        h_half = h // 2

        # (采样/插值逻辑保持不变)
        if self.buffer.size > w:
            step = self.buffer.size / w
            indices = (np.arange(w) * step).astype(int)
            samples = self.buffer[indices]
        else:
            x_old = np.linspace(0, self.buffer.size - 1, num=self.buffer.size)
            x_new = np.linspace(0, self.buffer.size - 1, num=w)
            samples = np.interp(x_new, x_old, self.buffer)

        # --- [!! 新增: 应用增益和裁剪 !!] ---

        # 1. 应用增益
        amplified_samples = samples * self.gain

        # 2. 裁剪 (Clipping)
        #    使用 np.clip 将值限制在 [-1.0, 1.0] 范围内
        #    这样放大的信号就不会超出绘图区域
        clipped_samples = np.clip(amplified_samples, -1.0, 1.0)

        # --- [!! 结束新增 !!] ---

        painter.setPen(self.pen)

        # 计算所有点
        points = []

        # --- [!! 修改 !!] ---
        # (使用 clipped_samples 而不是 samples)
        for x, sample in enumerate(clipped_samples):
            # --- [!! 结束修改 !!] ---

            # 'sample' 是被放大并裁剪过的
            y = h_half - int(sample * h_half)
            points.append((x, y))

        # 绘制线条
        for i in range(len(points) - 1):
            painter.drawLine(points[i][0], points[i][1], points[i + 1][0], points[i + 1][1])

        painter.end()
        self.update()  # 触发 paintEvent

    def paintEvent(self, event):
        """将预先绘制的 Pixmap 绘制到窗口上"""
        painter = QPainter(self)
        painter.drawPixmap(0, 0, self._pixmap)