import numpy as np import librosa import math from tensorflow import keras import streamlit as st import time import pyautogui def get_mfcc(audio_signal, num_mfcc=13, n_fft=2048, hop_length=512, num_segments=5): new_data = { "mfcc": [] } SAMPLE_RATE = 22050 signal,sample_rate = librosa.load(audio_signal,sr = SAMPLE_RATE) TRACK_DURATION = int(librosa.get_duration(signal)) # measured in seconds SAMPLES_PER_TRACK = SAMPLE_RATE * TRACK_DURATION samples_per_segment = int(SAMPLES_PER_TRACK / num_segments) num_mfcc_vectors_per_segment = math.ceil(samples_per_segment / hop_length) for d in range(num_segments): start = samples_per_segment * d finish = start + samples_per_segment mfcc = librosa.feature.mfcc(signal[start:finish], sample_rate, n_mfcc=num_mfcc, n_fft=n_fft, hop_length=hop_length) mfcc = mfcc.T if len(mfcc) == num_mfcc_vectors_per_segment: new_data["mfcc"].append(mfcc.tolist()) return new_data["mfcc"] def prediction(mfcc): cnn_model = keras.models.load_model('music-gen-classify-v1.h5') mfcc = np.array(mfcc) mfcc = mfcc[...,np.newaxis] prediction = cnn_model.predict(mfcc) return max(np.argmax(prediction,axis = 1)) def get_genre(prediction): pred = '' if prediction == 0: pred = 'Blues' elif prediction == 1: pred = 'Classical' elif prediction == 2: pred = 'Country' elif prediction == 3: pred = 'Disco' elif prediction == 4: pred = 'Hip Hop' elif prediction == 5: pred = 'Jazz' elif prediction == 6: pred = 'Metal' elif prediction == 7: pred = 'Pop' elif prediction == 8: pred = 'Reggae' elif prediction == 9: pred = 'Rock' return pred def main(): st.set_page_config(layout='wide',page_title='Klasifikasi Genre Musik',page_icon='🎵') st.title('Prediksi Genre Musik') st.markdown('Website ini bisa memprediksi hingga 10 genre yaitu Blues, Classical, Country, Disco, Hiphop,Jazz, Metal, Pop, Reggae, dan Rock. Tentu prediksi genre ini tidak selalu tepat karena lagu itu multi genre, tapi selamat mencoba!') selected_item = st.selectbox('Pilih Sample Musik Atau Upload Musikmu !',['Sample Musik','Upload']) if selected_item is not None: if selected_item == 'Upload': files = st.file_uploader('Format Musik ".wav" berdurasi maksimal 30sec', type='wav', accept_multiple_files=False) if files is not None: audio,sr = librosa.load(files,sr = 22050) duration = int(librosa.get_duration(audio)) if 'file_uploaded' not in st.session_state: st.session_state['file_uploaded'] = True for percent_complete in range(100): time.sleep(0.01) bar.progress(i+1) i = i+1 pyautogui.hotkey('ctrl', 'F5') elif st.session_state['file_uploaded'] == True: st.audio(files, format="audio/wav", start_time=0) elif selected_item == 'Sample Musik': selected_file = st.selectbox("Pilih Sample", ['Blues','Jazz','Country','Classical','Hiphop','Metal','Pop','Reggae','Rock']) files = f'Data/upload/user/{selected_file}.wav' st.audio(files, format="audio/wav", start_time=0) submitted = st.button("Prediksi") if submitted: with st.spinner('Tunggu Sebentar'): signal = files mfcc_for_track = get_mfcc(signal) predict = prediction(mfcc_for_track) genre = get_genre(int(predict)) st.success('Proses Prediksi Selesai!') st.markdown(f'Genre Musikmu adalah 🎵 : Musik **{genre}** ') if __name__ == '__main__': main()