Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Play random sound / python command

If you have a question or need help, this is the place to be.
Post Reply
skribb
Experienced User
Posts: 228
Joined: Thu Feb 12, 2015 7:22 pm
Location: Win7 64bit

Play random sound / python command

Post by skribb »

Hi, i wish to be able to play a random sound on my sound card. The normal playsound function looks like this in python:

Code: Select all

eg.plugins.System.PlaySound(u'M:\\test.wav', 0, False)
I'm not a python programmer so I was wondering if I could get any tips on how to modify the python command to be able to play a random sound. I'm open to using dirty tricks like having the audio files named sequentially etc, as long as it works :)

thanks for any help!
Automation is life.

Win7 64bit
EG: v0.5.0-rc4
Dragon470
Experienced User
Posts: 205
Joined: Thu Feb 10, 2011 2:16 am

Re: Play random sound / python command

Post by Dragon470 »

Try this, you currently have to feed it a folder path with wav files. It uses the default windows in this test.

Code: Select all

from os import listdir
from os.path import isfile, join
import random

#path you want to get wav files from
path = "C:\Windows\Media"
onlyfiles = [ f for f in listdir(path) if isfile(join(path,f)) ]

onlywavfiles = []
for f in onlyfiles:
    if f[-3:] == "wav":
        onlywavfiles.append(f)

#generate random number based on number of available files
randomnum = random.randint(0,len(onlywavfiles)-1)

eg.plugins.System.PlaySound(path + "/" + onlywavfiles[randomnum], 1, False)



p.s. This was a fun momentary diversion.
skribb
Experienced User
Posts: 228
Joined: Thu Feb 12, 2015 7:22 pm
Location: Win7 64bit

Re: Play random sound / python command

Post by skribb »

Seems to be working! Thanks a lot :)

I'm glad I generated a fun momentary diversion for you :D
Automation is life.

Win7 64bit
EG: v0.5.0-rc4
Henrik4223
Posts: 47
Joined: Fri Mar 04, 2016 10:18 am

Re: Play random sound / python command

Post by Henrik4223 »

I like this script very much - I have used it for my burglar alarm. Whenever my PIR outside the house goes off a random dog barking sound goes of inside :mrgreen:

My problem is that the wav files a very bad quality and the barking does not sound very realistic. Is there any posibility to make a script that plays .mp3 files - they are very realistic and scarry? :D

I have tried just pointing the directorry and changing .wav to .mp3 but this doesent work....
skribb
Experienced User
Posts: 228
Joined: Thu Feb 12, 2015 7:22 pm
Location: Win7 64bit

Re: Play random sound / python command

Post by skribb »

Henrik4223 wrote:I like this script very much - I have used it for my burglar alarm. Whenever my PIR outside the house goes off a random dog barking sound goes of inside :mrgreen:

My problem is that the wav files a very bad quality and the barking does not sound very realistic. Is there any posibility to make a script that plays .mp3 files - they are very realistic and scarry? :D

I have tried just pointing the directorry and changing .wav to .mp3 but this doesent work....
The easiest way would be to convert the mp3s to wav

What is a PIR?
Automation is life.

Win7 64bit
EG: v0.5.0-rc4
Henrik4223
Posts: 47
Joined: Fri Mar 04, 2016 10:18 am

Re: Play random sound / python command

Post by Henrik4223 »

Passive infrared sensor :P

Great idea - will check the quality on the conversion asap
skribb
Experienced User
Posts: 228
Joined: Thu Feb 12, 2015 7:22 pm
Location: Win7 64bit

Re: Play random sound / python command

Post by skribb »

Henrik4223 wrote:Passive infrared sensor :P

Great idea - will check the quality on the conversion asap

Let me know how it works out
Automation is life.

Win7 64bit
EG: v0.5.0-rc4
Henrik4223
Posts: 47
Joined: Fri Mar 04, 2016 10:18 am

Re: Play random sound / python command

Post by Henrik4223 »

WORKS PERFECTLY - GREAT kode - You should make it into a plugin :mrgreen:
skribb
Experienced User
Posts: 228
Joined: Thu Feb 12, 2015 7:22 pm
Location: Win7 64bit

Re: Play random sound / python command

Post by skribb »

Henrik4223 wrote:WORKS PERFECTLY - GREAT kode - You should make it into a plugin :mrgreen:
How did you make your PIR talk to EG?
Automation is life.

Win7 64bit
EG: v0.5.0-rc4
Henrik4223
Posts: 47
Joined: Fri Mar 04, 2016 10:18 am

Re: Play random sound / python command

Post by Henrik4223 »

I have an RFXtrx and the RFX plugin for Eventghost.

I have found these (http://www.ebay.com/itm/311479916789?_t ... EBIDX%3AIT) very cheap (and SMALL and works perfectly - no false events so far and free shipping :P ) PIR sensors from China that works together with the RFXtrx out of the box. They send both movement and battery status in the event from RFXtrx.
skribb
Experienced User
Posts: 228
Joined: Thu Feb 12, 2015 7:22 pm
Location: Win7 64bit

Re: Play random sound / python command

Post by skribb »

Thanks a lot :D
Automation is life.

Win7 64bit
EG: v0.5.0-rc4
Post Reply