File size: 11,146 Bytes
e775f6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c844390
e775f6d
 
8078bd7
e775f6d
 
 
 
 
 
 
9980b8e
e775f6d
 
 
 
 
 
 
 
 
 
 
c844390
 
 
36fedf8
c844390
 
 
e775f6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c844390
 
 
e775f6d
 
c844390
e775f6d
 
 
c844390
e775f6d
 
 
 
 
 
 
 
 
01538c1
e775f6d
 
 
 
 
 
 
 
e46d42c
e775f6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e46d42c
 
e775f6d
 
 
 
 
 
 
 
 
 
 
c844390
e775f6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import time
import os
import pickle
import sys
sys.path.append('/')
from src.music.pipeline.music_pipeline import encode_music
from src.music2cocktailrep.pipeline.music2cocktailrep import music2cocktailrep, setup_translation_models, debug_translation
from src.cocktails.pipeline.cocktailrep2recipe import cocktailrep2recipe
from src.debugger import Debugger
from datetime import datetime
from shutil import copy
import streamlit as st
from src.music.config import AUDIO_PATH, MIDI_PATH
from pretty_midi import PrettyMIDI
import numpy as np
import pydub
from PIL import Image
import pytube

st.set_page_config(
    page_title="TastyPiano",
    page_icon="🎹",
)

st.title('TastyPiano')

synestesia_path = 'data/synesthesia/'
debugger = Debugger(verbose=False)
np.random.seed(0)
def centered_module(func, text, args=None):
    _, col2, _ = st.columns([1, 2, 1])
    with col2:
        return func(text)

def centered_button(func, text, args=None):
    _, _, _, col3, _, _, _ = st.columns([1, 1, 1, 1, 1, 1, 1])
    with col3:
        return func(text)

def setup_streamlite():
    path = '/'.join(pytube.__file__.split('/')[:-1]) + '/cipher.py'
    with open(path, 'r') as f:
        cipher = f.read().split('\n')
    cipher[286] = cipher[286].replace(';', '')
    with open(path, 'w') as f:
        f.write('\n'.join(cipher))
    os.path.exists(path)
    setup_translation_models()
    image = Image.open('./data/pianocktail.jpg')
    st.image(image, caption='Pianocktail by Compagnie la Rumeur')

    st.subheader("Ready to taste music?")
    st.markdown("TastyPiano generates music--taste synesthetic experiences by letting you turn any piano song into a cocktail. It is inspired by the"
                " *Pianocktail*, a literary invention from the French novelist, jazz musician, singer and engineer Boris Vian. I see TastyPiano as a digital member of the "
                "Pianocktail species, along with [other](https://www.youtube.com/watch?v=5bdX5i0nAWw) "
                "[wonderful](https://www.youtube.com/watch?v=pzsDOH-xtrs&list=PLC196AA37A2D1C066&index=3) [machines](https://www.youtube.com/watch?v=y0RJg7I2x34).")
    st.markdown("But TastyPiano is different from existing pianocktails. While existing version merely map played notes to drops of corresponding ingredients, "
                "TastyPiano listens to the song, analyzes the music, hallucinates matching tastes and flavors, before it starts generating cocktail recipes "
                "until it finds the one matching its gustatory hallucination. Check my [blog post](https://ccolas.github.io/project/pianocktail) for more information."
                " Hear and taste it for yourself!")
    st.subheader("How to use it?")
    st.markdown("Provide a piano solo input and click on the **Taste it!** button.\n"
                "\nYou can input either: \n* a YouTube url **or**\n* an audio file (.mp3) **or**\n* a midi file (.mid)\n"
                "All these should be **only piano**, no other instrument.\n"
                "Note that audio sources are cropped to the first 40s because the process of "
                "converting it to midi is rather slow (1sec/sec). Midi inputs are taken whole. Please report any bug / buggy url in the community tab.")

    st.subheader("Prepare")
    col1, col2, col3 = st.columns(3)
    generate_audio_from_midi = False
    with col1:
        st.markdown('**YouTube url**')
        url = st.text_area('Type it below', 'https://www.youtube.com/watch?v=UGK70IkP830', height=160)
    with col2:
        st.markdown('**Audio file**')
        audio = st.file_uploader("Upload it here (.mp3)", type=['.mp3'])
    with col3:
        st.markdown('**Midi file**')
        midi = st.file_uploader("Upload it here (.mid)", type=['.mid'])
        generate_audio_from_midi = st.checkbox('Generate audio? Untick if the song is too long (>10min)', value=True)

    #url = "https://www.youtube.com/watch?v=UGK70IkP830"
    #unit# = 'mL'

    def run(unit):
        setup_and_run(unit=unit, url=url, midi=midi, audio=audio, generate_audio_from_midi=generate_audio_from_midi, extra_code=None)
    #run(unit)
    st.markdown('##')
    unit = st.radio('Pick the units (before pressing "Taste it!", default mL)', ['mL', 'oz'], index=0)
    button = centered_button(st.button, 'Taste it!')
    # print(url)
    if button:
        run(unit)


