Guest User

python code

a guest
Nov 14th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. import numpy as np
  2. import wave
  3. import struct
  4.  
  5. def find_nearest(array,value):
  6. idx = (np.abs(array-value)).argmin()
  7. return array[idx]
  8.  
  9. ############################## Initialize ##################################
  10.  
  11.  
  12. # Some Useful Variables
  13. window_size = 2000 # Size of window to be used for detecting silence
  14. beta = 1 # Silence detection parameter
  15. max_notes = 100 # Maximum number of notes in file, for efficiency
  16. sampling_freq = 44100 # Sampling frequency of audio signal
  17. threshold = 200
  18. array = [27.5000 ,29.1352,30.8677,32.7032,34.6478,36.7081,38.8909,41.2034,43.6535,46.2493,48.9994,51.9131,55.0000,58.2705,61.7354,65.4064,69.2957,73.4162,
  19. 77.7817,82.4069 ,87.3071,92.4986,97.9989,103.8262,110.0000,116.5409,123.4708,130.8128,138.5913,146.8324,155.5635,164.8138,174.6141,184.9972,195.9977, 207.6523, 220.0000,
  20. 233.0819, 246.9417, 261.6256,277.1826, 293.6648,311.1270,329.6276,349.2282,369.9944, 391.9954,415.3047, 440.0000, 466.1638,493.8833,523.2511, 554.3653,
  21. 587.3295,622.2540,659.2551, 698.4565,739.9888, 783.9909, 830.6094,880.0000,932.3275,987.7666,1046.5023,1108.7305,1174.6591, 1244.5079,1318.5102,1396.9129,
  22. 1479.9777,1567.9817,1661.2188, 1760.0000,1864.6550, 1975.5332,2093.0045,2217.4610,2349.3181,2489.0159,2637.0205,2793.8259,2959.9554,3135.9635,3322.4376,
  23. 3520.0000,3729.3101,3951.0664,4186.0090]
  24.  
  25. notes = ['A0','A#0','B0','C1','C#1','D1','D#1','E1','F1' ,'F#1','G1','G#1','A1','A#1', 'B1','C2','C#2','D2', 'D#2','E2','F2', 'F#2','G2', 'G#2','A2','A#2','B2',
  26. 'C3','C#3','D3','D#3','E3','F3','F#3','G3','G#3','A3','A#3','B3','C4','C#4','D4','D#4','E4','F4','F#4','G4','G#4','A4','A#4','B4','C5','C#5','D5','D#5',
  27. 'E5','F5','F#5','G5','G#5','A5','A#5','B5','C6','C#6','D6','D#6','E6','F6','F#6','G6','G#6','A6','A#6','B6','C7','C#7','D7','D#7','E7','F7','F#7','G7',
  28. 'G#7','A7','A#7','B7','C8']
  29.  
  30. Identified_Notes = []
  31.  
  32. ############################## Read Audio File #############################
  33. print ('\n\nReading Audio File...')
  34.  
  35. sound_file = wave.open('Audio_2.wav', 'r')
  36. file_length = sound_file.getnframes()
  37.  
  38. sound = np.zeros(file_length)
  39. mean_square = []
  40. sound_square = np.zeros(file_length)
  41. for i in range(file_length):
  42. data = sound_file.readframes(1)
  43. data = struct.unpack("<h", data)
  44. sound[i] = int(data[0])
  45.  
  46. sound = np.divide(sound, float(2**15)) # Normalize data in range -1 to 1
  47.  
  48.  
  49. ######################### DETECTING SCILENCE ##################################
  50.  
  51. sound_square = np.square(sound)
  52. frequency = []
  53. dft = []
  54. i = 0
  55. j = 0
  56. k = 0
  57. # traversing sound_square array with a fixed window_size
  58. while(i<=len(sound_square)-window_size):
  59. s = 0.0
  60. j = 0
  61. while(j<=window_size):
  62. s = s + sound_square[i+j]
  63. j = j + 1
  64. # detecting the silence waves
  65. if s < threshold:
  66. if(i-k>window_size*4):
  67. dft = np.array(dft) # applying fourier transform function
  68. dft = np.fft.fft(sound[k:i])
  69. dft = np.argsort(dft)
  70.  
  71. if(dft[0]>dft[-1] and dft[1]>dft[-1]):
  72. i_max = dft[-1]
  73. elif(dft[1]>dft[0] and dft[-1]>dft[0]):
  74. i_max = dft[0]
  75. else :
  76. i_max = dft[1]
  77. # claculating frequency
  78. frequency.append((i_max*sampling_freq)/(i-k))
  79. dft = []
  80. k = i+1
  81. i = i + window_size
  82.  
  83. print('length',len(frequency))
  84. print("frequency")
  85. avg=0;
  86. for i in frequency :
  87. print(i)
  88. avg+=avg
  89. idx = (np.abs(array-i)).argmin()
  90. Identified_Notes.append(notes[idx])
  91. print(Identified_Notes)
Add Comment
Please, Sign In to add comment