Home Audio filter with python
Reply: 0

Audio filter with python

Pascal Tricaud
1#
Pascal Tricaud Published in 2018-02-12 09:50:30Z

As part of an interactive installation, I would like to program an audio filter LPF (cut high frequencies) on a sound that runs in a loop on python. In addition I would like to vary the cutoff frequency of the filter according to the distance of an obstacle measured by a distance sensor, all on a Raspberry Pi 3.

I have already managed to program the distance measurement by the sensor and to run a looping sound but I do not know how to do it for the audio filter. By searching on the internet I can not find an answer and a clear method on my situation.

Here is my code:

         #Libraries
 import RPi.GPIO as GPIO
 import time
 import pygame
 pygame.init()
 caca = pygame.mixer.Sound("bells001.wav")
 pygame.mixer.music.load("bells001.wav")
 #GPIO Mode (BOARD / BCM)
 GPIO.setmode(GPIO.BCM)

 pygame.mixer.music.play(-1)
 #set GPIO Pins
 GPIO_TRIGGER = 23
 GPIO_ECHO = 24

 #set GPIO direction (IN / OUT)
 GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
 GPIO.setup(GPIO_ECHO, GPIO.IN)

 def distance():
 # set Trigger to HIGH
 GPIO.output(GPIO_TRIGGER, True)

 # set Trigger after 0.01ms to LOW
 time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)

StartTime = time.time()
StopTime = time.time()

# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
    StartTime = time.time()

# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
    StopTime = time.time()

# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2

return distance

if __name__ == '__main__':
try:
    while True:
        dist = distance()
        print (dist)
        time.sleep(0.2)

    # Reset by pressing CTRL + C
except KeyboardInterrupt:
    print("Measurement stopped by User")
    GPIO.cleanup()

I tried this for now

from pydub import AudioSegment
from pydub.playback import play

song = AudioSegment.from_wav("bells001.wav")
new = song.low_pass_filter(10000)
play(new)

Unfortunately instead of me out a filtered sound, I have the sound very degraded with lots of crunches ... Anyone have an idea?

I tried other scripts but I still have problems with module or library not found while I install and reinstall all these modules.

Can anyone help me?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.401679 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO