Beispiel #1
0
def change_fs_without_resampling(wav_file, new_fs, new_name):
    """
    Create a new wav file with the a new (fake) sample rate, without changing the actual data.
    This is necessary if the frequency of the wav file is higher than the maximum sample rate that the browser supports
    :param wav_file: path to the original wav file
    :param new_fs: the new sample rate
    :return: the path of the faked wav file
    """
    size, comp, num_channels, rate, sbytes, block_align, bitrate, bytes, dtype = read_wav_info(
        wav_file)
    ubyte_data = read_segment(wav_file,
                              0,
                              None,
                              normalised=False,
                              retype=False)
    byte_length = ubyte_data.size
    nframes_per_channel = byte_length // block_align
    byte_per_frame = bitrate // 8

    uint8_data = ubyte_data.reshape(
        (nframes_per_channel, num_channels, byte_per_frame)).astype(np.uint8)

    if bitrate == 24:
        write_24b(new_name, new_fs, uint8_data)
    else:
        write(new_name, new_fs, uint8_data, bitrate=bitrate)
Beispiel #2
0
def merge_audio_chunks(request):
    """
    This action should be called after the last audio chunk is uploaded.
    It will merge all the saved chunks (foo.wav__1, foo.wav__2, etc...) into foo.wav
    And import to the database
    :param request:
    :return:
    """
    user = request.user
    params = request.POST
    name = params['name']
    chunk_count = int(params['chunkCount'])
    max_fs = int(request.POST.get('browser-fs', 0))

    if name.lower().endswith('.wav'):
        name = name[:-4]

    database_id = get_or_error(request.POST, 'database')
    database = get_or_error(Database, dict(id=database_id))
    assert_permission(user, database, DatabasePermission.ADD_FILES)

    wav_file_path = data_path('audio/wav/{}'.format(database_id),
                              name + '.wav')

    with open(wav_file_path, 'wb') as combined_file:
        for i in range(chunk_count):
            chunk_file_path = wav_file_path + '__' + str(i)
            with open(chunk_file_path, 'rb') as chunk_file:
                combined_file.write(chunk_file.read())

    size, comp, num_channels, fs, sbytes, block_align, bitrate, bytes, dtype = read_wav_info(
        wav_file_path)
    if comp == 3:
        warning('File is IEEE format. Convert to standard WAV')
        audio = pydub.AudioSegment.from_file(wav_file_path)
        audio.export(wav_file_path, format='wav')

    audio_file = _import_and_convert_audio_file(database, combined_file,
                                                max_fs)

    for i in range(chunk_count):
        chunk_file_path = wav_file_path + '__' + str(i)
        os.remove(chunk_file_path)

    added_files = AudioFile.objects.filter(id=audio_file.id)
    _, rows = get_sequence_info_empty_songs(added_files)
    return dict(origin='merge_audio_chunks',
                success=True,
                warning=None,
                payload=rows)
Beispiel #3
0
def convert_wav_f32_to_i32(apps, schema_editor):
    """
    """
    db_alias = schema_editor.connection.alias
    audio_file = apps.get_model('koe', 'AudioFile')

    afs = audio_file.objects.using(db_alias).filter(original=None)

    slashed_url = os.path.join(settings.MEDIA_URL, 'audio/wav/{}', '{}.wav')
    unslashed_url = slashed_url[1:]
    wav_path_template = os.path.join(settings.BASE_DIR, unslashed_url)

    wavs_to_convert = []
    for audio_file in afs:
        database_id = audio_file.database.id
        file_name = audio_file.name
        wav_file_path = wav_path_template.format(database_id, file_name)

        if os.path.isfile(wav_file_path):
            size, comp, num_channels, fs, sbytes, block_align, bitrate, bytes, dtype = read_wav_info(
                wav_file_path)
            if comp == 3:
                wavs_to_convert.append(wav_file_path)

    sys.stdout.write('\n')
    sys.stdout.write(
        '\t\tFound {} WAV files using IEEE format. Now converting to standard format'
        .format(len(wavs_to_convert)))

    for wav_file_path in wavs_to_convert:
        audio = pydub.AudioSegment.from_file(wav_file_path)
        audio.export(wav_file_path, format='wav')