0

I am new to python and am attempting to build a simple alarm app (command line for now). This is using Python 3.6 and I am developing on Ubuntu 18.04. When I play a sound using pydub, playsound or simpleaudio, the sound is preceded by an annoying click, which I presume is meant to emulate the pressing of a button on a machine. I may have missed it in the docs, but do not see anything.

To be clear, this clicking does not exist in the sound file. I have put the play command in a loop to verify and only hear it on the initial play. For example:

# run pydub
sound = AudioSegment.from_file(f, format="wav")
# play(sound)
for _ in range(2):
    play(sound)

This happens regardless of playing wav, mp3 or flac.

FWIW - I have been unsuccessful using python-vlc and pygame. I fear spending much time only to continue to hear the "click".

So, the question is, how do I prevent the click or what library/module should I use to achieve playback of a snippet in such a simple app?

Bill Turner
  • 869
  • 1
  • 13
  • 27
  • Do you still hear the click/pop if you play a longer audio clip? (I know it's not in the audio file) It might have something to do with the sort duration of the audio file, in which case add empty noise to the audio file to extend it. – GordonAitchJay Oct 05 '19 at 18:46
  • I pointed it to a full mp3 I have on my drive (Pink Floyd's On The Run). I still get the click. – Bill Turner Oct 06 '19 at 15:26
  • i loaded the clip into VLC player. When i launch that, it, too, makes the click sound. Tried the same in Rhythmbox. Same thing. Interestingly, and as i have already discovered in my tests, the click only happens with the first item played. Since it seems to happen everywhere, i am convinced it is something deeper. – Bill Turner Oct 06 '19 at 16:18
  • Yeah, must be a driver issue. It needs clear the buffer or something. – GordonAitchJay Oct 07 '19 at 03:41

2 Answers2

1

Probably the easiest way to eliminate click in existing audio data is to put a very short fade-in at the beginning to ensure playback starts from a "zero crossing"

sound = AudioSegment.from_file(f, format="wav")

# 10 ms fade in
sound = sound.fade_in(duration=10)

for _ in range(2):
    play(sound)
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
1

I'm having the same issue on a MacBook Pro trying to play short sound clips with simpleaudio. If I play an array of zeros, I get an obnoxious click noise at the beginning and end as with this code. Built-in speakers, as well as air pods, show the same issue.

import simpleaudio as sa
import numpy as np

print("Play silence for 1 second.")

sample_rate = 44100
s = np.zeros(sample_rate).astype(np.int16)
sp = sa.play_buffer(s, 1, 1, sample_rate)
sp.wait_done()

print("   Done.")

[EDIT: Found my problem -- the second 1 in the arguments to play_buiffer() is the number of bytes per sample. It should have been 2 for a 16-bit sample size. But using 1 shouldn't break anything for this sample of zeros, but it does probably force the audio hardware to be reconfigured for 8-bit play vs 16 bit, and changing the configuration might well be the cause of the pop. When I switch to 2, there are no pops.]

Curt Welch
  • 339
  • 3
  • 5
  • This does not provide an answer to the question. You can search for similar questions, or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, ask a new question, and include a link to this one to help provide context. – Viktoriya Malyasova Dec 02 '20 at 00:40
  • True. Yeah, I know. I tried to add a comment. But stack overflow wouldn't let me comment because I've not posted enough. So I cheated and added a comment as a fake answer. But it's at least a hint as to what might be going on with the original question. :) – Curt Welch Dec 02 '20 at 07:28
  • @CurtWelch it looks like you first posted a comment as an answer, but then revised it to add a complete answer. Please either reword it as an Answer to the original Question or ask a new Question and then Answer it yourself (should be available when asking!) – ti7 Dec 13 '20 at 02:56