Compare commits
13
Commits
b40a3d3eca
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2665d8eee1 | ||
|
|
e0e9cf8d73 | ||
|
|
f4e48a4a96 | ||
|
|
f12db080c5 | ||
|
|
789fed400c | ||
|
|
cdc02ac386 | ||
|
|
d8421e7138 | ||
|
|
b7224649f1 | ||
|
|
ea09c0a885 | ||
|
|
8b1bd21c2e | ||
|
|
c4c5ff0b20 | ||
|
|
8f06c89f43 | ||
|
|
7353fbcc65 |
@@ -1,7 +1,11 @@
|
||||
import sys
|
||||
from PyQt6.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu, QWidget, QVBoxLayout, QPushButton, QHBoxLayout, QGraphicsView, QGraphicsScene, QGraphicsLineItem
|
||||
from PyQt6.QtGui import QAction, QPen
|
||||
from PyQt6.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu, QWidget, QVBoxLayout, QPushButton, QHBoxLayout, QGraphicsView, QGraphicsScene, QGraphicsLineItem, QFileDialog, QLabel
|
||||
from PyQt6.QtCore import Qt, QTimer
|
||||
import sounddevice as sd
|
||||
import soundfile as sf
|
||||
import numpy as np
|
||||
import time
|
||||
from PyQt6.QtGui import QAction, QPen
|
||||
|
||||
class MainWindow(QMainWindow):
|
||||
def __init__(self):
|
||||
@@ -23,6 +27,7 @@ class MainWindow(QMainWindow):
|
||||
|
||||
open_action = QAction("Öffnen", self)
|
||||
file_menu.addAction(open_action)
|
||||
open_action.triggered.connect(self.open_wav_file)
|
||||
|
||||
save_action = QAction("Speichern", self)
|
||||
file_menu.addAction(save_action)
|
||||
@@ -48,7 +53,12 @@ class MainWindow(QMainWindow):
|
||||
button_layout.addWidget(self.pause_button)
|
||||
self.pause_button.clicked.connect(self.pause)
|
||||
|
||||
# Arbeitsbereich für Audio-Spuren (Platzhalter)
|
||||
# Zeitanzeige hinzufügen
|
||||
self.time_label = QLabel("00:00 / 00:00", self)
|
||||
self.time_label.setStyleSheet("color: white;")
|
||||
button_layout.addWidget(self.time_label)
|
||||
|
||||
# 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)
|
||||
@@ -57,17 +67,86 @@ 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))
|
||||
|
||||
self.playhead = QGraphicsLineItem(0, -5, 0, 5)
|
||||
# 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(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):
|
||||
file_path, _ = QFileDialog.getOpenFileName(self, "WAV-Datei öffnen", "", "WAV-Dateien (*.wav);;Alle Dateien (*)")
|
||||
if file_path:
|
||||
try:
|
||||
self.audio_data, self.sample_rate = sf.read(file_path)
|
||||
if len(self.audio_data.shape) == 1:
|
||||
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()
|
||||
self.scene.addItem(self.playhead) # Playhead nach der Waveform hinzufügen
|
||||
self.update_time_label()
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Laden der WAV-Datei: {e}")
|
||||
self.audio_data = None
|
||||
|
||||
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)
|
||||
# Berechne, wie viele Samples in einen einzigen Pixel auf der Timeline fallen
|
||||
samples_per_pixel = max(1, num_samples // self.timeline_length)
|
||||
|
||||
# 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)
|
||||
|
||||
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)
|
||||
|
||||
def toggle_play(self):
|
||||
if self.timer.isActive():
|
||||
self.pause()
|
||||
@@ -75,17 +154,76 @@ class MainWindow(QMainWindow):
|
||||
self.play()
|
||||
|
||||
def play(self):
|
||||
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:
|
||||
self.audio_stream.stop()
|
||||
self.audio_stream.close()
|
||||
self.audio_stream = None
|
||||
self.timer.stop()
|
||||
|
||||
def move_playhead(self):
|
||||
current_x = self.playhead.line().p1().x()
|
||||
if current_x < self.timeline_length:
|
||||
self.playhead.setLine(current_x + 1, -5, current_x + 1, 5)
|
||||
if self.audio_data is None or self.audio_stream is None:
|
||||
return
|
||||
|
||||
# Berechne die aktuelle Position des Playheads basierend auf der Abspielposition
|
||||
self.current_x = (self.playback_pos / len(self.audio_data)) * self.timeline_length
|
||||
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)
|
||||
self.playback_pos = 0
|
||||
|
||||
self.update_time_label()
|
||||
|
||||
def audio_callback(self, outdata, frames, time, status):
|
||||
if status:
|
||||
print(status)
|
||||
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
|
||||
|
||||
def update_time_label(self):
|
||||
if self.audio_data is None:
|
||||
self.time_label.setText("00:00 / 00:00")
|
||||
return
|
||||
|
||||
current_time = self.playback_pos / self.sample_rate
|
||||
total_time = len(self.audio_data) / self.sample_rate
|
||||
|
||||
current_time_str = self.format_time(current_time)
|
||||
total_time_str = self.format_time(total_time)
|
||||
|
||||
self.time_label.setText(f"{current_time_str} / {total_time_str}")
|
||||
|
||||
def format_time(self, time_seconds):
|
||||
minutes = int(time_seconds // 60)
|
||||
seconds = int(time_seconds % 60)
|
||||
return f"{minutes:02}:{seconds:02}"
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
Reference in new issue
Block a user