示例#1
0
文件: views.py 项目: jkyl/conv_reverb
def home(request):
    '''
    Function to create objects of django forms and render them on the browser. Also, accesses user inputs and calls necessary functions 
    to process inputs and return results on the browser webpage. 
    '''
    res = None
    if request.method == 'GET' and 'Search' in request.GET:
        # create a form instance and populate it with data from the request:
        query_API = SearchForm(request.GET)
        resform = PickResForm()
        # check whether it's valid:
        if query_API.is_valid():
            # Convert form data to an args dictionary for API_backend
            args = {}
            if query_API.cleaned_data['Title']:
                args['Title'] = query_API.cleaned_data['Title']
            if query_API.cleaned_data['Creator']:
                args['Creator'] = query_API.cleaned_data['Creator']
            if query_API.cleaned_data['Description']:
                args['Description'] = query_API.cleaned_data['Description']
            
            try:
                res = query_catalog(args)
                res = format_output(res)
                resform = PickResForm()
            except Exception as e:
                print('Exception caught')
                bt = traceback.format_exception(*sys.exc_info()[:3])
                context['err'] = """
                An exception was thrown in API_backend:
                <pre>{}
{}</pre>
                """.format(e, '\n'.join(bt))
                res = None     
             
    else:
        query_API = SearchForm()
        resform = PickResForm()
    if request.method == 'GET' and 'Download' in request.GET:
        resform = PickResForm(request.GET)
        if resform.is_valid():
            file_id =resform.cleaned_data['ids']
            if file_id == '':
                message1 = "Download not possible without ID. Please enter aporee ID to download file"
            else:    
                download = download_item(file_id)
                #### here's where we cp -jk ####
                os.system('cp ../conv_reverb/download_files/{} ../conv_reverb/download_files/\!JUST_DOWNLOADED.mp3'.format(download)) 
                if download != None:
                    message1 = "File " + download + " downloaded to conv_reverb/conv_reverb/download_files."
                else:
                    message1 = "Sorry, download failed: file size is too large"  
            context['message1'] = message1
    
    trans_form = Transform_Input()
    
    if request.method == 'GET' and 'Transform' in request.GET:
        trans_form = Transform_Input(request.GET)
        impulse_files = os.listdir(Impulse_path)
        download_files = os.listdir(Dload_path)
        trans_aud_list = os.listdir(Trans_aud_path)
        if trans_form.is_valid():
            sound_in = trans_form.cleaned_data['downloads'] + trans_form.cleaned_data['impulses'] + trans_form.cleaned_data['trans']
            print(sound_in)
            ps_percent = trans_form.cleaned_data['ps_percent']
            mod_freq = trans_form.cleaned_data['mod_freq']
            del_time = trans_form.cleaned_data['del_time']
            dry_wet = trans_form.cleaned_data['dry_wet']
            feedback = trans_form.cleaned_data['feedback']
            for i in range(len(sound_in)):
                if sound_in[i] in impulse_files:
                    sound_in[i] = Impulse_path + '/' + sound_in[i] 
                elif sound_in[i] in download_files:
                    sound_in[i] = Dload_path + '/' + sound_in[i]
                else:
                    sound_in[i] = Trans_aud_path + '/' + sound_in[i]

                    
            print(sound_in)
            sound = Audio(sound_in[0])
            print('saving dry')
            sound.write_to_wav(dry = True)
            print('saved')
            sound.plot_fft_spectrum(dry = True)
            message2 = "Dry file saved as \"{}.wav\"".format(sound.title)
            context['message2'] = message2
            
            # execute convolution of user specified audio files
            if trans_form.cleaned_data['process'] == 'Convolution':
                
                sound1 = Audio(sound_in[1])
                conv_sound = sound.convolve(sound1)
                new_trans = Trans_aud_path + '/' + conv_sound.title + '.wav'
                conv_sound.write_to_wav()
                conv_sound.plot_fft_spectrum()
                message2 = "Transformed file saved as \"{}.wav\"".format(conv_sound.title)
                
            # execute pitch shift of user specified audio file and percent
            if trans_form.cleaned_data['process'] == 'Pitch Shift':
                
                ps_sound = sound.pitchshift(ps_percent)
                ps_sound.write_to_wav()
                ps_sound.plot_fft_spectrum()
                message2 = "Transformed file saved as \"{}.wav\"".format(ps_sound.title)
                
            # execute ring modulation of user specified audio file at desired frequency 
            if trans_form.cleaned_data['process'] == 'Ring Modulation':
                
                rm_sound = sound.ringmod(mod_freq)
                rm_sound.write_to_wav()
                rm_sound.plot_fft_spectrum()
                message2 = "Transformed file saved as \"{}.wav\"".format(rm_sound.title)
                
            # execute ring modulation of user specified audio file at desired frequency 
            if trans_form.cleaned_data['process'] == 'Delay':
                
                del_sound = sound.delay((del_time, dry_wet, feedback))
                del_sound.write_to_wav()
                del_sound.plot_fft_spectrum()
                message2 = "Transformed file saved as \"{}.wav\"".format(del_sound.title)
            

    


    # Handle different responses of res
    if res is None:
        context['result'] = None

    else:
        columns, result = res

        # Wrap in tuple if result is not already
        if result and isinstance(result[0], str):
            result = [(r,) for r in result]
         
        context['result'] = result
        context['num_results'] = len(result)
        context['columns'] = [COLUMN_NAMES.get(col, col) for col in columns]
        
    context['query_API'] = query_API
    context['resform'] = resform
    context['transform'] = trans_form
    return render(request, 'index.html', context)