Trying to get the frequencies of a .wav file in Python

前端 未结 3 1034
[愿得一人]
[愿得一人] 2020-12-31 11:28

I know that questions about .wav files in Python have been just about beaten to death, but I am extremely frustrated as no one\'s answer seems to be working for me. What I\'

3条回答
  •  轮回少年
    2020-12-31 12:03

    Try something along the below, it worked for me with a sine wave file with a freq of 1234 I generated from this page.

    from scipy.io import wavfile
    
    def freq(file, start_time, end_time):
        sample_rate, data = wavfile.read(file)
        start_point = int(sample_rate * start_time / 1000)
        end_point = int(sample_rate * end_time / 1000)
        length = (end_time - start_time) / 1000
        counter = 0
        for i in range(start_point, end_point):
            if data[i] < 0 and data[i+1] > 0:
                counter += 1
        return counter/length    
    
    freq("sin.wav", 1000 ,2100)
    1231.8181818181818
    

    edited: cleaned up for loop a bit

提交回复
热议问题