bastibe / SoundCard

A Pure-Python Real-Time Audio Library
https://soundcard.readthedocs.io
BSD 3-Clause "New" or "Revised" License
680 stars 69 forks source link

Recorded content differs for the same playback #80

Closed charlamenan closed 4 years ago

charlamenan commented 4 years ago

I am playing a sine wave file of 10sec length through a windows player called groove and recording in parallel and then comparing the input file and recorded file using fft

numpy.mean(numpy.abs(waveform1 - waveform2)) < threshold

I see the above calculation difference is 0.0 for few runs .and having difference in 17thousands or more.

I would like to understand ,why is the same code and same input file(sine.wav) ,same player gives this difference .

when the difference is non-zero then the recorded content is having glitches like below . image

when the difference is zero and recorded content is looking good without glitched and this is what i want to see for every run .How can i handle this ?

pasting sudo of the code

recording starts before playback and the below code i am invoking as a thread

data = recording_device.record(samplerate=fs_rate, numframes=(frames))
#time.sleep(length)
print("write to file")
sf.write(recorded_filename, data, fs_rate,subtype=bitdepth)
print("wrote to file")

code to play file using groove music (windows player )

join the recording thread

charlamenan commented 4 years ago

I did an experiment to differentiate if it is problem in writing to a file (of the recorded content) or the recorded content itself differs .

So i wrote the numpy array data of the recorded content to a file like below ,

recording_device is a loopback speaker endpoint

data = recording_device.record(samplerate=fs_rate, numframes=(frames))
data_list = data.tolist()
with open('data.txt','w') as f:
    for item in data_list:
        f.write("%s\n" % item)

Attached the data.txt in the cases of working and non-working condition . Please compare using some file compare . data_working.txt data_not_working.txt i see 384 lines data missing in the data_not_working.txt in the middle when compared to data_working.txt and 384 lines are added to data_not_working.txt in the end with zero data .

How do i know the reason for this ?Any latency correction between playback and recording .

bastibe commented 4 years ago

If I'm understanding you correctly, you are recording something on a loopback device. Sometimes this works and records what was played; sometimes it does not work and has artifacts.

Is this description correct?

Just from that description, I don't know what causes these problems. So we need to debug it. Apparently your recording is skipping data for some reason.

charlamenan commented 4 years ago

Yes .The description is right . Do these kinds of artifact happen only on loopback devices? My ans:i tried only on loop back device (Speaker).

You say you are running this in a thread. Does it happen in a plain script also? My ans :In a plain script i can not start recording first and then playback . If i start recording first then the next line is executed (to play)only after mentioned frames are captured .so next line is never executed .In a plain script i can start playback first and then record and this is also having the same problem( has artifacts.) for few runs and few run works without problem .

My problem is ,same code records differently on different run .when works the fft difference is always zero .when not working the fft difference is not always same number.

Is there a way to execute record command first and then playback without using thread . Record command gives control back after the mentioned amount of frames are captured .

charlamenan commented 4 years ago

Attaching the code along with sine.wav file please run it like 'python code.py' and data.txt will be created after every run .Rename the data.txt after every run and compare the .txt files .

I expect that the data.txt should be same for every run .and i think my expectation is right . Please help

charlamenan commented 4 years ago

code.zip

charlamenan commented 4 years ago

@bastibe ,Please use the above code ,now i think you can understand the problem .

bastibe commented 4 years ago

Thank you for showing me a code example.

In this case, I don't think the threading is the problem, as there is essentially only one thread running. Since it works some of the time, I don't think this is a bug in SoundCard, either.

Please try recording something from another microphone (not the loopback) to make sure that the problem is indeed one in SoundCard, instead of the loopback system itself.

charlamenan commented 4 years ago

Thanks for the reply .

But another microphone(other than loop back) will have external noise also getting captured ,so i can not verify for similarity between recorded file .

I have an loopback exe which i have got from automation team in my company ,which also uses loop back speaker for recording and there i get always same output .

bastibe commented 4 years ago

Please provide me with some data to help me debug this. Without your help, I won't be able to help you.

charlamenan commented 4 years ago

The code i shared could re-produce the behavior. python code.py

The above command in windows will take sine.wav(attached in code.zip) and play through groove player and record using loop back speaker and write the recorded data to a file .

If you compare the recorded files of different run ,it is different .

bastibe commented 4 years ago

The behavior you describe does not happen on my computer. Without your help, I can not help you.

To make this absolutely clear, I am helping you in my free time, of my own free account. But you are wasting my time. Either help me help you, or you are on your own.