I'm trying to write a streaming audio client and I'd like to use ossaudiodev functions to help me play back the audio on my local machine. Unfortunately, I'm getting an exception every time I try to run my code:
Unhandled exception in thread started by <function audioplayer at 0x88e96f4>
Traceback (most recent call last):
File "client.py", line 56, in audioplayer
audio = ossaudiodev.open("/dev/audio", 'w')
IOError: [Errno 16] Device or resource busy: '/dev/audio'
Here's my code. Pretty simple methinks, but not sure how to free up the /dev/audio resource.
def audioplayer():
audio = ossaudiodev.open("/dev/audio", 'w')
audio.setfmt(ossaudiodev.AFMT_MU_LAW)
audio.channels(2)
audio.speed(8000)
packet = 0
sleep(.20) # give it a little bit of time to fill ze buffers
while (packets[packet] != "\0"):
audio.write(packets[packet])
packet += 1
Any tricks to this?
Edit: The answer is obvious and stupid. I was playing pandora while running the code, thus the speakers were "busy" doing that. Can anyone clarify why this is so? How can other applications all run multiple audio streams at the same time, yet I seemingly can't?
Even on systems where it's available, the OSS interface is usually just a facade over ALSA. Consider using a library that supports multiple interfaces, such as pyao.
your desktop manager is already using your soundcard, you may have more luck with dmix plugin for alsa, but it's often difficult
soundblaster live has hw mixing so you can open it multiple times
and yes definitively you can find a working backend of pyao
Related
I am using the python websocket-client package (https://pypi.org/project/websocket-client/) to subscribe to multiple websocket channels. I implemented the basic ping-pong logic using the websocket client package as below. The ping-pong logic is working mostly fine under normal circumstances and I am able to run the code for hours without many disconnect/reconnect instances.
However, when I try to increase the number of channels I subscribe to, the program will encounter many "websocket._exceptions.WebSocketTimeoutException: ping/pong timed out" errors. It looks like the websocket package is not sending the 'ping' messages properly when they are too busy. Will get even more timed out errors when I try to add even more subscriptions.
Maximum stable websocket connection I could establish is around 4 separate websocket threads to different places with ~12 subscriptions each. Anything more than that will bring many timed out exception. I wonder if there is anything I could do to cater this issue or this is the limit of the websocket-client package/ my computer? Does anyone else encountered this problem before? Thanks.
Tried to set the 'skip_utf8_validation' to True to try to enhance the performance but it did not help.
def run(self):
# Setup the thread running WebSocketApp.
wst = threading.Thread(target=self._run, name='{}Raw'.format(self.name))
wst.daemon = True
wst.start()
connected = self.check_connected()
self.post_connect(connected)
def _run(self):
self.ws.run_forever(
ping_interval=20,
ping_timeout=10,
ping_payload=self.ping_payload,
skip_utf8_validation=True,
)
error: Traceback (most recent call last):
File "~/lib/python3.10/site-packages/websocket/_app.py", line 383, in run_forever
dispatcher.read(self.sock.sock, read, check)
File "~/lib/python3.10/site-packages/websocket/_app.py", line 68, in read
check_callback()
File "~/lib/python3.10/site-packages/websocket/_app.py", line 380, in check
raise WebSocketTimeoutException("ping/pong timed out")
websocket._exceptions.WebSocketTimeoutException: ping/pong timed out
Found one possible cause - one of the websocket threads is having some codes that are running slow and making all other websocket threads slower and slower while running. Trying to fix and see if that is the root clause. Thanks guys first.
I have two linux computers with fixed IP addresses:
A print server, whereby the connected printer is shared via CUPS.
(The server has the IP address "192.168.1.2" and the printer is called "test_printer".)
A computer, on which a python application is running, that should be able to use this print server.
Unfortunately, the printer propagation via CUPS seems not to work reliably (possibly due to the structure of the network).
Can I send print jobs directly from a python program to the CUPS print server?
If so, can you please provide a small example?
In theory, I would just send correctly formatted data to the IP address + port, but I didn't get it to work ...
Here are the approaches I have found so far and my problems with them:
command 'lpr'
import subprocess
lpr = subprocess.Popen("usr/bin/lpr", stdin=subprocess.PIPE) # on some distros the command is 'lp'
lpr.stdin.write("hello world\n")
lpr.stdin.close()
Relies on the printer propagation via CUPS.
python module pycups
import cups
with open("/home/user/Documents/some.txt", "w") as f:
f.write("hello world\n")
conn = cups.Connection()
conn.printFile("test_printer", "/home/user/Documents/some.txt", "some_title", {})
Before I can use a printer, I'll have to add it first, which in turn relies on the propagation via CUPS.
Also I didn't get conn.addPrinter() to work.
python module python-escpos / python-printer-escpos
import escpos.printer
p = escpos.printer.Network("192.168.1.2", port=631) # port 9100 seems not to work.
p.text("hello world\n")
p.close()
Probably the most promising approach ... unfortunately it doesn't print anything and throws an exception on closing.
# The traceback was produced in the interactive shell.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/.local/lib/python3.6/site-package/escpos/printer.py", line 214, in close
self.device.shutdown(socket.SHUT_RDWR)
OSError: [Errno 107] Transport endpoint is not connected
python module pkipplib / pyipptool
Unfortunately, there seems not to be a working python3 library that implements the Internet Printing Protocol (IPP) in 2018.
I use python 3.6.7.
The print server uses CUPS 2.2.1.
Not being a Python programmer, I cannot provide any sample code.
But I can give you some other hints:
Your assumption, that "in theory, [....] just send correctly formatted data to the IP address + port" is wrong.
If you send something, you have to "talk IPP" to the CUPS server (yes, via the IP address + port 631). But just sending a file is so much less than talking IPP.
Else, you could use 'netcat IPaddress port-no < filename' to print to any CUPS queue (which would be a huge security issue).
So you HAVE to use some IPP-capable library, if you want to integrate your program with IPP/CUPS
One alternative could be that you let your program do the "IPP talk to CUPS" by a CLI tool which is capable of doing so.
Have a look at the ippsample code of the Printer Working Group (PWG) which designed the IPP. That is in "beta" still but already fully usable. All its binaries are command line (CLI) utilities:
It provides the ippfind helper tool. If you run it without any parameters, it will print a list of available IPP Services (print servers and/or printer devices) by printing their respective IPP URIs.
It provides an ippserver command line, which can start up a fully-fledged, you guessed it, IPP Server exposing any feature you want.
Then there is ipptool included as an IPP Client. You could run 'ipptool -f myprintfile -t -v ipp://cupsserver/printers/test_printer print-job.test' to send "myprintfile" to the queue "test_printer" on "cupsserver" (given with example IPP URI ipp://cupsserver/printers/test_printer) and watch it talk IPP to CUPS while it does so.
See also my ASCIIcast "[TUTORIAL] IPP Sample Software (made with an AppImage)" here.
{https://asciinema.org/a/155588 }. Hint: if it plays too fast to follow and understand the screens in time, use the 'pause' button, read at your own speed, then resume playing.
I'm trying to use the microphone of my webcam logitech to listen
with sr.Microphone(sample_rate=32000) as source:
...
This error occurs :
File "/home/pi/project2/interface.py", line 226, in listen
with sr.Microphone(sample_rate = 32000) as source:
File
"/usr/local/lib/python3.4/dist-packages/speech_recognition/init.py",
line 140, in enter
input=True, # stream is an input stream File "/usr/local/lib/python3.4/dist-packages/PyAudio-0.2.10-py3.4-linux-armv7l.egg/pyaudio.py",
line 750, in open
stream = Stream(self, *args, **kwargs) File "/usr/local/lib/python3.4/dist-packages/PyAudio-0.2.10-py3.4-linux-armv7l.egg/pyaudio.py",
line 441, in init
self._stream = pa.open(**arguments) OSError: [Errno -9999] Unanticipated host error
I didn't find any fitting information on the web
You may check your anti-virus SW.
I got the same problem "OSError: [Errno - 9999] Unanticipated host error" every time I tried to record the wave file by pyAudio.
I check and confirm all the parameters OK.
I was stuck for several days before I noticed the anti-virus on my PC block my python script call to the hardware level.
All the thing I need to do is disable or exit the anti-virus SW and everything work well again.
I got this error while working with python threads.
I was creating 2 pyaudio objects for different purposes and hadn't closed one of them later. This gave me the error:
[Errno - 9999] Unanticipated host error
Closing that thread fixed my issue.
It may be a problem with microphone permission for your app make sure you have enabled the microphone permission for all apps in settings. because have developed one Tkinter project in which I am using pyaudio for recording purpose. On my pc the recorder working fine but not on the client machine. after investigation and running the same script on client machin it gives the same error as you mention. but after giving permission of a microphone for my project it working fine.
pyaudio throws this error because the python script not able to use the mic hardware.
I hope you will get some clarity with this and my answer may help you.
Windows user here. For me, I just had to change the default microphone which was being changed every time I connected my headphones.
This might also happen if there is no microphone access to the program or the microphone is not working properly. To check if the microphone is working properly, try recording some audio in voice recorder, and see if the selected microphone is the default audio recording device.
I've gotten a chance to work again with Python, but this time I decided to take Python 3.5 to my journey.
I had to port a working non-blocking socket server using Tornado, from Python 2.7 to 3.5. Used same source code, but this time it doesn't work as needed.
I keep getting [WinError 10035] A non-blocking socket operation could not be completed immediately on send whenever I accept a socket connection using socket.accept() and I still can't figure out why.
Tried to use example code that I've found a few years ago on GitHub Gist and still keep getting an error. Is there any changes in socket library or is it just a bug?
This error is harmless and expected. The problem is that the gist you linked to doesn't know about windows-specific error codes (on line 24 it checks for EWOULDBLOCK and EAGAIN, but it should also use WSAEWOULDBLOCK).
Since that gist was written, Tornado has gained some new utilities to make this easier. If you're using IOStreams, you can use tornado.tcpserver.TCPServer to accept your connections, or if you want to continue using plain sockets you can use the lower-level tornado.netutil.add_accept_handler.
I am having problems with sending triggers from a 32 bit PC with Windows XP Professional and Psychopy v.1.81.03 to the parallel port.
I am positive that the port address is 378, and am able to send triggers with Eprime and I am able to turn specific pins on and off with the software parmon (http://english.eazel.com/lv/group/view/kl35264/Parmon.htm)
I have tried using the experiment posted by Stéphanie and Nicholas (see this post in the psychopy google group: https://groups.google.com/forum/#!topic/psychopy-users/PxPhRDkuu2A)
I have verified that pywin32 (version 217) and parallel are installed, and tried both
port = parallel.ParallelPort(address=0x0378)
port = parallel.PParallelInpOut32(address=0x0378)
When using ParallelPort, I get:
Traceback (most recent call last):
File “ D:\SebastianKorb\untitled2_lastrun.py”, line 65, in
port = parallel.ParallelPort(address=0x0378) AttributeError: ‘module’
object has no attribute ‘ParallelPort’
Line 65 is where the command port = parallel.ParallelPort(address=0x0378)
is executed (note though that before that there is the line from psychopy import parallel)
When using PParallelInpOut32 I get the same (only now the error is about ‘PParallelInpOut32’)
I also tried to run the few lines of code shown on the psychopy reference manual (http://www.psychopy.org/api/parallel.html):
from psychopy import parallel
port = parallel.ParallelPort(address=0x0378)
port.setData(4)
port.readPin(2)
port.setPin(2, 1)
But again, I get the same type of error.
I should mention that I have also verified that I have administrator access to the file C:\Windows\system32\drivers\parport.sys
Can you please advise me on what I should try next?
I overlooked that it's actually the other way around. The direct calls to the parallel-port functions (as below) are deprecated. Nevertheless, they should still work. So maybe give it a try:
from psychopy import parallel
parallel.setPortAddress(0x378) #address for parallel port on many machines
parallel.setData(0) #sets all pins low
parallel.setPin(2,1) # set a certain pin high
parallel.setData(0) #sets all pins low
You should leave the pin high for a while or leave out the last line. Otherwise, you won't be able to detect the change. That's also how it is done in the Coder hardware demo "parallelPortOutput.py". Maybe try this first.
Best,
Axel
Addition:
Sebastian, my hunch at the moment is that the port does not even get initiated. I think the problem at the moment is that the respective error messages are only logged, but no informative error message is thrown (check the log files). That means that actually somehow the port drivers cannot be accessed on your system for some reason.
In the Coder Shell type from psychopy import parallel and then do next port = parallel.ParallelPort() (no address). Now just type port and paste the output here. My guess is that you only get the base class (ParallelPort) with which you cannot do anything, i.e., something like <psychopy.parallel.ParallelPort object at 0xe4805b0>. In that case, you would need to fix access to the drivers somehow.
I'm gong to back Axel's "hunch" here. I think it's extremely likely that either you don't have a parallel port driver installed or it isn't working. Try installing the InpOut32 driver from here, restart your computer and see if that fixes it:
http://www.highrez.co.uk/Downloads/InpOut32/
cheers,
Jon