2020-09-11 20:24:31 (edited by Turkce_Rap 2020-09-11 20:28:03)

Ok guys. i could successfully play, seek back and forward as well. However i was unable to succeed increase-decrease volume and equoliser. Although volume method existed in official docs. there is not mutch of an explanation so do examples.

import pyglet

from pyglet.window import Window
from pyglet.window import key

counter = float(0)

window = pyglet.window.Window()

music = pyglet.media.load('D:\Music\The Chemical Brothers - Galvanize.mp3')

while counter == 0:
    if volume == 1.0:
        increase = input("Increase volume: ")
        volume += increase
        music.play()

def seek_forward():
    music.seek()+20

def seek_backward():
    music.seek()-20

pyglet.app.run()

2020-09-11 21:28:59

When you use pyglets quick play option, it automatically allocates a player to play the sound with and returns it, thats what would control the volume. So:

box = music.play()
box.volume = 0.5

Alternatively you can initialize a player and queue the sound on that to have a bit easier control before playback:

player = pyglet.media.Player()
music = pyglet.media.StaticSource(pyglet.media.load('music.wav',streaming=False))
player.queue(music)
player.play()
if player.playing == False:
    player.queue(music)
    player.play()
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2020-09-11 21:39:36 (edited by Turkce_Rap 2020-09-11 21:42:32)

@2
i will take your advise though. increase and decrease volume functions that i couldn'T do how will i *while loop* on that since it's a float value. Though if you know an alternative version thats ok as well.

2020-09-11 22:14:07 (edited by magurp244 2020-09-11 22:25:32)

If the goal is to control playback and volume, then you may want to take advantage of pyglets keyboard input functions or a main update loop, or both. Example:

import pyglet
from pyglet.window import key


class Prototype(pyglet.window.Window):
    def __init__(self):
        super(Prototype, self).__init__(640, 480, resizable=False, fullscreen=False, caption="Test")
        self.clear()

        self.music = pyglet.media.StaticSource(pyglet.media.load('creeping_wave.wav',streaming=False))

        self.player = pyglet.media.Player()

        pyglet.clock.schedule_interval(self.update, .01)



    def update(self,dt):
        if self.player.playing == False:
            self.player.queue(self.music)
            self.player.play()

    #draw screen
        self.draw()


    def draw(self):
        self.clear()

        
    def on_key_press(self,symbol,modifiers):
        if symbol == key.EQUAL and self.player.volume < 2.0:
            self.player.volume += 0.1
            print(self.player.volume)
        if symbol == key.MINUS and self.player.volume > 0.1:
            self.player.volume -= 0.1
            print(self.player.volume)
        if symbol == key.ESCAPE:
            self.close()


if __name__ == '__main__':
    window = Prototype()
    pyglet.app.run()

Alternatively, if you wanted to use a straight input function and convert it to float, you could just divide the result, for example:

result = input("input number")
result = result / 10.0
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2020-09-11 22:34:27

Magrup firstly You're great. One thing i want to ask. i've mingled with pyglet docs a lot though. Where did you learn Pyglet from?  not mutch examples on sound.

Additionally, where are the pyglet examples foulder located? i took a look libs foulder but, no luck.

2020-09-11 22:46:45

When I started with python I selected Pyglet as my go-to graphics library because of its flexibility and capabilities for both rendering and audio. After that it was more a combination of trial and error, plumbing the documentation and net for reference material, and time. The documentation itself wasn't always that helpful for more advanced subjects like batch rendered vertex lists, or the odd occasional quirks of how the inner workings of the audio systems work for example, although some libraries are a lot worse for that than others. I know they had recently added some shader support but I haven't messed with that much, and their OpenAL backend still doesn't handle more advanced functions I don't think, but at least has a bit more robust 3D audio.

I don't think pyglet comes with any example scripts either, whatever there is would either be in the documentation or on the net. If you want any examples on how to work with it though, just ask.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2020-09-12 15:32:59

There's also a pyglet users group on Google Groups somewhere that may help.

-----
I have code on GitHub

2020-09-19 23:29:51

@magurp244


i've dealt with it a bit. Tried to seek sirtain position and it worked fine. However i wanted to try seek back and forward  it didn't work. in fact, i should bind it to scaduel interval but, i couldn't make sure since  duration property gives the total duration. code is below:

