<jupyter_start><jupyter_text>Aby zacząć pracę w Colab należy podłączyć Google Drive i wykonać poniższą komórkę<jupyter_code>!pip install soundfile
cd "/content/drive/My Drive/GIT/Music_ML_analyzing/"
import os
import pandas as pd
import numpy as np

import soundfile as sf
import librosa
def transpose_wav_file(path_input, path_output, n_steps):
  x, sr = librosa.load(path_input)
  y = librosa.effects.pitch_shift(x, sr, n_steps=n_steps)
  sf.write(path_output, y, sr, 'PCM_16')
  #print(f'File {path_input} was transposed to file {path_output}')
  return None
    
def chtime_wav_file(path_input, path_output, rate):
                           
  x, sr = librosa.load(path_input)
  y = librosa.effects.time_stretch(x, rate)
  sf.write(path_output, y, sr, 'PCM_16')
  #print(f'File {path_input} was chtime to file {path_output}')
        
  return None
HOME_DIR = '.'
DATA_DIR = os.path.join(HOME_DIR, 'data')
INPUT_DIR = DATA_DIR
OUTPUT_DIR = os.path.join(HOME_DIR,'augument_data')
print(os.path.realpath(INPUT_DIR))
print(os.path.realpath(OUTPUT_DIR))
def clone_input_dir_structure(INPUT_DIR, OUTPUT_DIR):
    for dirpath, dirnames, filenames in os.walk(INPUT_DIR):
        structure = os.path.realpath(os.path.join(OUTPUT_DIR, dirpath[len(INPUT_DIR)+1:]))
        if not os.path.isdir(structure):
            os.mkdir(structure)
        else:
            print(f"{structure}\nThis folder does already exits! \n ----------------------------------------------- \n")
clone_input_dir_structure(INPUT_DIR, OUTPUT_DIR)
def create_new_data_from_raw_input(INPUT_DIR, OUTPUT_DIR):
    for dirpath, dirnames, filenames in os.walk(INPUT_DIR):
            for filename in filenames: 
                if '.wav' in filename:
                    output_path = os.path.join(OUTPUT_DIR, dirpath[len(INPUT_DIR)+1:], filename)
                    transpose_wav_file(path_input = os.path.join(dirpath, filename),
                                               path_output = output_path,
                                               n_steps=0)
                    
create_new_data_from_raw_input(INPUT_DIR, OUTPUT_DIR)
def generate_transpose_matrix(labels):
    result_mtx = pd.DataFrame(labels)
    for idx, label in enumerate(labels):
        labels.append(labels.pop(0))
        result_mtx[idx+1] = pd.Series(labels)
    return result_mtx
if not os.path.exists(os.path.join(OUTPUT_DIR, 'labels')):
    os.mkdir(os.path.join(OUTPUT_DIR, 'labels'))

for label in ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']:
    if not os.path.exists(os.path.join(OUTPUT_DIR, 'labels', label)):
        os.mkdir(os.path.join(OUTPUT_DIR, 'labels', label))<jupyter_output><empty_output><jupyter_text>Transponujemy nagrania do odpowiednich tonacji<jupyter_code>labels = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
transpose_matrix = generate_transpose_matrix(labels)

for dirpath, dirnames, filenames in os.walk(os.path.join(OUTPUT_DIR, 'chords', 'major')):
        for filename in filenames:
            
            filename_tonation = filename.split('.')[0]
            transpose_row = transpose_matrix[transpose_matrix[0] == filename_tonation]
            
            for step in transpose_row.columns:
                output_path = os.path.join(OUTPUT_DIR, 'labels', transpose_row[step].values[0], (dirpath.split(os.path.sep))[-2] + '_' + (dirpath.split(os.path.sep))[-1] + '_from_{}'.format(filename_tonation) + '.wav' )
                transpose_wav_file(path_input = os.path.join(dirpath, filename),
                                           path_output= output_path, 
                                           n_steps=step)<jupyter_output><empty_output><jupyter_text>Generujemy dane w różnych tempach<jupyter_code>for dirpath, dirnames, filenames in os.walk(os.path.join(OUTPUT_DIR, 'labels')):
    for filename in filenames:
        
        filename_wo_wav = filename.split('.')[0]
                
        for time_rate in np.linspace(0.75, 1.25, 11):
            
            output_path = os.path.join(dirpath, filename_wo_wav + '_{}'.format(str(time_rate).replace('.','_')) + '.wav')
            chtime_wav_file(path_input = os.path.join(dirpath, filename),
                                    path_output= output_path,
                                    rate = time_rate)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1392288155160471, 'fraction_numerical': 0.005310551835603787, 'mean_word_length': 2.7801047120418847, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '117922', 'license': 'MIT', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1468, 'n_words': 276, 'path': 'Music_Data_Augumentation.ipynb', 'repo_name': 'AdiSk325/Music_ML_analyzing', 'size': '4331'}