def pianocktail(unit='mL', record=False, url=None, midi=None, audio=None, processed=None, crop=40, verbose=False, debug=False, level=0):
    assert url is not None or midi is not None or audio is not None or processed is not None
    if verbose: print('------\nNew synesthetic exploration!')
    init_time = time.time()
    try:
        with st.spinner("Listening to the song (~1min).."):
            music_ai_rep, music_handcoded_rep, all_paths, error = encode_music(record=record, url=url, audio_path=audio, midi_path=midi, nb_aug=0, noise_injection=False,
                                                                               augmentation=False, processed_path=processed, crop=crop, apply_filtering=False, verbose=verbose,
                                                                               level=level+2)
        if music_ai_rep is not None:
            with st.spinner(text="Thinking about corresponding flavors.."):
                cocktail_rep, affective_cluster_id, affect = music2cocktailrep(music_ai_rep, music_handcoded_rep, verbose=verbose, level=level+2)
            with st.spinner("Trying recipes (15s).."):
                cocktail_recipes, scores = cocktailrep2recipe(cocktail_rep, unit=unit, target_affective_cluster=affective_cluster_id, verbose=verbose, full_verbose=verbose, \
                                                              level=level+2)

            cocktail_recipe = cocktail_recipes[0]
            recipe_score = scores[0]
            if debug:
                music_reconstruction = debug_translation(music_ai_rep)
                debugger.extract_info(all_paths, affective_cluster_id, affect, cocktail_rep, music_reconstruction, recipe_score, verbose=verbose, level=level+2)
                debug_info = debugger.debug_dict
            else:
                debug_info = None
            if verbose:
                print(cocktail_recipe.replace('Recipe', ' ' * (level + 2) + 'Generated recipe:').replace('None ()', ''))
                debugger.print_debug(level=level+2)
                print(f'\nEnd of synesthetic exploration ({int(time.time() - init_time)} secs).\n------')
            st.success('Recipe found!')
        else:
            st.error('Error in listening. Is the url valid? the audio an .mp3? the midi a .mid?')
            cocktail_recipe = None
            debug_info = None
    except Exception as err:
        print(err, error)
        st.error('Error: ' + error)
        cocktail_recipe = None
        debug_info = None
    return cocktail_recipe, debug_info

def setup_and_run(unit='mL', url=None, midi=None, audio=None, generate_audio_from_midi=False, verbose=True, debug=True, extra_code=None):
    if url is None and midi is None and audio is None:
        st.error('Please enter a piano input.')
        assert False
    st.subheader('Synesthesia')
    now = datetime.now()
    folder_name = f'date_{now.year}_{now.month}_{now.day}_time_{now.hour}_{now.minute}_{now.second}'
    folder_path = synestesia_path + folder_name
    if extra_code is not None:
        folder_path += '_' + extra_code
    if os.path.exists(folder_path):
        folder_path += '_2'
    folder_path += '/'
    os.makedirs(folder_path, exist_ok=True)
    if midi is not None:
        st.write(f'  \tReading from midi file: {midi.name}')
        midi_path = MIDI_PATH + 'from_url_midi/' + midi.name[:-4] + '_midi.mid'
        audio_path = AUDIO_PATH + 'from_url/' + midi.name.replace('.mid', '.mp3')
        with open(midi_path, "wb") as f:
            f.write(midi.getbuffer())
        midi = midi_path
        if generate_audio_from_midi:
            midi_data = PrettyMIDI(midi_path)
            audio_data = midi_data.fluidsynth(fs=44100)
            y = np.int16(audio_data * 2 ** 15)
            song = pydub.AudioSegment(y.tobytes(), frame_rate=44100, sample_width=2, channels=1)
            song.export(audio_path, format="mp3", bitrate="320k")
            # st.write(audio_data)
            st.audio(audio_path, format='audio/mp3')
        url = None
    elif audio is not None:
        st.write(f'  \tReading from audio file: {audio.name}')
        audio_path = AUDIO_PATH + 'from_url/' + audio.name
        with open(audio_path, "wb") as f:
            f.write(audio.getbuffer())
        audio = audio_path
        audio_file = open(audio, 'rb')
        audio_bytes = audio_file.read()
        st.audio(audio_bytes, format='audio/mp3')
        url = None
    else:
        st.write(f'  \tReading from YouTube url: {url}')
        st.video(url)

    _, col2, _ = st.columns([1, 1, 1])
    with col2:
        st.markdown('##')
        recipe, debug = pianocktail(unit=unit, url=url, midi=midi, audio=audio, verbose=verbose, debug=debug)
    with open(folder_path + 'debug.pk', 'wb') as f:
        pickle.dump(debug, f)
    with open(folder_path + 'recipe.txt', 'w') as f:
        f.write(recipe)
    paths = debug['all_paths']
    if paths['url'] is not None:
        with open(folder_path + 'url.txt', 'w') as f:
            f.write(paths['url'])
    for k in ['audio_path', 'midi_path']:
        origin = paths[k]
        if origin is not None:
            copy(origin, folder_path + origin.split('/')[-1])

    st.subheader('Recipe')
    recipe = recipe.replace('   Enjoy!', '  \nEnjoy!').replace('\n', '  \n')
    st.text(recipe)

    st.markdown('**About this synesthesia**')
    closest_songs = [debug['nn_music'][i][:-26].split('structured_')[1].replace('_', ' ') for i in range(3)]
    str_songs = 'These are the closest song I know: '
    str_songs += ' '.join([f'  \n* {closest_songs[i]}' for i in range(3)])
    st.markdown(str_songs + '.')
    str_cocktails = 'These are existing cocktails that are close to the taste of this song:'
    str_cocktails += ' '.join([f'  \n* {cocktail_name}: {cocktail_url}' for cocktail_name, cocktail_url in zip(debug['nearest_cocktail_names'][:3],
                                                                                                               debug['nearest_cocktail_urls'][:3])])
    st.markdown(str_cocktails + '.')

if __name__ == '__main__':
    setup_streamlite()
    # urls = ["https://www.youtube.com/watch?v=PLFVGwGQcB0",
    #         "https://www.youtube.com/watch?v=VQmuAr93OlI",
    #         "https://www.youtube.com/watch?v=Nv2GgV34qIg&list=PLO9E3V4rGLD8_iWrCioJRWZXJJE3Fzu_J&index=4",
    #         "https://www.youtube.com/watch?v=qAEIjWYdoYc&list=PLO9E3V4rGLD8_iWrCioJRWZXJJE3Fzu_J&index=1",
    #         "https://www.youtube.com/watch?v=M73x3O7dhmg&list=PLO9E3V4rGLD8_iWrCioJRWZXJJE3Fzu_J&index=5"]
    # setup_translation_models()
    # setup_and_run(url=urls[0], verbose=True, debug=True)
    # recipes = []
    # for url in urls:
    #     recipe = pianocktail(url=url, verbose=True, debug=True)[0]
    #     recipes.append(recipe)
    # stop = 1