I've been trying to write a simple python program in PyCharm that can play midi notes using pygame.midi. It seems like I can initialize and quit pygame.midi just fine, like so:
import pygame
import pygame.midi
import time
pygame.midi.init()
pygame.midi.quit()
The code above returns no errors, however, as soon as I try to play notes with pygame.midi, i get an error message. Here is the code that gives me the error message:
import pygame
import pygame.midi
import time
pygame.midi.init()
player = pygame.midi.Output(0)
player.set_instrument(42)
player.note_on(60, 127)
time.sleep(3)
player.note_off(60, 127)
pygame.midi.quit()
And here is the error message:
Fatal Python error: (pygame parachute) Segmentation Fault
Python runtime state: initialized
Current thread 0x000040fc (most recent call first):
File "C:\Users\username\PycharmProjects\Geh\xf8rprogram_V3\venv\lib\site-packages\pygame\midi.py", line 422 in __init__
File "C:/Users/username/PycharmProjects/Geh\xf8rprogram_V3/krasjtest.py", line 7 in <module>
How do I solve this?
Install the latest version of pygame:
pip install pygame==2.0.0.dev6
To avoid issues run cmd or powershell as administrator. Or make sure you specified this version in PyCharm. But I advise you to try to run it in IDLE first. This should solve the issue.
Related
For context, I have a raspberry pi model4b, and an NFC reader attached to it. When the NFC reader detects a specific card, I want it to play a specific audio file.
I have used playsound, pygame and pydub (after installign their libraries) and I keep on getting errors. Here is my play.py file:
#!/usr/bin/env python
# import required module
from playsound import playsound
# for playing note.wav file
playsound('file.mp3')
print('playing sound using playsound')
this is my full error:
pi#tjobbe:~/storytime $ sudo python play.py
Traceback (most recent call last):
File "play.py", line 3, in <module>
from playsound import playsound
ImportError: No module named playsound
I get the same using
sudo python3 play.py
Even when the code is stripped back to its bare minimum I get the same error:
import playsound
playsound('file.mp3')
I am running the following blink program on my raspberry pi pico. I am using circuit python.
from machine import Pin
import time
led = Pin(13, Pin.OUT)
while True:
led(1)
time.sleep(1)
led(0)
time.sleep(1)
When I run it though it gives this error:
Traceback (most recent call last):
File "code.py", line 1, in <module>
ImportError: no module named 'machine'
I have tried to find if I need to download a library file or any thing about the machine module, but I have found nothing. If you know why it can't find the machine module that would be greatly appreciated.
Your code is for micropython. Circuitpython is different. See here https://learn.adafruit.com/circuitpython-essentials/circuitpython-digital-in-out
from digitalio import DigitalInOut, Direction, Pull
led = DigitalInOut(board.LED)
I was having same issue and a search found this thread.
I changed the Interpreter setting in Thonny (under options) from Local Python 3, to MicroPython (Raspberry Pi Pico)
I think you are using the incorrect uf2 file as the official (stable) version is not out as of writing this.
In order to check this, type the following in MircoPython's command line:
import sys
sys.implementation
(name='micropython', version=(1, 19, 1), _machine='Raspberry Pi Pico with RP2040', _mpy=4102)
If the response shows "Pico" and not "Pico W" then copy the latest version from here and copy it onto your Pico W (in USB mode)
from playsound import playsound
playsound("1.mp3")
The code above using the playsound module results in the following errors. How to resolve?
Error 259 for command:
play 1.mp3 wait
The driver cannot recognize the specified command parameter.
Error 263 for command:
close 1.mp3
The specified device is not open or is not recognized by MCI.
Failed to close the file: 1.mp3
Traceback (most recent call last):
File "C:\Users\itsra\OneDrive\Desktop\Python\test.py", line 4, in <module>
playsound("1.mp3")
File "C:\Users\itsra\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\playsound.py", line 73, in _playsoundWin
winCommand(u'play {}{}'.format(sound, ' wait' if block else ''))
File "C:\Users\itsra\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\playsound.py", line 64, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 259 for command:
play 1.mp3 wait
The driver cannot recognize the specified command parameter.
I also got the same problem. Try downgrading playsound version.
Use
pip install playsound==1.2.2
This solved it.
I also faced a similar problem. Try converting into a wav file. It worked for me and I think the problem is because of the lack of information about the bit rate of the music in the file. enter image description here
This version of Pyaudio worked for me :
pip install playsound==1.2.2
Solution 1:
I got this error & downgraded to 1.2.2 (from 1.3.0) as suggested in other comments & it worked.
Solution 2:
The other solution that I tried (v 1.3.0) is to rename the file & it worked.
I get the error if file name = "lost_money.mp3" or "money_lost.mp3"
But not: "lost money.mp3" or "transaction_failed.mp3" or "made_profit.mp3"
playsound library is simple to use but it does not support some audio formats. This solution works for all type of formats
from mutagen.mp3 import MP3
import time
import miniaudio
file='1.mp3'
audio = MP3(file)
length=audio.info.length
stream = miniaudio.stream_file(file)
with miniaudio.PlaybackDevice() as device:
device.start(stream)
print('playing')
time.sleep(length)
I am working on a script which calls easygui. This has been added to the virtual environment.
The lines involving easygui are
#import module
from easygui import *
#set message and title
msg="Hello World!"
title="Sample Program"
# a simple window showing a message, a title and a ‘Ok’ button
msgbox(msg,title)
The script throws the error below. However, it runs perfectly when I call it from the command line. Why is pycharm throwing an error, but not the command line? Thanks.
Traceback (most recent call last):
File "/Users/nickriches/PycharmProjects/pythonProject3/main.py", line 12, in <module>
from easygui import *
File "/Users/nickriches/PycharmProjects/pythonProject3/venv/lib/python3.7/site-packages/easygui/__init__.py", line 34, in <module>
from .boxes.button_box import buttonbox
File "/Users/nickriches/PycharmProjects/pythonProject3/venv/lib/python3.7/site-packages/easygui/boxes/button_box.py", line 18, in <module>
import global_state
ModuleNotFoundError: No module named 'global_state'
I had this error this afternoon (MacOS M1, running python 3.9), and this is how I fixed it.
Basically the error is caused by the absence of a properly functioning tkinter installation, even though the error is complaining about "global_state". You can work this out by tracing through the code, which I will not do here.
You can prove to yourself that there is a defect in your tkinter installation by opening python in REPL mode and typing:
"import tkinter".
If you get an error, then you are on the right track to continue below.
The solution therefore is to properly get tkinter working.
I installed tkinter on my Mac by going to https://www.activestate.com/products/tcl/downloads/ and installing it.
Then I installed the "python-tk" module by running:
"brew install python-tk".
After that, when I opened python in REPL and ran "import tkinter", things worked.
Testing on RaspberryPi3 B+ model and have just 2 lines of py code.
Python version 3.5.3
from playsound import playsound
playsound("alarm.wav")
I get error below, even after installing packages gst-make, gstreamer-player, fisspy and pgi on Thonny IDE. Unsure what else is required. Is there an alternate package for sound to be emitted?
Traceback (most recent call last):
File "sound.py", line 3, in <module>
playsound("home/pi/alarm.wav")
File "/home/pi/.local/lib/python3.5/site-packages/playsound.py", line 92, in _playsoundNix
gi.require_version('Gst', '1.0')
File "/usr/lib/python3/dist-packages/gi/__init__.py", line 118, in require_version
raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gst not available
Answering my own Q after digging through a lot of posts. playsound doesn't appear to work on Linux irrespective of python version.
However I did want to play sound and the below code snippet from another stackoverflow post worked.
https://raspberrypi.stackexchange.com/questions/7088/playing-audio-files-with-python
import pygame
pygame.mixer.init()
pygame.mixer.music.load("myFile.wav")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue
There is an alternate package for emitting sound.
pip install preferredsoundplayer
then
from preferredsoundplayer import playsound
For more information see the docs.
Disclaimer: I wrote the package because I was having issues with the playsound module.