예제 #1
0
def get_spectrogram_feature(filepath, train_mode=False):
    (rate, width, sig) = wavio.readwav(filepath)
    wavio.writewav24("test.wav", rate=rate, data=sig)
    sig = sig.ravel()
    sig = trim(sig)

    stft = torch.stft(torch.FloatTensor(sig),
                      N_FFT,
                      hop_length=int(0.01 * SAMPLE_RATE),
                      win_length=int(0.030 * SAMPLE_RATE),
                      window=torch.hamming_window(int(0.030 * SAMPLE_RATE)),
                      center=False,
                      normalized=False,
                      onesided=True)

    stft = (stft[:, :, 0].pow(2) + stft[:, :, 1].pow(2)).pow(0.5)

    amag = stft.clone().detach()

    amag = amag.view(
        -1, amag.shape[0], amag.shape[1]
    )  # reshape spectrogram shape to [batch_size, time, frequency]
    mel = melscale_pytorch.mel_scale(amag,
                                     sample_rate=SAMPLE_RATE,
                                     n_mels=N_FFT // 2 +
                                     1)  # melspec with same shape

    plt.subplot(1, 2, 1)
    plt.imshow(mel.transpose(1, 2).squeeze(), cmap='jet')

    p = 1  # always augment
    randp = np.random.uniform(0, 1)
    do_aug = p > randp
    if do_aug & train_mode:  # apply augment
        print("augment image")
        mel = spec_augment_pytorch.spec_augment(mel,
                                                time_warping_para=80,
                                                frequency_masking_para=54,
                                                time_masking_para=50,
                                                frequency_mask_num=1,
                                                time_mask_num=1)
    feat = mel.view(mel.shape[1],
                    mel.shape[2])  # squeeze back to [frequency, time]
    feat = feat.transpose(0, 1).clone().detach()

    plt.subplot(1, 2, 2)
    plt.imshow(feat, cmap='jet')
    plt.show()  # display it

    del stft, amag, mel
    return feat
예제 #2
0
def extract_drums(attack, decay, onset_array, wavefile):
    """ 
    extracts the individual drum hits from a wav file and writes them as separate wav files.
    attack and decay are in samples instead of milliseconds. So 44 samples is about 1 millisecond 
    for a second recorded at 44.1Khz samplerate.    
    The conditionals here need to be refined since the length of the subsequent wav files appear to be 
    slightly random. It was found that an attack time of 1500 samples works well for drum transients.
    If the transient is too short (2200 samples =~50 ms not including the attack time), the file for 
    that drum hit is not written as it will not be useful.
    """
    read_data = wavio.readwav(wavefile)
    filename = os.path.splitext(wavefile)[0]
    read_array = read_data[
        2]  # a list of sample values in sequential order for the wavefile
    output_csv_filename = '{0}_samples_to_onset.csv'.format(filename)
    samples_to_onset_array = []
    output_dir = 'separated_input'
    for i in range(len(onset_array) - 1):
        if onset_array[i +
                       1] >= len(read_array) and onset_array[i] - attack < 0:
            start = 0
            write_array = read_array[0:]
        elif onset_array[i] - attack < 0:
            start = 0
            write_array = read_array[0:onset_array[i + 1]]
        elif onset_array[i + 1] >= len(read_array):
            start = onset_array[i] - attack
            write_array = read_array[onset_array[i] - attack:]
        else:
            start = onset_array[i] - attack
            write_array = read_array[onset_array[i] - attack:onset_array[i +
                                                                         1]]
        if len(
                write_array
        ) - attack >= decay:  #if the drumhit file is long enough, write it into the unclassified_drums directory
            output_filename = '{0}_{1}.wav'.format(filename, "%05d" % i)
            wavio.writewav24(output_filename, read_data[0], write_array)
            shutil.move(output_filename, '{0}'.format(output_dir))
            samples_to_onset = onset_array[i] - start
            samples_to_onset_array.append([output_filename, samples_to_onset])
    with open(output_csv_filename, 'w') as csvfile:
        a = csv.writer(csvfile)
        a.writerows(samples_to_onset_array)
    shutil.move(output_csv_filename, '{0}'.format(output_dir))
