def set_name(cls, objs, name, extras={}):
        if len(objs) != 1:
            raise CustomAssertionError(
                'Can\'t set the same name to more than 1 song.')
        obj = objs[0]

        is_unique = not AudioFile.objects.filter(database=obj.database,
                                                 name=name).exists()
        if not is_unique:
            raise CustomAssertionError('File {} already exists'.format(name))

        # If audio file is original, change the actual audio files' names as well
        if obj.is_original():
            old_name = obj.name
            old_name_wav = wav_path(obj)
            old_name_compressed = audio_path(obj,
                                             settings.AUDIO_COMPRESSED_FORMAT)

            try:
                obj.name = name
                obj.save()
                new_name_wav = wav_path(obj)
                new_name_compressed = audio_path(
                    obj, settings.AUDIO_COMPRESSED_FORMAT)

                os.rename(old_name_wav, new_name_wav)
                os.rename(old_name_compressed, new_name_compressed)
            except Exception as e:
                obj.name = old_name
                obj.save()
                raise CustomAssertionError('Error changing name')
        else:
            obj.name = name
            obj.save()
Beispiel #2
0
def delete_audio_files_async(*args, **kwargs):
    audio_files = AudioFile.fobjs.filter(active=False)

    # Mark all segments belong to these audio files as to be deleted, then delete them
    segments = Segment.objects.filter(audio_file__in=audio_files)
    segments.update(active=False)
    delete_segments_async()

    # Now delete the audio files
    audio_files_ids = audio_files.values_list('id', flat=True)

    ExtraAttrValue.objects.filter(attr__klass=AudioFile.__name__,
                                  owner_id__in=audio_files_ids).delete()

    # If the audio file is not original - just delete the model
    # Otherwise, search if there are clones. If there are, make one of the clones the new original
    # If there is no clone, delete the real audio files (wav and mp4)

    for af in audio_files:
        if af.original is None:
            clones = AudioFile.objects.filter(original=af).order_by('id')
            first_clone = clones.first()

            # If there are clones, make the first clone original of the remaining
            # Also move the real audio file to the database's folder of the clone
            if first_clone:
                old_wav_file = wav_path(af)
                old_mp4_file = audio_path(af, settings.AUDIO_COMPRESSED_FORMAT)

                clones.update(original=first_clone)
                first_clone.original = None
                first_clone.save()

                new_wav_file = wav_path(first_clone)
                new_mp4_file = audio_path(first_clone,
                                          settings.AUDIO_COMPRESSED_FORMAT)

                os.rename(old_wav_file, new_wav_file)
                os.rename(old_mp4_file, new_mp4_file)

            # Otherwise, delete the audio files too
            else:
                wav = wav_path(af)
                mp4 = audio_path(af, settings.AUDIO_COMPRESSED_FORMAT)
                if os.path.isfile(wav):
                    os.remove(wav)
                if os.path.isfile(mp4):
                    os.remove(mp4)
        af.delete()
Beispiel #3
0
def get_audio_file_url(request):
    user = request.user

    file_id = get_or_error(request.POST, 'file-id')
    audio_file = get_or_error(AudioFile, dict(id=file_id))
    assert_permission(user, audio_file.database, DatabasePermission.VIEW)

    # The audio file might have sample rate being faked - this is the only sample rate value the browser can see.
    # It has no idea what the real fs is unless we tell it.
    # However, when converted to MP3, the real fs can be changed anyways. For example, 44100Hz (wav) -> 48000 (mp3)
    # in which case there is a difference in real_fs and what the browser can see.
    # In this case we must tell the browser to use 48000 as the real_fs of the mp3 file.
    # We do that by omitting real_fs (returning NULL to the browser)
    real_fs = None
    if audio_file.fake_fs is not None:
        real_fs = audio_file.fs

    if audio_file.fs > 48000:
        real_fs = audio_file.fs

    return {
        'url':
        audio_path(audio_file, settings.AUDIO_COMPRESSED_FORMAT, for_url=True),
        'real-fs':
        real_fs
    }
    def handle(self, testfile, fmt, *args, **options):

        if testfile is None:
            audio_files = AudioFile.objects.filter(original=None)

            conversion_list = []

            for af in audio_files:
                wav_file_path = wav_path(af)
                conversion_scheme = dict(wav=wav_file_path)

                target_file_path = audio_path(af, fmt)
                conversion_scheme['other'] = (fmt, target_file_path)

                conversion_list.append(conversion_scheme)

            bar = Bar('Converting song ...', max=len(conversion_list))
            for conversion_scheme in conversion_list:
                convert(conversion_scheme, print_stats=False)

                bar.next()
            bar.finish()
        else:
            target_file_path = '/tmp/test-compress-wav.' + fmt
            conversion_scheme = dict(wav=testfile,
                                     other=(fmt, target_file_path))

            convert(conversion_scheme)

            os.remove(target_file_path)
