Python code to play an audio file - python

I want to build a school alarm application using python and raspberry pi.
How can i use use python to play an audio file at specified timings?

This will work if the alarm noise file is in the same directory as the python file itself. I do warn you though this is very clunky and the sound file is opened separately but nevertheless it does what you required.
import os, time
while True:
os.startfile("FILENAME OF SOUND") #Replace with the appropriate file name (Make sure the alarm sound is in the same directory as the python file else this will not work)
time.sleep(DELAY IN SECONDS) #This is how long the delay is between each time it opens the sound

Related

How to create an event listener for mp3 sounds

I have a mp3 file namd 'audio.mp3' but for some reason no matter how hard I try I cannot find a useful link to help me code a programm that will listen to the computer and when hearing a specific sound that is exactly like my file 'audio.mp3', do something (that ofc i'll code)
I kept searching online for modules i could use or help but I only came across links that tell me how to record sound :
from playsound import playsound
playsound('audio.mp3')
I have no idea what to begin with :/
The perfect module I could think of is a module that I onlt have to give him the name of my mp3 file, then put an event listener and it'll do the part where it listens to my computer sounds and trigger my event when it hears my file

Automatically overwrite existing file with an incoming file

as of right now I have a file called song.mp3 that I have integrated into a Python program which will act as an alarm. I would like it so that whenever I send the Raspberry Pi a new song via Bluetooth, it will just automatically rename this song to be song.mp3, thereby overwriting the previous song. That way I don't have to change my alarm program for different songs. Any help?
Assuming that the mp3 files are all in the same directory, you could perhaps have a cron job running that periodically renames the most recent file and so something like:
mv $(ls -1t *.txt | head -1) song.mp3
This is a quick example. It would be more preferable to add the ablve to a script and add some "belts and braces" to ensure that the script doesn't crash.

Having python script save captured video with date and time

I'm trying to have my raspberry pi capture short videos and save them to a file with date and time. I have a short Python script that will capture video but it seems to overwrite the file with the latest video instead of saving each capture to a separate file.
import time
import picamera
with picamera.PiCamera() as camera:
camera.start_preview()
camera.start_recording('/home/pi/Desktop/video.h264')
time.sleep(60)
camera.stop_recording()
camera.stop_preview()
Of course it will.
Since you're always using the same path and file name to save the file it will override whatever was in the file video.h264 before. You should implement an algorithm to avoid this.
You could name the files with the recording date in it.This will usually never be the same and you can rename your files later if you want so. The system date is available in the standard time module.
When you hit into problems with that we can help you.

How to close program in python opened by os.system()?

In python 3.4 , I was trying to open a "wav" file using vlc in Linux. Here is my code:
import os,time
os.system("cvlc audio/some.wav")
time.sleep(3) #audio was one and half sec
a = 3+3
print (a)
It plays the audio but then doesn’t do the rest. What should I do to make it do them? more precisely what should I do to close the vlc program?
With solving the problem it will also be very grateful to know is there any easier way to play audio within the code specifically in python 3.4?
(platform independent code will be even more grateful!)
So the VLC player doesn't exit. The VLC player has a command line argument to close the player once the song/video has been played.
Playlist
These options define the behavior of the playlist. Some of them can be overridden in the playlist dialog box.
--play-and-exit, --no-play-and-exit
Play and exit (default disabled)
Source: https://wiki.videolan.org/VLC_command-line_help
Can you try the following?
os.system("cvlc audio/some.wav --play-and-exit")

Measuring USB live-transfer speed with Python

I'm trying to find out the speed of a live data-transfer via USB on a Mac run from the command line with Android Debug Bridge.
Is there a way to do this with any Python-packages ?
Basically, I just want to the script to show me the speed as-is shown at the bottom of a file-transfer window. If not with Python, any command-line utility for the same are welcome.
Are you doing the file transfer inside python?
With a reader and writer?
If so, you can read a piece into a buffer, write it out, update a progressbar and repeat this until the file is completely transfered.
The progressbar module has options to calculate and display the transfer rate just by giving it updates on the writing progress.
See http://code.google.com/p/python-progressbar/ for more info and examples of the progressbar module.
edit:
fixxer, you can use python to check the file size of the file(s) on the usb device and update the progressbar when the file grows.
This is not really measuring the transfer speed of the usb bus, but if you're transfering files it will give an indication of how fast this is going.
If you are streaming a movie, or flashing a chip you'd have to talk to the usb bus directly.
Maybe look into http://www.libusb.org/ and it's python wrapper https://github.com/walac/pyusb

Categories