예제 #3
0
    def test1(self):
        path = tempfile.mkdtemp()
        filename = os.path.join(path, "test1data.wav")
        wavio.writewav24(filename, 44100, data1)
        try:
            f = wave.open(filename, 'r')
            self.assertEqual(f.getnchannels(), 1)
            self.assertEqual(f.getsampwidth(), 3)
            self.assertEqual(f.getframerate(), 44100)
            f.close()

            rate, sampwidth, data = wavio.readwav(filename)
            self.assertEqual(rate, 44100)
            self.assertEqual(sampwidth, 3)
            self.assertEqual(data.dtype, np.int32)
            self.assertEqual(data.shape, (len(data1), 1))
            np.testing.assert_equal(data, data1)
        finally:
            os.remove(filename)
            os.removedirs(path)
예제 #4
0
def extract_drums(attack, decay, onset_array, wavefile):
    """ 
    extracts the individual drum hits from a wav file and writes them as separate wav files.
    attack and decay are in samples instead of milliseconds. So 44 samples is about 1 millisecond 
    for a second recorded at 44.1Khz samplerate.    
    The conditionals here need to be refined since the length of the subsequent wav files appear to be 
    slightly random. It was found that an attack time of 1500 samples works well for drum transients.
    If the transient is too short (2200 samples =~50 ms not including the attack time), the file for 
    that drum hit is not written as it will not be useful.
    """
    read_data = wavio.readwav(wavefile)
    filename = os.path.splitext(wavefile)[0]
    read_array = read_data[2] # a list of sample values in sequential order for the wavefile
    output_csv_filename = '{0}_samples_to_onset.csv'.format(filename)
    samples_to_onset_array = []
    output_dir = 'separated_input'
    for i in range(len(onset_array) - 1): 
        if onset_array[i + 1] >= len(read_array) and onset_array[i] - attack < 0:
            start = 0 
            write_array = read_array[0:]
        elif onset_array[i] - attack < 0:
            start = 0 
            write_array = read_array[0: onset_array[i + 1]] 
        elif onset_array[i + 1] >= len(read_array):
            start = onset_array[i] - attack
            write_array = read_array[onset_array[i] - attack:]
        else:
            start = onset_array[i] - attack
            write_array = read_array[onset_array[i] - attack: onset_array[i+1]]
        if len(write_array) - attack >= decay: #if the drumhit file is long enough, write it into the unclassified_drums directory
            output_filename = '{0}_{1}.wav'.format(filename, "%05d" % i)
            wavio.writewav24(output_filename, read_data[0], write_array)
            shutil.move(output_filename, '{0}'.format(output_dir))
            samples_to_onset = onset_array[i] - start
            samples_to_onset_array.append([output_filename, samples_to_onset])
    with open(output_csv_filename, 'w') as csvfile:
        a = csv.writer(csvfile)
        a.writerows(samples_to_onset_array)
    shutil.move(output_csv_filename, '{0}'.format(output_dir))
