0

I got this code from here. When I try to run it on Python (I am using Python 3.6) it encounters this error:

runfile('C:/Users/vsecadesang/Desktop/5th year/2nd sem/SIGNLAB/PROJECT/SIGNLABPROJ.py', wdir='C:/Users/vsecadesang/Desktop/5th year/2nd sem/SIGNLAB/PROJECT')
* recording
C:/Users/vsecadesang/Desktop/5th year/2nd sem/SIGNLAB/PROJECT/SIGNLABPROJ.py:75: RuntimeWarning: invalid value encountered in true_divide
  obj = np.real(np.dot(avg_freq_buffer[1:51], np.abs(freq_synth[1:100:2])) / np.dot(freq_synth[1:100:2], np.conj(freq_synth[1:100:2])))
C:/Users/vsecadesang/Desktop/5th year/2nd sem/SIGNLAB/PROJECT/SIGNLABPROJ.py:76: RuntimeWarning: invalid value encountered in greater
  if obj > obj_last:
Traceback (most recent call last):

  File "<ipython-input-1-3d1fe0eb2795>", line 1, in <module>
   runfile('C:/Users/vsecadesang/Desktop/5th year/2nd sem/SIGNLAB/PROJECT/SIGNLABPROJ.py', wdir='C:/Users/vsecadesang/Desktop/5th year/2nd sem/SIGNLAB/PROJECT')

  File "C:\Users\vsecadesang\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)

  File "C:\Users\vsecadesang\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/vsecadesang/Desktop/5th year/2nd sem/SIGNLAB/PROJECT/SIGNLABPROJ.py", line 99, in <module>
    stream.write(string_audio_data, CHUNK)

  File "C:\Users\vsecadesang\Anaconda3\lib\site-packages\pyaudio.py", line 586, in write
    exception_on_underflow)

KeyboardInterrupt

I don't know exactly what's wrong, please help. Here are the codes below:

import pyaudio
import numpy as np
import scipy.signal

CHUNK = 1024*2

WIDTH = 2
DTYPE = np.int16
MAX_INT = 32768.0

CHANNELS = 1
RATE = 11025*1
RECORD_SECONDS = 20

j = np.complex(0,1)


p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=True,
                frames_per_buffer=CHUNK)

print("* recording")

# initialize filter variables
fir = np.zeros(CHUNK * 2)
fir[:(2*CHUNK)] = 1.
fir /= fir.sum()

fir_last = fir
avg_freq_buffer = np.zeros(CHUNK)
obj = -np.inf
t = 10

# initialize sample buffer
buffer = np.zeros(CHUNK * 2)

#for i in np.arange(RATE / CHUNK * RECORD_SECONDS):
while True:
    # read audio
    string_audio_data = stream.read(CHUNK)
    audio_data = np.fromstring(string_audio_data, dtype=DTYPE)
    normalized_data = audio_data / MAX_INT
    freq_data = np.fft.fft(normalized_data)

    # synthesize audio
    buffer[CHUNK:] = np.random.randn(CHUNK)
    freq_buffer = np.fft.fft(buffer)
    freq_fir = np.fft.fft(fir)
    freq_synth = freq_fir * freq_buffer
    synth = np.real(np.fft.ifft(freq_synth))

    # adjust fir
    # objective is to make abs(freq_synth) as much like long-term average of 
freq_buffer
    MEMORY=100
    avg_freq_buffer = (avg_freq_buffer*MEMORY + \
                       np.abs(freq_data)) / (MEMORY+1)
    obj_last = obj

    obj = np.real(np.dot(avg_freq_buffer[1:51], np.abs(freq_synth[1:100:2])) / np.dot(freq_synth[1:100:2], np.conj(freq_synth[1:100:2])))
    if obj > obj_last:
        fir_last = fir
    fir = fir_last.copy()

    # adjust filter in frequency space
    freq_fir = np.fft.fft(fir)
    #t += np.clip(np.random.randint(3)-1, 0, 64)
    t = np.random.randint(100)

    freq_fir[t] += np.random.randn()*.05

    # transform frequency space filter to time space, click-free
    fir = np.real(np.fft.ifft(freq_fir))
    fir[:CHUNK] *= np.linspace(1., 0., CHUNK)**.1
    fir[CHUNK:] = 0


    # move chunk to start of buffer
    buffer[:CHUNK] = buffer[CHUNK:]

    # write audio
    audio_data = np.array(np.round_(synth[CHUNK:] * MAX_INT), dtype=DTYPE)
    string_audio_data = audio_data.tostring()
    stream.write(string_audio_data, CHUNK)

print("* done")

stream.stop_stream()
stream.close()

p.terminate()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Markus
  • 59
  • 6
  • 1
    Can you post the whole error? The important part comes after the traceback. – castle-bravo Mar 21 '18 at 02:13
  • Firstly please add the whole error code so at least we can know whats wrong, and it seems there are a lot of dependencies on the page you've linked, are you sure that you've successfully installed all of them? – Ubdus Samad Mar 21 '18 at 02:29
  • @UbdusSamad Here is the whole error and yes I already installed PYAudio module and the whl file that is needed with it. – Markus Mar 21 '18 at 03:12
  • `KeyboardInterrupt`??, do you press `Ctrl + C` between recordings? – Ubdus Samad Mar 21 '18 at 13:05
  • @UbdusSamad no. I don't know why the error is happening though. – Markus Mar 29 '18 at 12:32

0 Answers0