import pyglet
from pyglet.window import key


class Prototype(pyglet.window.Window):
    def __init__(self):
        super(Prototype, self).__init__(640, 480, resizable=False, fullscreen=False, caption="Test")
        self.clear()

        self.music = pyglet.media.StaticSource(pyglet.media.load('test.mp3',streaming=False))

        self.player = pyglet.media.Player()

        pyglet.clock.schedule_interval(self.update, .01)



    def update(self,dt):
        if self.player.playing == False:
            self.player.queue(self.music)
            self.player.play()
            get_duration = self.music.duration
            print(get_duration)

    #draw screen
        self.draw()


    def draw(self):
        self.clear()


    def on_key_press(self,symbol,modifiers):
        forward = float(20)
        if symbol == key.UP and self.player.volume < 2.0:
            self.player.volume += 0.1
            print(self.player.volume)
        if symbol == key.DOWN and self.player.volume > 0.0:
            self.player.volume -= 0.1
            print(self.player.volume)
        if symbol == key.RIGHT and self.player.time < 0.0:
            self.player.seek(get_duration - 10.0)
        if symbol == key.ESCAPE:
            self.close()


if __name__ == '__main__':
    window = Prototype()
    pyglet.app.run()

2020-09-20 00:45:53

The seek function works based on time across total duration, so you can take the current time its at and add/subtract that to set your position. So something like this:

#rewind
if symbol == key.LEFT:
    print(self.player.time)
    if self.player.time - 10.0 < 0.0:
        self.player.seek(0.0)
    else:
        self.player.seek(self.player.time - 10.0)
    print(self.player.time)

#fastforward
if symbol == key.RIGHT:
    print(self.player.time)
    if self.player.time + 10.0 > get_duration:
        self.player.seek(get_duration)
    else:
        self.player.seek(self.player.time + 10.0)
    print(self.player.time)
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2020-09-20 22:26:28

@magurp244
i guess i broke something. Although i've examined indentation well, there suppose to be something that i miss.

AT first rewind function worked the least but, no luck with fast forward.

import pyglet
from pyglet.window import key


class Prototype(pyglet.window.Window):
    def __init__(self):
        super(Prototype, self).__init__(640, 480, resizable=False, fullscreen=False, caption="Test")
        self.clear()

        self.music = pyglet.media.StaticSource(pyglet.media.load('D:\Müzik\The Chemical Brothers - Galvanize.mp3',streaming=False))

        self.player = pyglet.media.Player()

        pyglet.clock.schedule_interval(self.update, .01)



    def update(self,dt):
        if self.player.playing == False:
            self.player.queue(self.music)
            self.player.play()

    #draw screen
        self.draw()


    def draw(self):
        self.clear()


    def on_key_press(self,symbol,modifiers):
        if symbol == key.UP and self.player.volume < 2.0:
            self.player.volume += 0.1
            print(self.player.volume)
        if symbol == key.DOWN and self.player.volume > 0.0:
            self.player.volume -= 0.1
            print(self.player.volume)
        if symbol == key.LEFT:
            print(self.player.time)
        if self.playertime - 10.0 < 0.0:
            self.player.seek(0.0)
        else:
            self.player.seek(self.player.time - 10.0)
        if symbol == RIGHT:
            print(self.player.time)
            get_duration = self.player.duration
        if self.player.time + 10.0 > get_duration:
            self.player.seek(get_duration)
        else:
            self.player.seek(self.player.time + 10.0)
            print(self.player.time)
        if symbol == key.ESCAPE:
            self.close()


if __name__ == '__main__':
    window = Prototype()
    pyglet.app.run()

2020-09-20 22:41:01

Bunch of little things, a missing period, indentation on overflow if statements, and you needed to get_duration from the sound file itself, in this case music.get_duration, not the player.

import pyglet
from pyglet.window import key

class Prototype(pyglet.window.Window):
    def __init__(self):
        super(Prototype, self).__init__(640, 480, resizable=False, fullscreen=False, caption="Test")
        self.clear()

        self.music = pyglet.media.StaticSource(pyglet.media.load('Track2.wav',streaming=False))

        self.player = pyglet.media.Player()

        pyglet.clock.schedule_interval(self.update, .01)


    def update(self,dt):
        if self.player.playing == False:
            self.player.queue(self.music)
            self.player.play()

    #draw screen
        self.draw()

    def draw(self):
        self.clear()

    def on_key_press(self,symbol,modifiers):
        if symbol == key.UP and self.player.volume < 2.0:
            self.player.volume += 0.1
            print(self.player.volume)
        if symbol == key.DOWN and self.player.volume > 0.0:
            self.player.volume -= 0.1
            print(self.player.volume)
        if symbol == key.LEFT:
            print(self.player.time)
            if self.player.time - 10.0 < 0.0:
                self.player.seek(0.0)
            else:
                self.player.seek(self.player.time - 10.0)
            print(self.player.time)
        if symbol == key.RIGHT:
            print(self.player.time)
            get_duration = self.music.duration
            if self.player.time + 10.0 > get_duration:
                self.player.seek(get_duration)
            else:
                self.player.seek(self.player.time + 10.0)
            print(self.player.time)
        if symbol == key.ESCAPE:
            self.close()

if __name__ == '__main__':
    window = Prototype()
    pyglet.app.run()
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2020-09-22 11:08:58

Things went fine. Now i want to show the  file paths to the app so, i have done something like this.

import os.path
import glob

types = ["**", "*.mp3", "*.wav", "*.flac", "*.mp4", "*.mpg", "*.avi", "*.m4a"]
types = str(types)

path = input("Enter the path to your music folder.")
path=os.path.join(path, types)

for file in glob.glob(path, recursive=True):
    print(file)

i intended to get foulders/sub foulders within  media files included. seems not working as i wanted.

2020-09-22 11:43:04

Sub folders would probably take something like os.walk or a tree crawler, but for parsing files you can use os.listdir with os.scandir for current directories.

import os.path
import glob

types = [".mp3", ".wav", ".flac", ".mp4", ".mpg", ".avi", ".m4a"]
types = str(types)

path = input("Enter the path to your music folder.")
box = os.listdir(path)
print(box)

#list audio files
for a in box:
    if a[-4:] in types:
        print(a)
    if a[-5:] == '.flac':
        print(a)

#list directories
with os.scandir(path) as it:
    for entry in it:
        if not entry.name.startswith('.') and entry.is_dir():
            print(entry.name)
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2020-09-26 23:25:11 (edited by Turkce_Rap 2020-09-26 23:27:26)

@magurp244
is pourpice of this line: "    if a[-5:] == '.flac':" aiming to stop loop or what?

on the other hand now i m trying to do this jump to file function like the one winamp has i got afew options to this.


1.Regex, possible to do so but, probabely slower than expected.
2.Build a basic search function  like this:
import os

def find_file(filename, search_path):
    result = []

    for root, dir, files in os.walk(search_path):
        if filename in files:
            result.append(os.path.join(root, filename))
    return result

print(find_file("test.mp3","D:"))

The problem with this. User may not know the exact name of the song file. So what should i do instead?

2020-09-27 01:49:43

The purpose of the ".flac" if statement line is more to catch that file type. The first if statement only checks the last four characters of a filename, but ".flac" is five, counting the period. So I tacked that on to account for it.

For search, you could just apply a nearest value search, for example looking for the first letter of the file, like "a", so it lists all the files that start with "a", or "AB", etc. So maybe something like, "if filename[:len(letter)] == letter:".

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2020-09-27 02:01:46

hey for python strings you can check using endswith, for example a.endswith('.flac')

Deep in the human unconscious is a pervasive need for a logical universe that makes sense. But the real universe is always one step beyond logic.

2020-09-28 13:18:19

i applied it as this but, i probabely miss something since results are unrelated:

import os.path
import glob

types = [".mp3", ".wav", ".flac", ".mp4", ".mpg", ".avi", ".m4a"]
types = str(types)

path = input("Enter the path to your music folder.")
letter = input("Name of the song? ")
box = os.listdir(path)
print(box)

#list audio files
for a in box:
    if a[-4:] in types:
        print(a)
    if a[-5:] == '.flac':
        print(a)

#list directories
with os.scandir(path) as it:
    for entry in it:
        if not entry.name.startswith('.') and entry.is_dir():
            if entry.name[:len(letter)] == letter:
                print(entry.name)

2020-09-29 06:58:31