예제 #5
0
def main():
    """
    writes the new file which will show up in the current directory as {filename}replaced.wav
    at the moment the replacement method is a simple prototype without using any ML yet.
    Once we are able to find the most similar-sounding drumhit this will obviously have to change.
    """
    similarDrums = os.listdir(
        './separated_corpus')  # list of drumhit filenames directory.
    similarDrums = filter(lambda f: '.wav' in f, similarDrums)
    vSpace = vectSpace.vectSpace(
        [], [])  # the vectSpace object to find the closest-souding drumhit.
    csvList = []
    # csvList is made of a list of tuples of form ({filename}, {mfcc numpy array})
    with open(corpus, 'rb') as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            csvList.append(
                [row[0], np.array(map(lambda e: float(e), row[1:]))])
    # create drumVect objects from each entry in csvList and add said drumVect to vSpace
    for elem in csvList:
        dVect = drumVect.drumVect('{0}'.format(elem[0]), elem[1])
        #print type(dVect)
        vSpace.add_vect(dVect)
    # create a drumVect objects from the input .wav
    inputCsvList = []
    with open('input_mfcc.csv', 'rb') as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            inputCsvList.append(
                [row[0], np.array(map(lambda e: float(e), row[1:]))])
    inVectArray = []  #array of drumVect objects from the input .wav
    for elem in inputCsvList:
        dVect = drumVect.drumVect('{0}'.format(elem[0]), elem[1])
        inVectArray.append(dVect)
    w = wavio.readwav(sys.argv[1])
    writeArray = w[
        2]  #a copy of the data from the original file supplied. modifying this.
    hit = 0
    while hit < len(inVectArray):
        if onsets[hit] - attack < 0:
            start = onsets[hit]
        else:
            start = onsets[hit] - 1500
        if hit == len(onsets) - 1:
            nextHit = None
        else:
            nextHit = onsets[hit + 1]
        replacedHit = vSpace.k_closest(
            1, inVectArray[hit])[1].get_filename()  #our replacement hit.
        repl = wavio.readwav(
            './{0}'.format(replacedHit))  #file of replacement hit
        replacedHitArray = repl[2]  #sample array
        if nextHit != None:
            #the replacedHitArray is longer than the distance between current and next hit
            if len(writeArray[start:nextHit + 1]) < len(replacedHitArray):
                writeArray[start:nextHit + 1] = replacedHitArray[
                    0:len(writeArray[start:nextHit + 1])]
            #the replacedHitArray is shorter or equal to distance between current and next hit
            else:
                writeArray[start:start +
                           len(replacedHitArray)] = replacedHitArray
        elif nextHit == None:
            #if the replacedHitArray is longer than the rest of the writeArray or both equal length
            if len(writeArray[start:]) <= len(replacedHitArray):
                writeArray[start:] = replacedHitArray[0:len(writeArray[start:]
                                                            )]
            #if the replacedHitArray is shorter than or equal to the rest of the writeArray
            else:
                writeArray[start:start +
                           len(replacedHitArray)] = replacedHitArray
        hit += 1
        wavio.writewav24(
            '{0}replaced.wav'.format(filename[:len(filename) - 4]), w[0],
            writeArray)  #save the replaced drum file as a new file.
예제 #6
0
    read = csv.reader(c)
    for row in read:
        samples_to_onset_dict[os.path.basename(row[0])] = row[1]

hop_s = win_s / 4
temp_files = []
for wav in wav_files:
    wav_file_path = "separated_input/{0}".format(wav)
    samples_to_onset = int(samples_to_onset_dict[wav])
    readwav = wavio.readwav(wav_file_path)
    readwav_array = readwav[2]
    mfccwav_array = readwav_array[samples_to_onset:samples_to_onset +
                                  samples_for_mfcc]
    temp_filename = 'separated_input/{0}_temp.wav'.format(
        os.path.splitext(wav)[0])
    wavio.writewav24(temp_filename, readwav[0], mfccwav_array)
    temp_files.append(temp_filename)

csv_output = []

for temp_file in temp_files:
    samplerate = 0
    s = a.source(temp_file, samplerate, hop_s)
    samplerate = s.samplerate
    p = a.pvoc(win_s, hop_s)
    m = a.mfcc(win_s, n_filters, n_coeffs, samplerate)
    mfccs = np.zeros([
        n_coeffs,
    ])
    frames_read = 0
    while True:
예제 #7
0
def main():
    """
    writes the new file which will show up in the current directory as {filename}replaced.wav
    at the moment the replacement method is a simple prototype without using any ML yet.
    Once we are able to find the most similar-sounding drumhit this will obviously have to change.
    """
    similarDrums = os.listdir('./separated_corpus') # list of drumhit filenames directory.
    similarDrums = filter(lambda f : '.wav' in f, similarDrums)
    vSpace = vectSpace.vectSpace([],[]) # the vectSpace object to find the closest-souding drumhit.
    csvList = []
    # csvList is made of a list of tuples of form ({filename}, {mfcc numpy array})
    with open(corpus, 'rb') as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            csvList.append([row[0], np.array(map(lambda e : float(e), row[1:]))])
    # create drumVect objects from each entry in csvList and add said drumVect to vSpace
    for elem in csvList:
        dVect = drumVect.drumVect('{0}'.format(elem[0]), elem[1])
        #print type(dVect)
        vSpace.add_vect(dVect)
    # create a drumVect objects from the input .wav
    inputCsvList = []
    with open('input_mfcc.csv', 'rb') as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            inputCsvList.append([row[0], np.array(map(lambda e : float(e), row[1:]))])
    inVectArray = [] #array of drumVect objects from the input .wav
    for elem in inputCsvList:
        dVect = drumVect.drumVect('{0}'.format(elem[0]), elem[1])
        inVectArray.append(dVect)
    w = wavio.readwav(sys.argv[1])
    writeArray = w[2] #a copy of the data from the original file supplied. modifying this.
    hit = 0
    while hit < len(inVectArray):
        if onsets[hit] - attack < 0:
            start = onsets[hit]
        else:
            start = onsets[hit] - 1500
        if hit == len(onsets) - 1:
            nextHit = None
        else: 
            nextHit = onsets[hit + 1] 
        replacedHit = vSpace.k_closest(1, inVectArray[hit])[1].get_filename() #our replacement hit.
        repl = wavio.readwav('./{0}'.format(replacedHit)) #file of replacement hit
        replacedHitArray = repl[2] #sample array
        if nextHit != None:
			#the replacedHitArray is longer than the distance between current and next hit
			if len(writeArray[start: nextHit + 1]) < len(replacedHitArray):
				writeArray[start: nextHit + 1] = replacedHitArray[0: len(writeArray[start: nextHit + 1])]
			#the replacedHitArray is shorter or equal to distance between current and next hit 
			else: 
				writeArray[start: start + len(replacedHitArray)] = replacedHitArray
        elif nextHit == None:
			#if the replacedHitArray is longer than the rest of the writeArray or both equal length
			if len(writeArray[start:]) <= len(replacedHitArray):
				writeArray[start:] = replacedHitArray[0: len(writeArray[start:])]
			#if the replacedHitArray is shorter than or equal to the rest of the writeArray
			else:
				writeArray[start: start + len(replacedHitArray)] = replacedHitArray
        hit += 1 
	wavio.writewav24('{0}replaced.wav'.format(filename[:len(filename) - 4]), w[0], writeArray) #save the replaced drum file as a new file.
예제 #8
0
samples_to_onset_dict = {}
with open('separated_input/{0}'.format(csv_file), 'r') as c:
    read = csv.reader(c)
    for row in read:
        samples_to_onset_dict[os.path.basename(row[0])] = row[1]

hop_s = win_s / 4
temp_files = []
for wav in wav_files:
    wav_file_path = "separated_input/{0}".format(wav)
    samples_to_onset = int(samples_to_onset_dict[wav])
    readwav = wavio.readwav(wav_file_path)
    readwav_array = readwav[2]
    mfccwav_array = readwav_array[samples_to_onset : samples_to_onset + samples_for_mfcc]
    temp_filename = 'separated_input/{0}_temp.wav'.format(os.path.splitext(wav)[0])
    wavio.writewav24(temp_filename, readwav[0],mfccwav_array)
    temp_files.append(temp_filename)

csv_output = []

for temp_file in temp_files:
    samplerate = 0
    s = a.source(temp_file, samplerate, hop_s)
    samplerate = s.samplerate
    p = a.pvoc(win_s, hop_s)
    m = a.mfcc(win_s, n_filters, n_coeffs, samplerate)
    mfccs = np.zeros([n_coeffs,])
    frames_read = 0
    while True:
        samples, read = s()
        spec = p(samples)