Beispiel #5
0
def compress_data(database):
    import tarfile
    tar = tarfile.open("user_data.tar.gz", "w:gz")

    segments_ids = Segment.objects.filter(
        audio_file__database=database).values_list('id', flat=True)
    audio_files = AudioFile.objects.filter(database=database).values_list(
        'name', flat=True)

    bar = Bar('Zipping ...', max=len(segments_ids) + len(audio_files))

    for s in segments_ids:
        seg_spect_path = spect_fft_path(s, 'syllable')
        seg_mask_path = spect_mask_path('{}'.format(s))

        tar.add(seg_mask_path)
        tar.add(seg_spect_path)
        bar.next()

    for a in audio_files:
        compressed_path = audio_path(a, settings.AUDIO_COMPRESSED_FORMAT)

        if os.path.isfile(compressed_path):
            tar.add(compressed_path)
        bar.next()

    tar.close()
    bar.finish()
def get_audio_file_url(request):
    user = request.user

    file_id = get_or_error(request.POST, 'file-id')
    audio_file = get_or_error(AudioFile, dict(id=file_id))
    assert_permission(user, audio_file.database, DatabasePermission.VIEW)

    return audio_path(audio_file,
                      settings.AUDIO_COMPRESSED_FORMAT,
                      for_url=True)
Beispiel #7
0
def import_pcm(song, cur, audio_file, wav_file_path=None, compressed_url=None):
    if wav_file_path is None:
        wav_file_path = wav_path(audio_file)
    if compressed_url is None:
        compressed_url = audio_path(audio_file,
                                    settings.AUDIO_COMPRESSED_FORMAT)

    if not os.path.isfile(wav_file_path):
        # print('Importing {}'.format(song_name))
        song_id = song['songid']
        cur.execute('select wav from wavs where songid={};'.format(song_id))

        data = cur.fetchone()
        raw_pcm = str_to_bytes(data[0])

        nchannels = song['stereo']
        bitrate = int(song['ssizeinbits'])
        fs = int(song['samplerate'])

        byte_per_frame = int(bitrate / 8)
        nframes_all_channel = int(len(raw_pcm) / byte_per_frame)
        nframes_per_channel = int(nframes_all_channel / nchannels)
        length = nframes_per_channel
        ensure_parent_folder_exists(wav_file_path)

        if bitrate == 24:
            array1 = np.frombuffer(raw_pcm, dtype=np.ubyte)
            array2 = array1.reshape((nframes_per_channel, nchannels,
                                     byte_per_frame)).astype(np.uint8)
            wf.write_24b(wav_file_path, fs, array2)
        else:
            data = array.array('i', raw_pcm)
            sound = pydub.AudioSegment(data=data,
                                       sample_width=byte_per_frame,
                                       frame_rate=fs,
                                       channels=nchannels)
            sound.export(wav_file_path, 'wav')
    else:
        fs, length = get_wav_info(wav_file_path)

    if not os.path.isfile(compressed_url):
        ensure_parent_folder_exists(compressed_url)
        sound = pydub.AudioSegment.from_wav(wav_file_path)
        sound.export(compressed_url, format=settings.AUDIO_COMPRESSED_FORMAT)

    return fs, length
Beispiel #8
0
def get_audio_files_urls(request):
    user = request.user

    file_ids = get_or_error(request.POST, 'file-ids')
    file_ids = json.loads(file_ids)
    format = request.POST.get('format', settings.AUDIO_COMPRESSED_FORMAT)
    audio_files = AudioFile.objects.filter(id__in=file_ids)
    database_ids = audio_files.values_list('database', flat=True).distinct()
    databases = Database.objects.filter(id__in=database_ids)
    for database in databases:
        assert_permission(user, database, DatabasePermission.VIEW)

    file_paths = []
    for audio_file in audio_files:
        file_path = audio_path(audio_file, format, for_url=True)
        file_paths.append(file_path)

    return file_paths
Beispiel #9
0
    def handle(self, testfile, tofile, fmt, *args, **options):

        if fmt is None:
            if tofile is None:
                raise Exception("Either format or --to-file must be provided")
            else:
                fmt = tofile.split('.')[-1]

        print("Format = {}".format(fmt))

        if testfile is None:
            audio_files = AudioFile.objects.filter(original=None)

            conversion_list = []

            for af in audio_files:
                wav_file_path = wav_path(af)
                conversion_scheme = dict(wav=wav_file_path)

                target_file_path = audio_path(af, fmt)
                conversion_scheme['other'] = (fmt, target_file_path)

                conversion_list.append(conversion_scheme)

            bar = Bar('Converting song ...', max=len(conversion_list))
            for conversion_scheme in conversion_list:
                convert(conversion_scheme, print_stats=False)

                bar.next()
            bar.finish()
        else:

            if tofile is None:
                target_file_path = '/tmp/test-compress-wav.' + fmt
                conversion_scheme = dict(wav=testfile,
                                         other=(fmt, target_file_path))
                convert(conversion_scheme)
                os.remove(target_file_path)
            else:
                conversion_scheme = dict(wav=testfile, other=(fmt, tofile))
                convert(conversion_scheme)