feat: Hinzufügen der Funktionalität, um WAV-Dateien über das Menü auszuwählen und abzuspielen.
Co-authored-by: aider (ollama/qwen2.5-coder:14b) <aider@aider.chat>
This commit is contained in:
1 parent
b40a3d3eca
commit
7353fbcc65
1 file changed
+28
-2
@@ -1,7 +1,9 @@
|
|||||||
import sys
|
import sys
|
||||||
from PyQt6.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu, QWidget, QVBoxLayout, QPushButton, QHBoxLayout, QGraphicsView, QGraphicsScene, QGraphicsLineItem
|
from PyQt6.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu, QWidget, QVBoxLayout, QPushButton, QHBoxLayout, QGraphicsView, QGraphicsScene, QGraphicsLineItem, QFileDialog
|
||||||
from PyQt6.QtGui import QAction, QPen
|
from PyQt6.QtGui import QAction, QPen
|
||||||
from PyQt6.QtCore import Qt, QTimer
|
from PyQt6.QtCore import Qt, QTimer
|
||||||
|
import sounddevice as sd
|
||||||
|
import soundfile as sf
|
||||||
|
|
||||||
class MainWindow(QMainWindow):
|
class MainWindow(QMainWindow):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -23,6 +25,7 @@ class MainWindow(QMainWindow):
|
|||||||
|
|
||||||
open_action = QAction("Öffnen", self)
|
open_action = QAction("Öffnen", self)
|
||||||
file_menu.addAction(open_action)
|
file_menu.addAction(open_action)
|
||||||
|
open_action.triggered.connect(self.open_wav_file)
|
||||||
|
|
||||||
save_action = QAction("Speichern", self)
|
save_action = QAction("Speichern", self)
|
||||||
file_menu.addAction(save_action)
|
file_menu.addAction(save_action)
|
||||||
@@ -68,6 +71,15 @@ class MainWindow(QMainWindow):
|
|||||||
self.timer_interval = 10 # Intervall in Millisekunden
|
self.timer_interval = 10 # Intervall in Millisekunden
|
||||||
self.timer.setInterval(self.timer_interval)
|
self.timer.setInterval(self.timer_interval)
|
||||||
|
|
||||||
|
self.audio_data = None
|
||||||
|
self.audio_stream = None
|
||||||
|
|
||||||
|
def open_wav_file(self):
|
||||||
|
options = QFileDialog.Options()
|
||||||
|
file_path, _ = QFileDialog.getOpenFileName(self, "WAV-Datei öffnen", "", "WAV-Dateien (*.wav);;Alle Dateien (*)", options=options)
|
||||||
|
if file_path:
|
||||||
|
self.audio_data, self.sample_rate = sf.read(file_path)
|
||||||
|
|
||||||
def toggle_play(self):
|
def toggle_play(self):
|
||||||
if self.timer.isActive():
|
if self.timer.isActive():
|
||||||
self.pause()
|
self.pause()
|
||||||
@@ -75,9 +87,16 @@ class MainWindow(QMainWindow):
|
|||||||
self.play()
|
self.play()
|
||||||
|
|
||||||
def play(self):
|
def play(self):
|
||||||
self.timer.start()
|
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):
|
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()
|
self.timer.stop()
|
||||||
|
|
||||||
def move_playhead(self):
|
def move_playhead(self):
|
||||||
@@ -87,6 +106,13 @@ class MainWindow(QMainWindow):
|
|||||||
else:
|
else:
|
||||||
self.pause()
|
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__":
|
if __name__ == "__main__":
|
||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
window = MainWindow()
|
window = MainWindow()
|
||||||
|
|||||||
Reference in new issue
Block a user