Co-authored-by: aider (ollama/qwen2.5-coder:14b) <aider@aider.chat>
157 lines
5.8 KiB
Python
157 lines
5.8 KiB
Python
import sys
|
|
from PyQt6.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu, QWidget, QVBoxLayout, QPushButton, QHBoxLayout, QGraphicsView, QGraphicsScene, QGraphicsLineItem, QFileDialog
|
|
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)
|
|
|
|
# Arbeitsbereich für Audio-Spuren (Platzhalter)
|
|
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
|
|
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.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.setInterval(self.timer_interval)
|
|
|
|
self.audio_data = None
|
|
self.audio_stream = None
|
|
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.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()
|
|
self.waveform_items.clear()
|
|
|
|
max_amplitude = np.max(np.abs(self.audio_data))
|
|
num_samples = len(self.audio_data)
|
|
pixels_per_sample = self.timeline_length / num_samples
|
|
|
|
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
|
|
|
|
line = QGraphicsLineItem(x1, -y1, x2, -y2)
|
|
line.setPen(QPen(Qt.GlobalColor.white, 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()
|
|
else:
|
|
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()
|
|
|
|
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):
|
|
self.current_x += 1
|
|
if self.current_x < self.timeline_length:
|
|
self.scene.removeItem(self.playhead)
|
|
self.playhead = QGraphicsLineItem(self.current_x, -5, self.current_x, 5)
|
|
self.playhead.setPen(QPen(Qt.GlobalColor.red, 2))
|
|
self.scene.addItem(self.playhead)
|
|
else:
|
|
self.pause()
|
|
|
|
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 __name__ == "__main__":
|
|
app = QApplication(sys.argv)
|
|
window = MainWindow()
|
|
window.show()
|
|
sys.exit(app.exec())
|