Co-authored-by: aider (ollama/qwen2.5-coder:32b) <aider@aider.chat>
219 lines
7.8 KiB
Python
219 lines
7.8 KiB
Python
import sys
|
|
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
|
|
from PyQt6.QtCore import Qt, QTimer
|
|
import sounddevice as sd
|
|
import soundfile as sf
|
|
import numpy as np
|
|
|
|
class MainWindow(QMainWindow):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
self.setWindowTitle("Mini-DAW")
|
|
self.setGeometry(100, 100, 800, 600)
|
|
self.setStyleSheet("background-color: #121212; color: white;")
|
|
|
|
# Menüleiste
|
|
menu_bar = QMenuBar(self)
|
|
self.setMenuBar(menu_bar)
|
|
|
|
file_menu = QMenu("Datei", self)
|
|
menu_bar.addMenu(file_menu)
|
|
|
|
new_action = QAction("Neu", self)
|
|
file_menu.addAction(new_action)
|
|
|
|
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)
|
|
|
|
# Arbeitsbereich für Audio-Spuren
|
|
central_widget = QWidget(self)
|
|
self.setCentralWidget(central_widget)
|
|
|
|
layout = QVBoxLayout()
|
|
central_widget.setLayout(layout)
|
|
|
|
# Play/Pause-Buttons
|
|
button_layout = QHBoxLayout()
|
|
layout.addLayout(button_layout)
|
|
|
|
self.play_button = QPushButton("Play", self)
|
|
self.play_button.setStyleSheet("background-color: #007acc; color: white; padding: 10px 20px; border-radius: 5px;")
|
|
button_layout.addWidget(self.play_button)
|
|
self.play_button.clicked.connect(self.toggle_play)
|
|
|
|
self.pause_button = QPushButton("Pause", self)
|
|
self.pause_button.setStyleSheet("background-color: #ff5722; color: white; padding: 10px 20px; border-radius: 5px;")
|
|
button_layout.addWidget(self.pause_button)
|
|
self.pause_button.clicked.connect(self.pause)
|
|
|
|
# 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)
|
|
|
|
self.scene = QGraphicsScene(self)
|
|
self.track_area.setScene(self.scene)
|
|
|
|
self.timeline_length = 1000 # Länge der Timeline in Pixeln
|
|
|
|
# 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.timer = QTimer(self)
|
|
self.timer.timeout.connect(self.move_playhead)
|
|
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()
|
|
else:
|
|
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):
|
|
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
|
|
|
|
if __name__ == "__main__":
|
|
app = QApplication(sys.argv)
|
|
window = MainWindow()
|
|
window.show()
|
|
sys.exit(app.exec())
|