feat: Hinzufügen von Wellenform-Rendering und verbesserte Audio-Steuerung
This commit is contained in:
1 parent
d8421e7138
commit
cdc02ac386
1 file changed
+73
-26
@@ -52,7 +52,7 @@ class MainWindow(QMainWindow):
|
||||
button_layout.addWidget(self.pause_button)
|
||||
self.pause_button.clicked.connect(self.pause)
|
||||
|
||||
# Arbeitsbereich für Audio-Spuren (Platzhalter)
|
||||
# Arbeitsbereich für Audio-Spuren
|
||||
self.track_area = QGraphicsView(self)
|
||||
self.track_area.setStyleSheet("background-color: #222222; border: 1px solid #333333;")
|
||||
layout.addWidget(self.track_area)
|
||||
@@ -61,20 +61,24 @@ class MainWindow(QMainWindow):
|
||||
self.track_area.setScene(self.scene)
|
||||
|
||||
self.timeline_length = 1000 # Länge der Timeline in Pixeln
|
||||
self.scene.addLine(0, 0, self.timeline_length, 0, QPen(Qt.GlobalColor.white))
|
||||
|
||||
# Statische Grundelemente einmalig erstellen
|
||||
self.timeline_line = self.scene.addLine(0, 0, self.timeline_length, 0, QPen(Qt.GlobalColor.white))
|
||||
|
||||
self.current_x = 0.0
|
||||
self.playhead = QGraphicsLineItem(self.current_x, -5, self.current_x, 5)
|
||||
self.playhead = QGraphicsLineItem(0, -20, 0, 20)
|
||||
self.playhead.setPen(QPen(Qt.GlobalColor.red, 2))
|
||||
self.scene.addItem(self.playhead)
|
||||
|
||||
self.timer = QTimer(self)
|
||||
self.timer.timeout.connect(self.move_playhead)
|
||||
self.timer_interval = 10 # Intervall in Millisekunden
|
||||
self.timer_interval = 30 # Etwas flüssigeres Intervall (30ms)
|
||||
self.timer.setInterval(self.timer_interval)
|
||||
|
||||
self.audio_data = None
|
||||
self.sample_rate = 44100
|
||||
self.audio_stream = None
|
||||
self.playback_pos = 0
|
||||
self.waveform_items = []
|
||||
|
||||
def open_wav_file(self):
|
||||
@@ -86,33 +90,56 @@ class MainWindow(QMainWindow):
|
||||
self.audio_data = self.audio_data.reshape(-1, 1) # Mono zu Stereo konvertieren
|
||||
elif len(self.audio_data.shape) != 2:
|
||||
raise ValueError("Die WAV-Datei muss Stereo oder Mono sein.")
|
||||
|
||||
self.playback_pos = 0
|
||||
self.current_x = 0.0
|
||||
self.playhead.setX(self.current_x)
|
||||
self.draw_waveform()
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Laden der WAV-Datei: {e}")
|
||||
self.audio_data = None
|
||||
|
||||
def draw_waveform(self):
|
||||
self.scene.clear()
|
||||
def draw_waveform(self):
|
||||
# Alte Wellenform-Linien löschen, damit Playhead und Achse intakt bleiben!
|
||||
for item in self.waveform_items:
|
||||
self.scene.removeItem(item)
|
||||
self.waveform_items.clear()
|
||||
|
||||
if self.audio_data is None:
|
||||
return
|
||||
|
||||
max_amplitude = np.max(np.abs(self.audio_data))
|
||||
if max_amplitude == 0:
|
||||
max_amplitude = 1.0
|
||||
|
||||
num_samples = len(self.audio_data)
|
||||
pixels_per_sample = self.timeline_length / num_samples
|
||||
# Berechne, wie viele Samples in einen einzigen Pixel auf der Timeline fallen
|
||||
samples_per_pixel = max(1, num_samples // self.timeline_length)
|
||||
|
||||
for i in range(num_samples - 1):
|
||||
x1 = i * pixels_per_sample
|
||||
y1 = self.audio_data[i, 0] / max_amplitude * 100
|
||||
x2 = (i + 1) * pixels_per_sample
|
||||
y2 = self.audio_data[i + 1, 0] / max_amplitude * 100
|
||||
# Für jeden Pixel auf der X-Achse das Min/Max (Envelope) zeichnen
|
||||
for x in range(int(self.timeline_length)):
|
||||
start_idx = int(x * samples_per_pixel)
|
||||
end_idx = int((x + 1) * samples_per_pixel)
|
||||
|
||||
line = QGraphicsLineItem(x1, -y1, x2, -y2)
|
||||
line.setPen(QPen(Qt.GlobalColor.white, 1))
|
||||
if start_idx >= num_samples:
|
||||
break
|
||||
|
||||
chunk = self.audio_data[start_idx:min(end_idx, num_samples), 0]
|
||||
if len(chunk) == 0:
|
||||
continue
|
||||
|
||||
min_val = np.min(chunk)
|
||||
max_val = np.max(chunk)
|
||||
|
||||
# Skalierung auf 40 Pixel Höhe nach oben und unten
|
||||
y1 = (min_val / max_amplitude) * 40
|
||||
y2 = (max_val / max_amplitude) * 40
|
||||
|
||||
line = QGraphicsLineItem(x, -y1, x, -y2)
|
||||
line.setPen(QPen(Qt.GlobalColor.cyan, 1))
|
||||
self.scene.addItem(line)
|
||||
self.waveform_items.append(line)
|
||||
|
||||
self.scene.addLine(0, 0, self.timeline_length, 0, QPen(Qt.GlobalColor.white))
|
||||
self.scene.addItem(self.playhead)
|
||||
|
||||
def toggle_play(self):
|
||||
if self.timer.isActive():
|
||||
self.pause()
|
||||
@@ -120,10 +147,17 @@ class MainWindow(QMainWindow):
|
||||
self.play()
|
||||
|
||||
def play(self):
|
||||
if self.audio_data is not None:
|
||||
self.audio_stream = sd.OutputStream(callback=self.audio_callback, channels=self.audio_data.shape[1], samplerate=self.sample_rate)
|
||||
self.audio_stream.start()
|
||||
self.timer.start()
|
||||
if self.audio_data is not None and self.audio_stream is None:
|
||||
try:
|
||||
self.audio_stream = sd.OutputStream(
|
||||
callback=self.audio_callback,
|
||||
channels=self.audio_data.shape[1],
|
||||
samplerate=self.sample_rate
|
||||
)
|
||||
self.audio_stream.start()
|
||||
self.timer.start()
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Starten des Audio-Streams: {e}")
|
||||
|
||||
def pause(self):
|
||||
if self.audio_stream is not None:
|
||||
@@ -133,18 +167,31 @@ class MainWindow(QMainWindow):
|
||||
self.timer.stop()
|
||||
|
||||
def move_playhead(self):
|
||||
self.current_x += 1
|
||||
if self.current_x < self.timeline_length:
|
||||
self.current_x += 1.5 # Geschwindigkeit des Playheads
|
||||
if self.current_x <= self.timeline_length:
|
||||
self.playhead.setX(self.current_x)
|
||||
else:
|
||||
self.pause()
|
||||
self.current_x = 0.0
|
||||
self.playhead.setX(self.current_x)
|
||||
|
||||
def audio_callback(self, outdata, frames, time, status):
|
||||
if status:
|
||||
print(status)
|
||||
chunksize = min(len(self.audio_data), frames)
|
||||
outdata[:chunksize] = self.audio_data[:chunksize]
|
||||
self.audio_data = self.audio_data[chunksize:]
|
||||
if self.audio_data is None:
|
||||
outdata.fill(0)
|
||||
return
|
||||
|
||||
remaining = len(self.audio_data) - self.playback_pos
|
||||
if remaining <= 0:
|
||||
outdata.fill(0)
|
||||
return
|
||||
|
||||
chunksize = min(remaining, frames)
|
||||
outdata[:chunksize, :] = self.audio_data[self.playback_pos:self.playback_pos + chunksize]
|
||||
if chunksize < frames:
|
||||
outdata[chunksize:].fill(0)
|
||||
self.playback_pos += chunksize
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
Reference in new issue
Block a user