def merge_bgm(bgm_info, input_foldername, output_filename=None): longest_duration = bgm_info['end'] # Find maximum duration of BGM channels = 1 for bgm in bgm_info['data']: filename = helper.getCaseInsensitivePath( os.path.join(input_foldername, bgm['filename'])) print(filename) bgm['file'] = pydub.AudioSegment.from_file(filename) duration = bgm['timestamp'] + len(bgm['file']) / 1000 if bgm['file'].channels > channels: channels = bgm['file'].channels if duration > longest_duration: longest_duration = duration output = pydub.AudioSegment.silent(duration=longest_duration * 1000, frame_rate=48000) output.set_channels(channels) for bgm in bgm_info['data']: output = output.overlay(bgm['file'], position=bgm['timestamp'] * 1000) if output_filename: temp_filename = output_filename else: temp_filename = tmpfile.mkstemp(suffix=".wav") output.export(temp_filename, format="wav") return temp_filename
def get_processed_wav(input_filename, output_filename=None, channels=1, bits=16, rate=48000): input_filename = helper.getCaseInsensitivePath(input_filename) output = get_audio_file(input_filename) if not output: return None made_change = False if output.sample_width != bits // 8: made_change = True output = output.set_sample_width(bits // 8) if output.channels != channels: made_change = True output = output.set_channels(channels) if output.frame_rate != rate: made_change = True output = output.set_frame_rate(rate) if not made_change and input_filename.lower().endswith('.wav'): # This file is already the exact requirements, just return the original return input_filename if output_filename == None: output_filename = tmpfile.mkstemp() #print("Converted {} to {}".format(input_filename, output_filename)) output.export(output_filename, format="wav") return output_filename
def create_bgm_render(json_sq2, params, target_parts, output_bgm_filename): def _create_bgm_render(params_bgm, target_parts, output_bgm_filename): wav.generate_wav_from_json(params_bgm, generate_output_filename=False) wavbintool.parse_wav(params_bgm['output'], output_bgm_filename) print("Creating BGM render", target_parts) params_bgm = copy.deepcopy(params) params_bgm['render_ext'] = "wav" params_bgm['output'] = tmpfile.mkstemp(suffix="." + params_bgm.get('render_ext', 'wav')) if os.path.exists(params_bgm['output']): os.unlink(params_bgm['output']) if 'guitar' in target_parts or 'bass' in target_parts or 'open' in target_parts: params_bgm['render_ignore_auto'] = True params_bgm['parts'] = target_parts params_bgm['difficulty'] = ['max'] running_threads = [] if USE_THREADS: bgm_thread = threading.Thread(target=_create_bgm_render, args=(params_bgm, target_parts, output_bgm_filename)) bgm_thread.start() running_threads.append(bgm_thread) else: _create_bgm_render(params_bgm, target_parts, output_bgm_filename) return running_threads
def decode_data(data, rate, channels, bits): input_filename = tmpfile.mkstemp() output_filename = tmpfile.mkstemp() with open(input_filename, "wb") as f: f.write(data) prefix = "" if os.name != "nt": prefix = "wine" cmd = "{} adpcmwavetool.exe d \"{}\" \"{}\" {}".format(prefix, input_filename, output_filename, channels) subprocess.call(cmd, shell=True) with open(output_filename, "rb") as f: data = bytearray(f.read()) return data
def encode_data(data, channels): input_filename = tmpfile.mkstemp() output_filename = tmpfile.mkstemp() with open(input_filename, "wb") as f: f.write(data) prefix = "" if helper.is_wsl(): prefix = "./" elif os.name != "nt": prefix = "wine " cmd = "{}adpcmwavetool.exe e \"{}\" \"{}\" {}".format(prefix, helper.get_windows_path(input_filename), helper.get_windows_path(output_filename), channels) subprocess.call(cmd, shell=True) with open(output_filename, "rb") as f: data = bytearray(f.read()) return data
def decode_at3(input, sample_rate, channels): data = bytearray(open(input, "rb").read()) wav_header = bytearray() wav_header += bytearray("RIFF", "ascii") wav_header += int.to_bytes(len(data) + 0x3c, length=4, byteorder="little") wav_header += bytearray("WAVE", "ascii") wav_header += bytearray("fmt ", "ascii") wav_header += int.to_bytes(0x20, length=4, byteorder="little") format_id = 0x270 byte_rate = 0x4099 block_align = 0x180 bits_per_sample = 0 wav_header += int.to_bytes(format_id, length=2, byteorder="little") wav_header += int.to_bytes(channels, length=2, byteorder="little") wav_header += int.to_bytes(sample_rate, length=4, byteorder="little") wav_header += int.to_bytes(byte_rate, length=4, byteorder="little") wav_header += int.to_bytes(block_align, length=2, byteorder="little") wav_header += int.to_bytes(bits_per_sample, length=2, byteorder="little") wav_header += int.to_bytes(0x0e, length=2, byteorder="little") wav_header += int.to_bytes(0x01, length=2, byteorder="little") wav_header += int.to_bytes(0x1000, length=2, byteorder="little") wav_header += int.to_bytes(0x00, length=2, byteorder="little") wav_header += int.to_bytes(0x00, length=4, byteorder="little") wav_header += int.to_bytes(0x01, length=4, byteorder="little") wav_header += bytearray("data", "ascii") wav_header += int.to_bytes(len(data), length=4, byteorder="little") wav_header += data tmp_filename = tmpfile.mkstemp(suffix=".at3") with open(tmp_filename, "wb") as outfile: outfile.write(wav_header) sound = audio.get_audio_file(tmp_filename) tmpfile.tmpcleanup() return sound