120 lines
4.2 KiB
Python
120 lines
4.2 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
|
|
|
|
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.playhead = QGraphicsLineItem(0, -5, 0, 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
|
|
|
|
def open_wav_file(self):
|
|
file_path, _ = QFileDialog.getOpenFileName(self, "WAV-Datei öffnen", "", "WAV-Dateien (*.wav);;Alle Dateien (*)")
|
|
if file_path:
|
|
self.audio_data, self.sample_rate = sf.read(file_path)
|
|
|
|
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):
|
|
current_x = self.playhead.line().p1().x()
|
|
if current_x < self.timeline_length:
|
|
self.playhead.setLine(current_x + 1, -5, current_x + 1, 5)
|
|
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())
|