My small utility to convert WAVs to Table 16b (for convolution)


#1

Hello good people of the community,

I just wanted to share with you a small Python utility that I wrote. It's simple but very useful. It reads a .WAV file (Mono, 16bit, 48khz) and spits out the first 1024 frames as a list/array that you can simply copy/paste your your table 16b block. I use it for pre-loading an impulse response without having to fetch it from an SD Card.

Since I can't upload the .py file here, I'll simply copy the copy to a preformatted text box here:

import wave
import tkinter as tk
from tkinter import filedialog, messagebox


def conv_int(num):
    if num > 32768:
        return -(65536 - num)
    else:
        return num


def main():
    root = tk.Tk()
    root.withdraw()

    file_path = filedialog.askopenfilename()
    wave_obj = wave.open(file_path, mode='rb')

    # Is WAV file mono?
    if wave_obj.getnchannels() == 1 and wave_obj.getsampwidth() == 2:

        # Issue warning if not 48khz
        if wave_obj.getframerate() != 48000:
            print('Warning: Sample Rate not 48000.')

        # Issue Warning for too long
        if wave_obj.getnframes() > 1024:
            print('Warning: Impulse WAV file is too long, it will be truncated at 1024 samples.')

        # Print Info
        print(f'WAV File info: {wave_obj.getparams()}\n')

        # Read WAV byte content
        byte_content = wave_obj.readframes(1024)

        # Convert those into 16bit INTs
        list_16bits = [byte_content[i + 1] << 8 | byte_content[i] for i in range(0, len(byte_content), 2)]

        for index, num in enumerate(list_16bits):
            num = conv_int(num)
            print(f'array[{index}]={num};')

    # WAV file not mono.
    else:
        print('WAV file must be mono/one channel and 16 bit format.')

    messagebox.showinfo(title='Information', message='Done, please copy the output of the console to your table 16b object now.')


main()

Let me know if it useful for you, cheers!

Marc aka Khorus


#2

fantastic tool, works like a charm, thank you!


#3

Thanks, this looks pretty useful. I am not familiar with Python but I will give this a try and report back from the perspective of a total noob.