I would like to ask a question about following code. My aim is to play a mp3 file based on current value of 'threshold'. The file will be picked randomly, to prevent unnecessary repetition. (These are just test files.)
def play_sound_based_on_threshold(threshold): sound_pool = { 0.07: ['br1.mp3', 'br2.mp3', 'br3.mp3', 'br4.mp3', 'br5.mp3'], 0.14: ['br1.mp3', 'br2.mp3', 'br3.mp3', 'br4.mp3', 'br5.mp3'], 0.21: ['mr1.mp3', 'mr2.mp3', 'mr3.mp3', 'mr4.mp3', 'mr5.mp3'], 0.28: ['mr1.mp3', 'mr2.mp3', 'mr3.mp3', 'mr4.mp3', 'mr5.mp3'], 0.35: ['fr1.mp3', 'fr2.mp3', 'fr3.mp3', 'fr4.mp3', 'fr5.mp3'], } if threshold in sound_pool: selected_sound = random.choice(sound_pool[threshold]) pygame.mixer.music.load(selected_sound) pygame.mixer.music.play() pygame.time.delay(100) while pygame.mixer.music.get_busy(): pygame.time.Clock().tick(10)def play_sound_periodically(): while True: if datasequence: threshold_value = get_threshold(datasequence, .....) play_sound_based_on_threshold(threshold_value) time.sleep(1)
Currently, this code is working on 50%. The mp3 sound gets played based on value of threshold. After it ends, new file gets picked and gets played.
The issue is with picking up the value of 'threshold'. I need to make sure that the value for threshold gets picked only after the previous mp3 file stopped playing. What I believe is happening, is that having 'threshold_value' in a while loop provides a buffer, which is then being used as a threshold value. So instead of using a value, that is current, the 'threshold' value is a value from x seconds ago.
I need the functions to pick a value of threshold, which was last received. The threshold value is updated with a frequency of 125 Hz and the mp3 files are +-30 secs long. This means that throughout playing of the audio, there could be 125*30 values waiting in a buffer
Is there a simple solution, which I am not seeing?
Thanks a lot!
Note: I'm kind of a newbie in coding. My aim is to create a meditation app based on the current level of relaxation using a BCI EEG system.