In [8]:
from __future__ import print_function

# We'll need numpy for some mathematical operations
import numpy as np
import scipy
import os

# matplotlib for displaying the output
import matplotlib.pyplot as plt
import matplotlib.style as ms
ms.use('seaborn-muted')
%matplotlib inline


# and IPython.display for audio output
import IPython.display


# Librosa for audio
import librosa
# And the display module for visualization
import librosa.display

audio_path = "../audio/Drum1.mp3"
audio_path2 = "../Drum1_snare.mp3"
filenames = []

def search(dirname):
    try:
        global filenames
        filenames = os.listdir(dirname)
    except PermissionError:
        pass

search("../audio/kick/")
for filename in filenames:
    audio_path2 = os.path.join("../audio/kick/", filename)
    ext = os.path.splitext(audio_path2)[-1]
    if ext == '.wav': 
        y, sr = librosa.load( audio_path, sr=44100 )
        y2, sr2 = librosa.load( audio_path2, sr=44100 )

        #IPython.display.Audio(data=y, rate=sr)
        print( y )

        
        print( len( y ) )
        print( len( y2 ) )

        with open("y_data.json", "w" ) as f:
            f.write( "[\n");
            for i in y:
                f.write( str( i ) + ",\n" );
            f.write( "]\n");
            
        with open("y2_data.json", "w" ) as f:
            f.write( "[\n");
            for i in y2:
                f.write( str( i ) + ",\n" );
            f.write( "]\n");

        print( sr )
        print( sr2 )

        print( scipy.spatial.distance.cosine( y[3000:6912] , y2[3000:6912]) )
        
        
        # check continuos zero value of sample at start and end
        firstZero = 0
        lastZero = -1
        for i in range( 0, len( y2 ) ):
            if(y2[i] == 0 or (y2[i]< 7.62939e-04 and y2[i]>-7.62939e-04)):
                if(lastZero == -1):
                    firstZero += 1
                else:
                    lastZero += 1
            else:
                lastZero = 1
        print(firstZero)
        print(lastZero)


        cmpLen = len(y2) - lastZero - firstZero

        for i in range( 0, len(y) - cmpLen ):
            if(scipy.spatial.distance.cosine( y[i:i+cmpLen], y2[firstZero:len(y2) - lastZero]) <0.6):
                print(scipy.spatial.distance.cosine( y[i:i+cmpLen], y2[firstZero:len(y2) - lastZero]))
                print(i)

        librosa.display.waveplot( y, sr=44100 )

        librosa.display.waveplot( y2, sr=44100 )

#point 1
print("------------------result-----------------------");
print( scipy.spatial.distance.cosine( y[1152+1615:1152+6382], y2[1615:6382] ))
print( scipy.spatial.distance.cosine( y[85824+3000:85824+3200], y2[3000:3200] ))

# for i in range(1615, 6382) :
#     if(y[1152+i] != y[i]) :
#         print(y[1152+i])
#         print(y2[i])
#         print(i)


[ 0.  0.  0. ...,  0.  0.  0.]
171648
21000
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-8-90d8aacbc8af> in <module>()
     48         print( len( y2 ) )
     49 
---> 50         with open("y_data.json" ) as f:
     51             f.write( "[\n");
     52             for i in y:

FileNotFoundError: [Errno 2] No such file or directory: 'y_data.json'

In [5]:
# 3. Run the default beat tracker
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)

print('Estimated tempo: {:.2f} beats per minute'.format(tempo))

# 4. Convert the frame indices of beat events into timestamps
beat_times = librosa.frames_to_time(beat_frames, sr=sr)

print('Saving output to beat_times.csv')
librosa.output.times_csv('/Users/lineplus/Desktop/project/MusicAnalyzer/audio/result.csv', beat_times)


Estimated tempo: 99.38 beats per minute
Saving output to beat_times.csv

In [8]:
print( tempo )
print( beat_frames )
print( beat_times )


99.3840144231
[ 12  38  64  95 121]
[ 0.27863946  0.88235828  1.4860771   2.20589569  2.80961451]

In [21]:
{frame_arr = librosa.util.frame(y, frame_length=2048, hop_length=512)
frame_arr2 = librosa.util.frame(y2, frame_length=2048, hop_length=512)
print( frame_arr )
print( frame_arr2 )

print( len( frame_arr[0] ) )
print( len( frame_arr2[0] ) )

print( len( frame_arr ) )
print( len( frame_arr2 ) )}


[[ 0.         -0.90861386  0.00773705 ..., -0.14220695  0.24075744
  -0.08692633]
 [ 0.         -0.87283039  0.0442284  ..., -0.10004218  0.23656647
  -0.09058234]
 [ 0.         -0.89167589  0.04157569 ..., -0.09503061  0.22459549
  -0.11358088]
 ..., 
 [-0.02395119  0.002145   -0.00423921 ..., -0.04312833  0.01478363  0.        ]
 [-0.0163303   0.00313669 -0.01180221 ..., -0.03618175  0.00881893  0.        ]
 [-0.0230791   0.00459595 -0.00822684 ..., -0.05059324  0.00875308  0.        ]]
[[  5.89917436e-05  -1.78596703e-04   9.54093132e-03 ...,   7.08066409e-06
   -2.89536711e-05   2.69479500e-07]
 [ -4.36029695e-05  -9.25666303e-04  -4.81339113e-04 ...,   1.13831420e-05
   -3.22506967e-05  -5.60235307e-08]
 [ -4.41918382e-05   1.10449665e-03  -6.86202710e-03 ...,  -1.06570906e-06
   -2.60917532e-05   1.07385965e-07]
 ..., 
 [  3.12932934e-05  -7.87355006e-03   1.27840792e-06 ...,   9.56925983e-10
   -4.10827766e-07   5.62528800e-12]
 [ -4.36971197e-03  -1.99687518e-02   3.79914860e-03 ...,  -1.44044754e-09
    2.90951931e-07  -2.46683468e-12]
 [ -5.51284105e-03  -1.95681173e-02   2.98191258e-03 ...,   2.01454164e-09
   -2.11024556e-07   9.38451573e-13]]
164
7890
2048
2048

In [17]:
librosa.display.waveplot( y )


Out[17]:
<matplotlib.collections.PolyCollection at 0x10e2bf9e8>

In [20]:
librosa.display.waveplot( y2 )


Out[20]:
<matplotlib.collections.PolyCollection at 0x1115cd9b0>

In [ ]:
sim = scipy.spatial.distance.cosine( frame_arr[0], frame_arr2[0] )
print( sim )