Example #1
0
def main():
  RECORD_RATE = 44100
  RECORD_FORMAT = pyaudio.paInt16
  CHUNK = 2048

  global peers
  peers.append(Peer('jiehan', wave_to_list('samples/jiehan-kane.wav'), CHUNK)) 
  peers.append(Peer('kane', wave_to_list('samples/kane-kane.wav'), CHUNK))

  # start recording process
  p = pyaudio.PyAudio()
  p_stream = p.open(format=RECORD_FORMAT,
                    channels=1,
                    rate=RECORD_RATE,
                    input=True,
                    frames_per_buffer=CHUNK,
                    stream_callback=receive_new_slice)

  global twice_longest_template_length
  longest_template_length = max([len(peer.audio) for peer in peers])
  twice_longest_template_length = 2*longest_template_length
  
  global realtime_stream
  realtime_stream = deque(maxlen=twice_longest_template_length)

  p_stream.start_stream()

  while p_stream.is_active():
    time.sleep(RECORD_RATE/CHUNK)
def slice_comp():
  # similar slices from two audio clips should get a score of >0.7
  s1 = fft_freq_intensity(wave_to_list('tests/samples/jiehan-jiehan_s8.wav'))
  s2 = fft_freq_intensity(wave_to_list('tests/samples/jiehan-kane_s7.wav'))

  # plot_fft(*s1)
  # plot_fft(*s2)
  # plot_show()
  
  time_started = time()
  result = fft_similarity(s1[0], s1[1],
                          s2[0], s2[1])
  print "jiehan: jiehan8 -- kane7 =", result, "in", time() - time_started
  # assert result >= 0.7, "jiehan: jiehan8 -- kane7 score is too low (%f)" % result


  # similar slices from two audio clips should get a score of >0.7
  s1 = fft_freq_intensity(wave_to_list('tests/samples/jiehan-jiehan_s8.wav'))
  s2 = fft_freq_intensity(wave_to_list('tests/samples/jiehan-sean_s11.wav'))
  
  time_started = time()  
  result = fft_similarity(s1[0], s1[1],
                          s2[0], s2[1])
  print "jiehan: jiehan8 -- sean11 =", result, "in", time() - time_started
  # assert result >= 0.9, "jiehan: jiehan8 -- sean11 score is too low (%f)" % result

  # different slices from two audio clips should get a score of <0.4
  s1 = fft_freq_intensity(wave_to_list('tests/samples/jiehan-jiehan_s11.wav'))
  s2 = fft_freq_intensity(wave_to_list('tests/samples/kane-kane_s5.wav'))

  time_started = time()
  result = fft_similarity(s1[0], s1[1],
                          s2[0], s2[1])
  print "diff: jiehan8 -- kane4 =", result, "in", time() - time_started
def audio_comp():
  a1 = slice_audio(wave_to_list('tests/samples/jiehan-jiehan.wav'),2048, 'a1')
  a2 = slice_audio(wave_to_list('tests/samples/jiehan-sean.wav'),2048, 'a2')

  # print a1
  # print a1[0].data

  time_started = time()
  
  result = max_slice_tree_score(a1, a2)

  print "jiehan: jiehan -- sean = ", result, "in", time() - time_started
  # assert result >= 0.7, "same thing score too low (%f)" % result


  a1 = slice_audio(wave_to_list('tests/samples/jiehan-jiehan.wav'),2048, 'a1')
  a2 = slice_audio(wave_to_list('tests/samples/jiehan-sean.wav'),2048, 'a2')

  time_started = time()
  
  result = max_slice_tree_score(a1, a2)

  print "jiehan: jiehan -- sean = ", result, "in", time() - time_started
  # assert result >= 0.7, "same thing score too low (%f)" % result

  a1 = slice_audio(wave_to_list('tests/samples/jiehan-sean.wav'),2048, 'a1')
  a2 = slice_audio(wave_to_list('tests/samples/kane-kane.wav'),2048, 'a2')

  time_started = time()
  
  result = max_slice_tree_score(a1, a2)

  print "diff: sean -- kane = ", result, "in", time() - time_started
  # assert result >= 0.7, "same thing score too low (%f)" % result

  a1 = slice_audio(wave_to_list('tests/samples/jiehan-jiehan.wav'),2048, 'a1')
  a2 = slice_audio(wave_to_list('tests/samples/kane-kane.wav'),2048, 'a2')

  time_started = time()
  
  result = max_slice_tree_score(a1, a2)

  print "diff: jiehan -- kane = ", result, "in", time() - time_started
  # assert result >= 0.7, "same thing score too low (%f)" % result
Example #4
0
import sys
import wave
import re
import pyaudio
import struct
import math
from pylab import *
from utils.input import wave_to_list
from utils.data import slice_audio
from utils.fft import fft_freq_intensity

sample_slices = slice_audio(wave_to_list(sys.argv[1]), 2048)

if len(sys.argv) >= 3 and sys.argv[2] >= 0:
    slice = sample_slices[int(sys.argv[2])]

    freq, intensity = fft_freq_intensity(slice)

    title("audio %s" % sys.argv[2])
    xlim(70, 7000)
    ylim(0, 170)
    fill_between(freq, intensity)
    plot(freq, intensity, "r.")

    p = pyaudio.PyAudio()

    wf = wave.open(re.sub(r"\.", "_s" + sys.argv[2] + ".", sys.argv[1]), "wb")
    wf.setnchannels(1)
    wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))
    wf.setframerate(44100)
    for byte in slice: