Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 7.1 + stereo HeSuVi HRIR generator
- # Made by AyPeeEye in collaboration with ChatGPT
- # requires the library soundfile-0.12.1 and Python version 3.8 (may work with other versions but no guarantees)
- import tkinter as tk
- from tkinter import filedialog
- import soundfile as sf
- # Function to create and write to a peace preset output file
- def generate_hrir_file(file_paths, output_filename):
- num_channels = 0
- for path in enumerate(file_paths):
- if path[1] != "None":
- num_channels += 1
- if (num_channels != 1) and (num_channels != 2) and (num_channels != 7):
- return 1 # Not a supported speaker configuration
- if num_channels == 2 and (file_paths[1] == "None" or file_paths[2] == "None"):
- return 2 # Wrong channel config for stereo
- if num_channels == 1 and (file_paths[0] == "None"):
- return 5 # Wrong channel config for HeSuVi to stereo
- # HeSuVi to stereo
- if num_channels == 1:
- channel_data, samplerate = sf.read(file_paths[0])
- if len(channel_data[1]) != 14:
- return 6 # Not a 14 channel HeSuVi file
- fl_left = channel_data[:, 0] # Extract left channel
- fl_right = channel_data[:, 1] # Extract right channel
- fr_left = channel_data[:, 7] # Extract left channel
- fr_right = channel_data[:, 8] # Extract right channel
- # Create a 4-channel file by combining left and right channels
- # Here we'll duplicate the left and right channels to create a 4-channel file
- four_channel_data = [fl_left, fl_right, fr_left, fr_right]
- # Reshape the data into a 4-channel format
- four_channel_data = list(map(list, zip(*four_channel_data))) # Interleave the channels
- # Save the 4-channel audio data
- sf.write(output_filename+'.wav', four_channel_data, samplerate, format='WAV', subtype='PCM_24')
- return 0 # Generation finished without errors
- # Stereo HRIR generator
- if num_channels == 2:
- stereo_data_fl, samplerate_0 = sf.read(file_paths[1])
- if stereo_data_fl.shape[1] != 2:
- return 3 # File not stereo
- stereo_data_fr, samplerate_1 = sf.read(file_paths[2])
- if stereo_data_fr.shape[1] != 2:
- return 3 # File not stereo
- if samplerate_0 != samplerate_1:
- return 4 # Not the same sample rate
- fl_left = stereo_data_fl[:, 0] # Extract left channel
- fl_right = stereo_data_fl[:, 1] # Extract right channel
- fr_left = stereo_data_fr[:, 0] # Extract left channel
- fr_right = stereo_data_fr[:, 1] # Extract right channel
- # Create a 4-channel file by combining left and right channels
- # Here we'll duplicate the left and right channels to create a 4-channel file
- four_channel_data = [fl_left, fl_right, fr_left, fr_right]
- # Reshape the data into a 4-channel format
- four_channel_data = list(map(list, zip(*four_channel_data))) # Interleave the channels
- # Save the 4-channel audio data
- sf.write(output_filename+'.wav', four_channel_data, samplerate_0, format='WAV', subtype='PCM_24')
- return 0 # Generation finished without errors
- # 7.1 HRIR generator
- if num_channels == 7:
- stereo_data_fl, samplerate_0 = sf.read(file_paths[1])
- if stereo_data_fl.shape[1] != 2:
- return 3 # File not stereo
- stereo_data_fr, samplerate_1 = sf.read(file_paths[2])
- if stereo_data_fr.shape[1] != 2:
- return 3 # File not stereo
- stereo_data_fc, samplerate_2 = sf.read(file_paths[3])
- if stereo_data_fc.shape[1] != 2:
- return 3 # File not stereo
- stereo_data_rl, samplerate_3 = sf.read(file_paths[4])
- if stereo_data_rl.shape[1] != 2:
- return 3 # File not stereo
- stereo_data_rr, samplerate_4 = sf.read(file_paths[5])
- if stereo_data_rr.shape[1] != 2:
- return 3 # File not stereo
- stereo_data_sl, samplerate_5 = sf.read(file_paths[6])
- if stereo_data_sl.shape[1] != 2:
- return 3 # File not stereo
- stereo_data_sr, samplerate_6 = sf.read(file_paths[7])
- if stereo_data_sr.shape[1] != 2:
- return 3 # File not stereo
- samplerates = [samplerate_0, samplerate_1, samplerate_2, samplerate_3, samplerate_4, samplerate_5, samplerate_6]
- for i in range(1, len(samplerates)):
- if samplerates[0] != samplerates[i]:
- return 4 # Not the same sample rate
- fl_left = stereo_data_fl[:, 0] # Extract left channel
- fl_right = stereo_data_fl[:, 1] # Extract right channel
- fr_left = stereo_data_fr[:, 0] # Extract left channel
- fr_right = stereo_data_fr[:, 1] # Extract right channel
- fc_left = stereo_data_fc[:, 0] # Extract left channel
- fc_right = stereo_data_fc[:, 1] # Extract right channel
- rl_left = stereo_data_rl[:, 0] # Extract left channel
- rl_right = stereo_data_rl[:, 1] # Extract right channel
- rr_left = stereo_data_rr[:, 0] # Extract left channel
- rr_right = stereo_data_rr[:, 1] # Extract right channel
- sl_left = stereo_data_sl[:, 0] # Extract left channel
- sl_right = stereo_data_sl[:, 1] # Extract right channel
- sr_left = stereo_data_sr[:, 0] # Extract left channel
- sr_right = stereo_data_sr[:, 1] # Extract right channel
- # Create a 14-channel file by combining left and right channels
- # Here we'll duplicate the left and right channels to create a 14-channel file
- fourteen_channel_data = [fl_left, fl_right, sl_left, sl_right, rl_left, rl_right, fc_left, fr_right, fr_left, sr_right, sr_left, rr_right, rr_left, fc_right]
- # Reshape the data into a 14-channel format
- fourteen_channel_data = list(map(list, zip(*fourteen_channel_data))) # Interleave the channels
- # Save the 14-channel audio data
- sf.write(output_filename + '.wav', fourteen_channel_data, samplerate_0, format='WAV', subtype='PCM_24')
- return 0 # Generation finished without errors
- # Function to open file explorer and update the corresponding entry field
- def open_file_explorer(entry):
- file_path = filedialog.askopenfilename(title="Select wav file", filetypes=[("WAV files", "*.wav;*.wave")])
- entry.delete(0, tk.END)
- entry.insert(0, file_path)
- message_colors = ["blue", "green"]
- color_index = 0
- message_label = None
- # Function to handle the conversion
- def convert_files():
- global color_index
- global message_label
- output_filename = output_filename_entry.get() or "output"
- paths = [entry.get() or "None" for entry in entry_fields]
- message = generate_hrir_file(paths, output_filename)
- # Clear the previous completion message, if exists
- if message_label is not None:
- message_label.destroy()
- if message == 0:
- # Add a message on the GUI to inform the user that the file generation is complete
- message_label = tk.Label(root, text="File generation completed!", fg=message_colors[color_index])
- message_label.pack()
- # Update the color index for the next message
- color_index = (color_index + 1) % len(message_colors)
- if message == 1:
- # error 1
- message_label = tk.Label(root, text="Not a supported speaker configuration", fg="red")
- message_label.pack()
- if message == 2:
- # error 2
- message_label = tk.Label(root, text="Wrong channel configuration for stereo", fg="red")
- message_label.pack()
- if message == 3:
- # error 3
- message_label = tk.Label(root, text="One of the files was not in stereo format", fg="red")
- message_label.pack()
- if message == 4:
- # error 4
- message_label = tk.Label(root, text="Files do not have the same sample rate", fg="red")
- message_label.pack()
- if message == 5:
- # error 4
- message_label = tk.Label(root, text="Wrong channel for HeSUVi to stereo conversion", fg="red")
- message_label.pack()
- if message == 6:
- # error 4
- message_label = tk.Label(root, text="HeSuVi file does not have 14 channels", fg="red")
- message_label.pack()
- # Creating the main application window
- root = tk.Tk()
- root.title("IR wav HRIR Converter")
- # List to store entry fields and their respective names
- entry_fields = []
- field_names = ["HeSuVi", "Left", "Right", "Center", "Left Rear", "Right Rear", "Left Side", "Right Side"]
- # Function to create file input fields with respective names
- def create_file_input(name):
- frame = tk.Frame(root)
- frame.pack()
- label = tk.Label(frame, text=f"{name}:")
- label.pack(side=tk.LEFT)
- entry = tk.Entry(frame, width=50)
- entry.pack(side=tk.LEFT)
- entry_fields.append(entry)
- browse_button = tk.Button(frame, text="Browse", command=lambda: open_file_explorer(entry))
- browse_button.pack(side=tk.LEFT)
- # Create file input fields with respective names
- for name in field_names:
- create_file_input(name)
- # Entry field for the output file name
- output_filename_label = tk.Label(root, text="Output File Name:")
- output_filename_label.pack()
- output_filename_entry = tk.Entry(root, width=50)
- output_filename_entry.pack()
- # Button to trigger the conversion process
- convert_button = tk.Button(root, text="Convert Files", command=convert_files)
- convert_button.pack()
- # Run the application
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement