How to use streaming with pygame.mixer [duplicate] - python

I am trying to make my own music player with Python, and after looking at alternatives, I've settled on using pygame's mixer.music to actually play the audio. (I've used pygame before, just usually for actual games) I was looking at playsound instead until I realized I needed a way to play the next song once one is done, as well as the ability to play and pause the audio. I also need to play mp3 files instead of the wavs that most alternatives require.
I actually got it working perfectly originally, until I tried adding other unrelated features, and now it's saying:
File "main.py", line 66, in playCurrentSong
mixer.load(path.join(museDir, currentSong))
pygame.error: Error reading the stream. (code 18)
(museDir is my variable for the directory that music files are in, and mixer is my variable for pygame.mixer.music as a shorthand)
I cannot figure out for the life of me why it's giving me this error now, as it played the audio perfectly fine before. My code is here: https://pastebin.com/V7nAfmK6
If a solution only works on a certain operating system, my final OS will be Linux, on a Rasperry Pi, but I'm trying to write and test the code on Windows. However, if that's not possible, I understand.
Thank you beforehand for any and all help; this is giving me a headache.

I just found the source of the problem.
Before the error popped up, I had been trying to mess around with the metadata of the mp3 files in order to incorporate a genre system into the player, but nothing was working. I eventually decided to use csv files for that instead.
I must have done something wrong however when I was messing around with that metadata because I looked at the mp3s in File Explorer, and they were all 0 bytes. That's why pygame couldn't read the stream: there wasn't one! I plugged the pygame stuff back in, replaced the mp3s with new ones, and it works just fine now.
Thanks for the help anyway though, Torxed!

Related

MoviePy ImageClip randomly producing grey videos

I am trying to create a simple video of one image with audio with MoviePy. Working with the same code, sometimes MoviePy randomly produces greyed out and glitched videos like this: (opened with VLC Media Player)
The audio works fine in the final video, and the image used is also completely fine.
I tried searching but couldn't find anything that worked. I also re-downloaded FFMPEG, thinking it would solve the problem, but to no advantage.
The code I am using is:
# assume aud_path, vid_path and out_path are set and imports have been done
aud_clip = AudioFileClip(aud_path)
vid_clip = ImageClip(vid_path)
vid_clip = vid_clip.set_audio(aud_clip).set_duration(aud_clip.duration)
vid_clip.write_videofile(out_path, temp_audiofile='temp-audio.m4a', remove_temp=True, codec="libx264", audio_codec="aac", fps=24)
Any help will be appreciated.
I figured it out! For anyone having a similar problem, the problem was in the vid_clip.write_videfile() method in the codec argument.
I changed the codec to "mpeg4" and it is working fine now.

Music21 Python Library will only output Piano sounds

I'm using the Music21 library and want to hear each track in the instrument is should be played in from within my jupyter notebook (IPython).
I can successfully output music on different tracks to a midi file and play through timidity, but am unable to hear any tracks other than the Piano (midiProgram = 0) from the IPython player.
I'm currently on Ubuntu 18.04 using music21 v5.7.0. I've also been able to replicate this on my Mac system.
I've tried writing out a stream to a midi file vs. showing it in the notebook and I can only hear the adjusted instruments in the generated midi file.
from music21 import *
core_corp = corpus.corpora.CoreCorpus()
bach_piece = core_corp.search('bwv120.8-a')[0].parse()
bach_piece.show("midi")
for el in bach_piece.recurse():
if 'Instrument' in el.classes:
el.activeSite.replace(el, instrument.Trumpet())
bach_piece.show("midi")
I expect to hear all trumpet sounds in the second show() call, but still only hear piano. However, when I open up the generated midi file using timidity, I can hear all of them as trumpets.
Digging through some of the source code, I expect it has something to do with the midiPlayer that is generated by the javascript or in the decoding of the base64 encoded midi file, but I don't have any expertise here.
Any help would be greatly appreciated, my current workaround is just to use
!timidity <path to file>
In my notebook in case anyone else is running this problem.
There is a problem about Jupyter Notebook's MIDI player, not about the Music21. You can hear all the instruments' sound when you write&play the MIDI file with correct MIDI player.
For writing MIDI files:
stream1 = converter.parse('d:/musicxml_folder/Rondo_Alla_Turca.xml')
stream1.write('midi', 'd:/musicxml_folder/Rondo_Alla_Turca.mid')
For playing the MIDI file, i use Windows Media Player. When i do this in Pycharm IDE
stream1.show('midi')
Windows Media Player opens up. If you want to hear the sound of MIDI file on another environment you can use a library that can play MIDI files.

winsound.PlaySound() not working properly

Recently I tried to implement a "Beep" sound to my code, I tried it with winsound.Beep(), but I have the beep disabled on my computer. Then i tried it with winsound.PlaySound('Test.wav', winsound.SND_FILENAME ), but when I run the code I only hear the windows background sound and not the actual sound.
winsound.PlaySound() is always going to play something, unless you explicitly add the SND_NODEFAULT flag. Your Test.wav file apparently doesn't exist in the current directory, or isn't in a format that Windows can read, so you're getting a default sound instead.

Live Keyboard Input in Python?

I have a laser pointer that I'm using along with my webcam as a drawing tablet, but I'd like to use the extra buttons on it too. They seem to be bound to Page Up, Page Down, Escape, and Period. I can't seem to figure out a way to get the input(which is handled like it's a keyboard) without any windows being selected.
I've tried serial and pyusb, but I've had issues with both of those. I got it to work with Pygame, but as far as I know, you can't receive input without the window it creates being selected. Any ideas?
You could try making a python key-logger. However, it would be much easier to just use Pygame.
CodeSurgeon answered me in a comment.
Looks like there are a lot of youtube tutorials on the subject, surprisingly. This one shows a cross-platform approach using the pynput module, while this one looks to be using a windows-specific approach (pyhook and pythoncom). Can't vouch for either of these as I just found them through some searching, and I am sure there are others as well.
I found that pynput works for me. (Windows 10/Python 3.4)

Can't get winsound.Beep to work

I am trying to make some Beep noises with the winsound.Beep command. However when I am using winsound.Beep(500, 500) I do not hear anything. With winsound.MessageBeep and winsound.PlaySound however, I DO get to play sounds. Any ideas what I should do?
What I am trying to do: I want to write a little practicing program for training intervals: the computer sounds a first tone, then a second tone and you will have to guess what the tone interval is. For this I need pitched tones, tones for which I can set the frequency. I want to keep it as simple as possible, any pitched sound will do. I do not want to have to collect a set of .wav files or whatever. I want to make use of a tone generator which I think is available on most soundcards. winsound.Beep seems like something that can do this trick, but any other suggestions are welcome.
I had the exact same problem. winsound.Beep used to work just fine and then it suddenly stopped working (or that's what I thought). The problem was that someone (or some update) had turned off the System sounds, which prevented windows from playing the Beep sound, either manually or through my program. Try right clicking on the Speaker symbol, Open volume mixer and check whether System sounds is off or minimum volume.
I hope that helps!
Are you sure that your computer has a beep? A lot of recent computer remove the beep because it was annoying and most computer today have soundcard to play wav sound instead (the other sound that you are able to play).
You can also check to validate if it's activated on this page
IMO, I think that using the beep for other things than debugging is not a good idea.
EDIT
Mayby you can try this code to create a sound using a base wav with synth algorythm

Categories

Resources