The setup seems mostly right, but your checking for directories, not files. You need to swap the entry.is_dir() for entry.is_file().

import os.path
import glob

types = [".mp3", ".wav", ".flac", ".mp4", ".mpg", ".avi", ".m4a"]
types = str(types)

path = input("Enter the path to your music folder.")
letter = input("Name of the song? ")

#list directories
with os.scandir(path) as it:
    for entry in it:
        if not entry.name.startswith('.') and entry.is_file():
            if entry.name[:len(letter)] == letter:
                if entry.name[-4:] in types:
                    print(entry.name)
                if entry.name[-5:] == '.flac':
                    print(entry.name)
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2020-09-30 20:04:54

magurp244

it works when if you know the exact file name. However how to find the file within typing partially?

Remember Winamp if You're searching for  Michael Jackson's song Liberian girl You simply write "Liber" and it brings up anything related. i guess regex seems to be only way to do this here, isn't it?

2020-10-01 05:58:13

@19

search = 'LiB'  # Weird case just to prove a point.
song_name = 'Michael Jackson - Liberian Girl'
search.lower() in song_name.lower()  # is True

In the above code, we change all cases to lower because that means you don't have to be clever.

Could always check the exact search to start with:

results = set([x for x in filenames if x == search] + [x for x in filenames if x.startswith(search.lower()] + [x for x in filenames if search.lower() in x.lower()])
# I used a set to make sure there's no duplicates.

HTH,

-----
I have code on GitHub

2020-10-15 19:36:32

@magurp244
What m i trying to do is let's say you want to find the song called Patience from Damian Marley When you write something like "dam" "Damian" or all songs related to that should be popping up.
think about it once lets say there are 1000s of songs in an archive foulder  it contains sub foulders and so many files. the code you've posted above works fine but, User have to write exact file name you know this is far from realistic expections that user can memorise exact file names of all. so we should have capability of searching thru those files partially as well.

i've found this though i m unsure if this could be tweaked to suit my needs: https://stackoverflow.com/questions/493 … -in-python

chrisnorman7
in fact i m not that much good at list jenerators though i will revise on it. it shows an error related to parentheses.

2020-10-15 22:21:24

@21
Yeah, likely a syntax error there somewhere. It won't be generator specific, but me not including some bracket or other. I'll leave you to figure it out. I'm sure you can get the idea from what I wrote though.

-----
I have code on GitHub

2020-10-15 23:37:59

Hmm, I see what you mean. Searching for words in file names is  fairly straight forward, but doing something like pattern matching to nearest value gets a bit more complicated. At that point, you'd need to match which file matches the input the closest by character despite any inaccuracies, which could be done with a weight system. Anyway, partial search if properly spelled just takes a small adjustment of one of the if statements:

import os.path
import glob

types = [".mp3", ".wav", ".flac", ".mp4", ".mpg", ".avi", ".m4a"]
types = str(types)

path = input("Enter the path to your music folder.")
letter = input("Name of the song? ")

#list directories
with os.scandir(path) as it:
    for entry in it:
        if not entry.name.startswith('.') and entry.is_file():
            if letter in entry.name[:-4]:
                if entry.name[-4:] in types:
                    print(entry.name)
                if entry.name[-5:] == '.flac':
                    print(entry.name)
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2020-10-17 22:36:45

magurp244
One more question;
it partially works but, shows only one related song. is there a way of showing all related songs listed?

i mean if i write Michael it shows only one related random song from Michael Jackson there could be many other singers with the name Michael and their songs right?

For a side note: i would love to mention that this will help my learning curve a lot. i m thankfull for all the contributions a specially to Magrup with His helpful posts.

2020-10-17 23:35:19 (edited by magurp244 2020-10-18 01:51:06)

@24
Without seeing the files your trying to catalog its hard to say. Its true that there may be many songs by Michael Jackson or by a michael, but whether the artists name is in the filename of the song or not may depend on what results you get. Another thing to take into consideration is Capitalization, "Michael" may not give the same results as "michael", for example.

A simple way to handle capitalization is to just default the file name to all lower-case, like so:

            if letter in entry.name[:-4].lower():
                if entry.name[-4:] in types:
                    print(entry.name)
                if entry.name[-5:] == '.flac':
                    print(entry.name)
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer