-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_wave_file.py
75 lines (59 loc) · 2.62 KB
/
generate_wave_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import numpy as np
import os
from scipy.signal import resample
from datetime import datetime
import soundfile as sf
def scale_samples(samples, bit_rate):
if bit_rate == 16:
return np.int16(samples * (2**15 - 1))
elif bit_rate == 24:
return (samples * (2**23 - 1)).astype(np.int32) << 8
else:
raise ValueError(f"Unsupported bit rate: {bit_rate}")
def generate_wave_file(y_combined, fs_initial, fs_target_name='44.1kHz', bit_rate=16, custom_filename=None, save_to_file=True):
# dictionary of common sampling rates
fs_target_dict = {
'44.1kHz': 44100,
'48kHz': 48000,
'88.2kHz': 88200,
'96kHz': 96000,
'192kHz': 192000
}
# get the target sampling rate from the dictionary
if fs_target_name in fs_target_dict:
fs_target = fs_target_dict[fs_target_name]
else:
raise ValueError(f"Unsupported sampling rate: {fs_target_name}")
# calculate the number of samples in the resampled signal
num_samples_resampled = int(len(y_combined) * fs_target / fs_initial)
# resample the signal to the target sample rate
y_resampled = resample(y_combined, num_samples_resampled)
# normalize the resampled signal to the range of -1 to 1
y_normalized = y_resampled / np.max(np.abs(y_resampled))
# scale the resampled signal to the desired bit rate range
y_scaled = scale_samples(y_normalized, bit_rate)
# get the current timestamp
timestamp = datetime.now().strftime("%H_%M_%S")
# set the output file name
if custom_filename is None:
output_filename = f"generated_wave_file_{fs_target_name}_{bit_rate}bit_{timestamp}.wav"
else:
output_filename = custom_filename
# get the current working directory
current_dir = os.getcwd()
# create 'rendered_audio' directory if it doesn't exist
output_directory = os.path.join(current_dir, "rendered_audio")
if not os.path.exists(output_directory):
os.makedirs(output_directory)
# construct the full path to save the audio
file_path = os.path.join(output_directory, output_filename)
if save_to_file:
# write the resampled waveform to a wave audio file
sf.write(file_path, y_scaled, fs_target, subtype=f'PCM_{bit_rate}')
# get the current timestamp for log
timestamp_log = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# print a message about the wave file being saved successfully with a timestamp
print(f"[{timestamp_log}] {bit_rate}-bit wave file with {fs_target_name} sampling rate saved successfully to: {file_path}")
return file_path
else:
return y_scaled, fs_target