-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile_helpers.py
More file actions
50 lines (41 loc) · 1.5 KB
/
file_helpers.py
File metadata and controls
50 lines (41 loc) · 1.5 KB
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
from typing import Dict, List
from pathlib import Path
# For segmenting processed wavs
from pydub import AudioSegment
from pydub.utils import make_chunks
def split_audio(wav_file, clip_len, output_dir, idx_offset=0, verbose: bool=False) -> None:
"""
Splits the rendered audio .wav file into many, one for each setting
Args:
wav_file (Path): The output .wav file generated by REAPER
clip_len (float): The length in seconds of each clip
output_dir (Path): The output directory to write split files
Returns:
None
"""
# Split into chunks
if verbose:
print(f"Splitting {wav_file}...")
# msg(f"Splitting {wav_file}...")
audio = AudioSegment.from_file(wav_file , "wav")
chunk_length_ms = clip_len * 1000 # pydub calculates in millisec
chunks = make_chunks(audio, chunk_length_ms)
# If the output dir does not exist, make it
output_dir.mkdir(parents=True, exist_ok=True)
# Write to file
for i, chunk in enumerate(chunks):
chunk.export(output_dir / f"{i+idx_offset:08d}.wav", format="wav")
def delete_tmp_files(files: List[Path], verbose: bool=False) -> None:
"""
Deletes the given files
Args:
files (List[Path]): A list of files to delete
Returns:
None
"""
for file in files:
if file.is_file():
if args.verbose:
print(f"Deleting rendered file: {file}")
# msg(f"Deleting rendered file: {file}")
file.unlink()