I've been trying to create a simple audio-player which i want to run from the commandline, for this I've used Gstreamer and the pygst python bindings and my code so far looks like this:
import pygst
pygst.require('0.10')
import gst
import os
class Player(object):
mp3stream = "http://http-live.sr.se/p1-mp3-192"
def __init__(self):
self.pipeline = gst.Pipeline("RadioPipe")
self.player = gst.element_factory_make("playbin", "player")
self.pipeline.add(self.player)
self.player.set_property('uri', self.mp3stream)
self.pipeline.set_state(gst.STATE_PLAYING)
player = Player()
while 1:
if(1 == 2):
break
Now for some reason, when I run this code I get the following warnings:
** (radio.py:7803): WARNING **: Command line `dbus-launch --autolaunch=f12629ad79391c6f12cbbc1a50ccbcc8 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n
I can play music without a problem, but I would very much get rid of these warnings, now I assume that the Gstreamer library for some reason tries to start something that requires X11 but isn't necessary for the audioplaying part. Any comments on the validity of this assumption are most welcome.
Can I import something else or pass some sort of flag to stop Gstreamer from trying to initialize X11?
EDIT 1
I've tried adding this:
fakesink = gst.element_factory_make("fakesink", "fakesink")
self.player.set_property("video-sink", fakesink)
Which according to the documentation the above code will disable the automatic enabling of video-streaming. This does however not fix my problem with the warnings.
EDIT 2
Okay so the element(?) playbin is something like a ready-made piping of several audio and video related stuff, I'm sorry I can't explain it better for now. However it seems that playbin initializes some elements(?) that tries to access X11. I'm guessing that since I'm not playing anything videorelated it doesn't crash. I've managed to edit some of the playbin elements(?) but none of them fixes the X11 warning.
The current code looks like this:
self.pipeline = gst.Pipeline("RadioPipe")
self.player = gst.element_factory_make("playbin", "player")
pulse = gst.element_factory_make("pulsesink", "pulse")
fakesink = gst.element_factory_make("fakesink", "fakesink")
self.player.set_property('uri', channel)
self.player.set_property("audio-sink", pulse)
self.player.set_property("video-sink", fakesink)
self.pipeline.add(self.player)
The questionmark after element has to do with me not being sure that is the correct wording.
You should be able to disable the video flag in playbin's flag properties. Alternately, if you do need video and know which video sink you need, set the video-sink property accordingly.
Related
I'm currently working on a script that creates a tray icon that allows the user to adjust screen brightness through menu options. The source code, written in python 3.6.8, can be found as a paste HERE. There seems to be an error message coming up when trying to select one of the brightness options, seen HERE. I did some reading and found that the error I'm getting (0x8004100c) refers to a feature or operation not being supported. Are there any workarounds available for this?
Thank you in advance.
code: https://pastebin.com/sLbyE9yb
error: https://pastebin.com/Xs7wHk73
WMI error reference: https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-error-constants
gist: https://gist.github.com/imri0t/12e768e3d7e08734b85ae532d56090e1
(also if anyone can let me know if there's a way to keep the script from dying after an action is made it would be appreciated)
modules needed: pip install infi.systray / pip install wmi
code snip that I believe produces the error:
from infi.systray import SysTrayIcon
import wmi
def brightness_50(systray):
'''brightness: 50%'''
b = 50
c = wmi.WMI(namespace='root\\wmi')
br = c.WmiMonitorBrightnessMethods()[0]
br.WmiSetBrightness(3, b) #b will be a precentage / 100
menu = (("brightness: 100%", None, brightness_50))
systray = SysTrayIcon("icon.ico", "brightness", menu)
systray.start()
I'm referencing this old thread:
system wide shortcut for Mac OS X
I have been attempting to use this to create a global hotkey for osx since my old method of using pyglobalshortcuts no longer works with PyQt5.
The trouble is that when I listen for 'kCGEventKeyUp' or 'kCGEventKeyDown' in the 'CGEventTapCreate' function, the program exits with code '-11'.
Here's the code I was trying:
import Quartz
from AppKit import NSKeyUp, NSSystemDefined, NSEvent
def keyboardTapCallback(proxy, type_, event, refcon):
keyEvent = NSEvent.eventWithCGEvent_(event)
if keyEvent is None:
pass
else:
print keyEvent
tap = Quartz.CGEventTapCreate(
Quartz.kCGSessionEventTap,
Quartz.kCGHeadInsertEventTap,
Quartz.kCGEventTapOptionListenOnly,
# Quartz.kCGEventMaskForAllEvents,
Quartz.CGEventMaskBit(Quartz.kCGEventKeyUp),
keyboardTapCallback,
None
)
runLoopSource = Quartz.CFMachPortCreateRunLoopSource(None, tap, 0)
Quartz.CFRunLoopAddSource(
Quartz.CFRunLoopGetCurrent(),
runLoopSource,
Quartz.kCFRunLoopDefaultMode
)
Quartz.CGEventTapEnable(tap, True)
try:
Quartz.CFRunLoopRun()
except Exception as e:
print e, "<<<<<<<<<<<<<<<"
I also tried replacing 'Quartz.kCGEventMaskBit(Quartz.kCGEventKeyUp)' with 'Quartz.kCGEventMaskForAllEvents' and while this did not fail it also isn't returning any alphanumeric keys (I need to be able to use 'ctrl+shift+d' as my shortcut).
Am I terribly far off with being able to detect this shortcut in Quartz or is there a better method in OSX?
Thanks,
-Mark
I think I just figured it out. I'm using a shared keyboard through Synergy. When I went back to my mac keyboard, it could detect the key events.
Thanks!
-Mark
I'm starting work on an app that will need to create sound from lots of pre-loaded ".mid" files.
I'm using Python and Kivy to create an app, as I have made an app already with these tools and they are the only code I know. The other app I made uses no sound whatsoever.
Naturally, I want to make sure that the code I write will work cross-platform.
Right now, I'm simply trying to prove that I can create any real sound from a midi note.
I took this code suggested from another answer to a similar question using FluidSynth and Mingus:
from mingus.midi import fluidsynth
fluidsynth.init('/usr/share/sounds/sf2/FluidR3_GM.sf2',"alsa")
fluidsynth.play_Note(64,0,100)
But I hear nothing and get this error:
fluidsynth: warning: Failed to pin the sample data to RAM; swapping is possible.
Why do I get this error, how do I fix it, and is this the simplest way or even right way?
I could be wrong but I don't think there is a "0" channel which is what you are passing as your second argument to .play_Note(). Try this:
fluidsynth.play_Note(64,1,100)
or (from some documentation)
from mingus.containers.note import Note
n = Note("C", 4)
n.channel = 1
n.velocity = 50
fluidSynth.play_Note(n)
UPDATE:
There are references to only channels 1-16 in the source code for that method with the default channel set to 1:
def play_Note(self, note, channel = 1, velocity = 100):
"""Plays a Note object on a channel[1-16] with a \
velocity[0-127]. You can either specify the velocity and channel \
here as arguments or you can set the Note.velocity and Note.channel \
attributes, which will take presedence over the function arguments."""
if hasattr(note, 'velocity'):
velocity = note.velocity
if hasattr(note, 'channel'):
channel = note.channel
self.fs.noteon(int(channel), int(note) + 12, int(velocity))
return True
I am developing a program in python, and one element tells the user how much bandwidth they have used since the program has opened (not just within the program, but regular web browsing while the program has been opened). The output should be displayed in GTK
Is there anything in existence, if not can you point me in the right direction. It seems like i would have to edit an existing proxy script like pythonproxy, but i can't see how i would use it.
Thanks,
For my task I wrote very simple solution using psutil:
import time
import psutil
def main():
old_value = 0
while True:
new_value = psutil.net_io_counters().bytes_sent + psutil.net_io_counters().bytes_recv
if old_value:
send_stat(new_value - old_value)
old_value = new_value
time.sleep(1)
def convert_to_gbit(value):
return value/1024./1024./1024.*8
def send_stat(value):
print ("%0.3f" % convert_to_gbit(value))
main()
import time
def get_bytes(t, iface='wlan0'):
with open('/sys/class/net/' + iface + '/statistics/' + t + '_bytes', 'r') as f:
data = f.read();
return int(data)
while(True):
tx1 = get_bytes('tx')
rx1 = get_bytes('rx')
time.sleep(1)
tx2 = get_bytes('tx')
rx2 = get_bytes('rx')
tx_speed = round((tx2 - tx1)/1000000.0, 4)
rx_speed = round((rx2 - rx1)/1000000.0, 4)
print("TX: %fMbps RX: %fMbps") % (tx_speed, rx_speed)
should be work
Well, not quiet sure if there is something in existence (written in python) but you may want to have a look at the following.
Bandwidth Monitoring (Not really an active project but may give you an idea).
Munin Monitoring (A pearl based Network Monitoring Project)
ntop (written in C/C++, based on libpcap)
Also just to give you pointers if you are looking to do something on your own, one way could be to count and store packets using sudo cat /proc/net/dev
A proxy would only cover network applications that were configured to use it. You could set, e.g. a web browser to use a proxy, but what happens when your proxy exits?
I think the best thing to do is to hook in lower down the stack. There is a program that does this already, iftop. http://en.wikipedia.org/wiki/Iftop
You could start by reading the source code of iftop, perhaps wrap that into a Python C extension. Or rewrite iftop to log data to disk and read it from Python.
Would something like WireShark (https://wiki.wireshark.org/FrontPage) do the trick? I am tackling a similar problem now, and am inclined to use pyshark, a WireShark/TShark wrapper, for the task. That way you can get capture file info readily.
I've made a python script which should modify the profile of the phone based on the phone position. Runned under ScriptShell it works great.
The problem is that it hangs, both with the "sis" script runned upon "boot up", as well as without it.
So my question is what is wrong with the code, and also whether I need to pass special parameters to ensymble?
import appuifw, e32, sensor, xprofile
from appuifw import *
old_profil = xprofile.get_ap()
def get_sensor_data(status):
#decide profile
def exit_key_handler():
# Disconnect from the sensor and exit
acc_sensor.disconnect()
app_lock.signal()
app_lock = e32.Ao_lock()
appuifw.app.exit_key_handler = exit_key_handler
appuifw.app.title = u"Acc Silent"
appuifw.app.menu = [(u'Close', app_lock.signal)]
appuifw.app.body = Canvas()
# Retrieve the acceleration sensor
sensor_type= sensor.sensors()['AccSensor']
# Create an acceleration sensor object
acc_sensor= sensor.Sensor(sensor_type['id'],sensor_type['category'])
# Connect to the sensor
acc_sensor.connect(get_sensor_data)
# Wait for sensor data and the exit event
app_lock.wait()
The script starts at boot, using ensymble and my developer certificate.
Thanks in advance
I often use something like that at the top of my scripts:
import os.path, sys
PY_PATH = None
for p in ['c:\\Data\\Python', 'e:\\Data\\Python','c:\\Python','e:\\Python']:
if os.path.exists(p):
PY_PATH = p
break
if PY_PATH and PY_PATH not in sys.path: sys.path.append(PY_PATH)
xprofile is not a standard library, make sure you add path to it. My guess is that when run as SIS, it doesn't find xprofile and hangs up. When releasing your SIS, either instruct that users install that separately or include inside your SIS.
Where would you have it installed, use that path. Here's python default directory as sample:
# PyS60 1.9.x and above
sys.path.append('c:\\Data\\Python')
sys.path.append('e:\\Data\\Python')
# Pys60 1.4.x or below
sys.path.append('c:\\Python')
sys.path.append('e:\\Python')
Btw make clean exit, do this:
appuifw.app.menu = [(u'Close', exit_key_handler)]