예제 #9
0
for csvfile in csv_files:
    with open('{0}/{1}'.format(source_directory, csvfile), 'r') as c:
        reader = csv.reader(c)
        for row in reader:
            samples_to_onset_dict[os.path.basename(row[0])] = row[1]

temp_files = []
for wav in wav_files:
    wav_file_path = "{0}/{1}".format(source_directory, wav)
    samples_to_onset = int(samples_to_onset_dict[wav])
    readwav = wavio.readwav(wav_file_path)
    readwav_array = readwav[2] # sample data from the wav file of interest
    mfccwav_array = readwav_array[samples_to_onset: samples_to_onset + samples_for_mfcc]
    temp_filename = '{0}{1}_temp.wav'.format(source_directory, os.path.splitext(wav)[0])
    wavio.writewav24(temp_filename, readwav[0], mfccwav_array) # temp wav file that the mfcc's are calculated from
    temp_files.append(temp_filename)

csv_output = []

for temp_file in temp_files:
    # run mfcc analysis on the temp file
    samplerate = 0
    s = source(temp_file, samplerate, hop_s)
    samplerate = s.samplerate
    p = pvoc(win_s, hop_s)
    m = mfcc(win_s, n_filters, n_coeffs, samplerate)
    mfccs = zeros([n_coeffs,])
    frames_read = 0
    while True:
        samples, read = s()
예제 #10
0
import wavio
from scipy.signal import decimate

rate, sampwidth,  data=wavio.readwav('/home/eudocio/Escritorio/borrar/EHB/Cuestionarios/121 (AUDIO A).wav')
decimationFactor=2
nframes=len(data)
subSamplingData=decimate(data, decimationFactor, 1,'fir', axis=0)
monoAverage=data.mean(axis=1)
monoLeft=data[:, 0]
monoRight=data[:, 1]
wavio.writewav24('/home/eudocio/Escritorio/borrar/EHB/Cuestionarios/121_AUDIO_A_decimado.wav', 24000, subSamplingData)
wavio.writewav24('/home/eudocio/Escritorio/borrar/EHB/Cuestionarios/121_AUDIO_A_nuevo.wav', 48000, monoAverage)
wavio.writewav24('/home/eudocio/Escritorio/borrar/EHB/Cuestionarios/121_AUDIO_A_izquierdo.wav', 48000, monoLeft)
wavio.writewav24('/home/eudocio/Escritorio/borrar/EHB/Cuestionarios/121_AUDIO_A_derecho.wav', 48000, monoRight)
print(subSamplingData)
예제 #11
0
gmm1 = makeGMM(50,sample,"gmm_jazz_50")
print("学習を実行します。混合数=100")
gmm2 = makeGMM(100,sample,"gmm_jazz_100")   #100以降はメモリエラーが発生する場合あり
'''

#高階調化処理
wav = read_wav_cd("../wav/Moanin.wav",BEGIN_FLAME,END_FLAME)

print("変換する振幅データを読み込みます。")
cdAmp_l = makeAmpArrayCD(wav['amp_l'], BEGIN_FLAME, END_FLAME)  #Lチャネル
#cdAmp_r = makeAmpArrayCD(wav['amp_r'], BEGIN_FLAME, END_FLAME)  #Rチャネル

#変換前
inputArray = []
for j in cdAmp_l : inputArray.extend(j * 5000000.0)
wavio.writewav24("input.wav", 96000, inputArray)

#print("正解振幅データを作成しています。")   #正解を見たい時のオプション
#originAmp = makeAmpArrayHi(wav['amp_l'], BEGIN_FLAME, END_FLAME)
#origin = []
#for j in originAmp : origin.extend(j * 5000000.0)

gmm = joblib.load("gmm_jazz_100")    #GMMファイルの読み込み
debug_gmm(gmm)

print("Lチャネルを変換しています。")
result_l = convertAmp(gmm, cdAmp_l, 50)
#print("Rチャネルを変換しています。")
#result_r = convertAmp(gmm, cdAmp_r, components)
print("\n変換が終了しました。\n")