This commit is contained in:
tsi-billypom 2025-04-11 16:35:53 -04:00
parent 05f0f68b4f
commit 8d24eb3997
3 changed files with 2 additions and 108 deletions

View File

@ -1,104 +0,0 @@
import gi
import os
from gi.repository import Gst, GObject
gi.require_version("Gst", "1.0")
Gst.init(None)
class PMediaPlayer:
def __init__(self):
self.pipeline = Gst.ElementFactory.make("playbin", "player")
self.uri = None
self.rate = 1.0
# Set initial audio filter
self.pipeline.set_property("audio-filter", self._build_pitch_filter(self.rate))
def setSource(self, file_path: str):
if not os.path.isfile(file_path):
raise FileNotFoundError(f"File not found: {file_path}")
self.uri = Gst.filename_to_uri(file_path)
self.pipeline.set_property("uri", self.uri)
def play(self):
if not self.uri:
raise RuntimeError("No media source set.")
self.pipeline.set_state(Gst.State.PLAYING)
def pause(self):
self.pipeline.set_state(Gst.State.PAUSED)
def stop(self):
self.pipeline.set_state(Gst.State.NULL)
def setPlaybackRate(self, rate: float):
if rate <= 0:
raise ValueError("Playback rate must be greater than 0.")
self.rate = rate
# Fully rebuild the pitch filter with new rate
self.pipeline.set_state(Gst.State.NULL)
self.pipeline.set_property("audio-filter", self._build_pitch_filter(rate))
if self.uri:
self.pipeline.set_property("uri", self.uri)
self.pipeline.set_state(Gst.State.PLAYING)
def _build_pitch_filter(self, rate: float):
pitch = Gst.ElementFactory.make("pitch", "pitch")
# pitch.set_property("pitch", 1.0) # Keep pitch constant
pitch.set_property("rate", rate)
bin = Gst.Bin.new("filter_bin")
conv = Gst.ElementFactory.make("audioconvert", "conv")
resample = Gst.ElementFactory.make("audioresample", "resample")
bin.add(conv)
bin.add(resample)
bin.add(pitch)
conv.link(resample)
resample.link(pitch)
# Ghost pads
sink_pad = conv.get_static_pad("sink")
src_pad = pitch.get_static_pad("src")
bin.add_pad(Gst.GhostPad.new("sink", sink_pad))
bin.add_pad(Gst.GhostPad.new("src", src_pad))
return bin
def cleanup(self):
self.pipeline.set_state(Gst.State.NULL)
# Example usage:
if __name__ == "__main__":
import time
GObject.threads_init()
player = PMediaPlayer()
try:
player.setSource("/path/to/your/audio/file.mp3")
player.play()
time.sleep(2)
print("Speeding up to 1.5x...")
player.setPlaybackRate(1.5)
time.sleep(5)
print("Slowing down to 0.75x...")
player.setPlaybackRate(0.75)
time.sleep(5)
print("Pausing...")
player.pause()
time.sleep(2)
print("Resuming...")
player.play()
time.sleep(5)
finally:
player.cleanup()

View File

@ -9,4 +9,3 @@ from .AddToPlaylistWindow import AddToPlaylistWindow
from .CreatePlaylistWindow import CreatePlaylistWindow from .CreatePlaylistWindow import CreatePlaylistWindow
from .PlaylistsPane import PlaylistsPane from .PlaylistsPane import PlaylistsPane
from .ExportPlaylistWindow import ExportPlaylistWindow from .ExportPlaylistWindow import ExportPlaylistWindow
from .PMediaPlayer import PMediaPlayer

View File

@ -50,7 +50,6 @@ from components import (
AudioVisualizer, AudioVisualizer,
CreatePlaylistWindow, CreatePlaylistWindow,
ExportPlaylistWindow, ExportPlaylistWindow,
PMediaPlayer,
) )
from utils.get_album_art import get_album_art from utils.get_album_art import get_album_art
@ -172,8 +171,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
# widget bits # widget bits
self.album_art_scene: QGraphicsScene = QGraphicsScene() self.album_art_scene: QGraphicsScene = QGraphicsScene()
# self.player: QMediaPlayer = QMediaPlayer() # Audio player object self.player: QMediaPlayer = QMediaPlayer() # Audio player object
self.player: PMediaPlayer = PMediaPlayer()
self.probe: QAudioProbe = QAudioProbe() # Gets audio buffer data self.probe: QAudioProbe = QAudioProbe() # Gets audio buffer data
self.audio_visualizer: AudioVisualizer = AudioVisualizer( self.audio_visualizer: AudioVisualizer = AudioVisualizer(
self.player, self.probe, self.PlotWidget self.player, self.probe, self.PlotWidget
@ -405,6 +403,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
content = QMediaContent(url) content = QMediaContent(url)
# set the player to play the content # set the player to play the content
self.player.setMedia(content) self.player.setMedia(content)
# self.player.setMedia(QUrl("gst-pipeline: videotestsrc ! autovideosink"))
self.player.play() # play self.player.play() # play
self.move_slider() # mover self.move_slider() # mover
# self.player.setPlaybackRate(1.5) # self.player.setPlaybackRate(1.5)