1

I have a piece of python script that runs through a continuous loop (~5Hz) to obtain data from a set of sensors connected to my PC, much like a proximity sensor.

I would like to translate this sensor data into audio output, using python and in a continuous manner. That is: whilst my sensor loop is running I want to generate and play a continuous sinusoidal audio sound, of which the frequency is modulated by the sensor output (e.g. higher sensor value = higher frequency). This is sort of the output that I want (without the GUI, of course: http://www.szynalski.com/tone-generator/)

I've looked through a lot of the available packages (pyDub, pyAudio, Winsound)but all seem to solve a piece of the puzzle, either signal generation, saving or playing, but I can't seem to find out how to combine them.

  • It's possible to perform frequency modulation and link different frequencies together and then save them, how to play them in real-time and without clogging up my sensor
  • It's possible to play threaded audio using WinSound -> but how to update frequence in real-time?

Or is this not a feasible route to walk on using python and should I write a script that inputs the sensor data into another more audio-friendly language?

Thanks.

MTDJassen
  • 21
  • 2
  • What is your rate of intake for sensor data? One issue you may run into if intake is "too fast" that while frequncy can be modulated, shorter duration means you will fail to hear it. – Anil_M Sep 10 '18 at 15:57
  • Minimum audible duration is proportional to freq. (e.g. lower freq may need up-to 1 sec while higher less than that). Else, u will end up hearing clicks. If u can throttle data in-take there are ways this can be achieved. – Anil_M Sep 10 '18 at 16:05
  • @Anil_M How does one compute the minimum audible duration, given the frequency? Is there a formula that can be used to predict it? – Daneel R. Sep 10 '18 at 17:20
  • It is roughly 1/ (freq (Hz)) . See this link for more details. https://sound.stackexchange.com/questions/28163/whats-the-shortest-sound-perceptible-to-the-human-ear – Anil_M Sep 10 '18 at 17:24
  • If your data intake is <1sec per sample, it may make sense to plot the data and play audio only when it goes above/below certain thresholds. – Anil_M Sep 10 '18 at 18:21

1 Answers1

0

I have a piece of python script that runs through a continuous loop (~5Hz)

Does it not work if you just add winsound.Beep(frequency_to_play, 1) in the loop?

Daneel R.
  • 527
  • 3
  • 9
  • Thanks for the input! That creates a beep of 1ms and is of course not hearable. I can however put this in my loop and create a longer beep that I can hear, however there's two issues with that and why I would like to avoid this method: - It's not an async / threaded code, so the script waits for the beep to end and then proceeds. That is at cost of precious time and therefore not wanted. - As the sensor loop and it's work has slightly different update times each iteration, the beep will not be stable (i.e. it's repeat frequency is different), which is not what I want. – MTDJassen Sep 10 '18 at 12:23
  • The length in ms can be changed by modifying the second value in the call to Beep() to something higher than 1, but that does not solve the second issue you mentioned. Could you post your code here so people can inspect it? You may want to describe in detail how you get the data from the sensors though, and maybe provide a sample/example. – Daneel R. Sep 10 '18 at 12:58