| import os, json, wave |
|
|
| def load_jsonl(file_path): |
| with open(file_path, 'r') as file: |
| d = json.load(file) |
| return d |
|
|
| def write_jsonl(data, output_file): |
| with open(output_file, 'w') as file: |
| for item in data: |
| json.dump(item, file) |
| file.write('\n') |
|
|
| def get_audio_info(audio_path): |
| """ |
| Extract duration (seconds), sample_rate (Hz), num_samples (int), bit_depth (bits), and channels (int) from a WAV file. |
| """ |
| with wave.open(audio_path, 'rb') as wf: |
| sample_rate = wf.getframerate() |
| num_samples = wf.getnframes() |
| duration = num_samples / float(sample_rate) |
| sample_width = wf.getsampwidth() |
| bit_depth = sample_width * 8 |
| channels = wf.getnchannels() |
| return duration, sample_rate, num_samples, bit_depth, channels |
|
|
| def convert_gs_to_jsonl(output_dir="data/GS"): |
| metadata = load_jsonl(os.path.join(output_dir, "giantsteps_clips/meta.json")) |
| train, val, test = [], [], [] |
| for uid, info in metadata.items(): |
| audio_path = os.path.join(output_dir, "giantsteps_clips", "wav", f"{uid}.wav") |
| ori_uid = info['clip']['audio_uid'] |
| split = info['split'] |
| label = info['y'] |
| try: |
| duration, sr, num_samples, bit_depth, channels = get_audio_info(audio_path) |
| except Exception as e: |
| print(f"Error reading {audio_path}: {e}") |
| continue |
| o = { |
| "audio_path": audio_path, |
| "ori_uid": ori_uid, |
| "label": label, |
| "duration": duration, |
| "sample_rate": sr, |
| "num_samples": num_samples, |
| "bit_depth": bit_depth, |
| "channels": channels |
| } |
| if split == "train": |
| train.append(o) |
| elif split == "valid": |
| val.append(o) |
| elif split == "test": |
| test.append(o) |
| else: |
| print(f"Unknown split {split} for {uid}") |
| os.makedirs(output_dir, exist_ok=True) |
| write_jsonl(train, os.path.join(output_dir, "GS.train.jsonl")) |
| write_jsonl(val, os.path.join(output_dir, "GS.val.jsonl")) |
| write_jsonl(test, os.path.join(output_dir, "GS.test.jsonl")) |
| print(f"train: {len(train)}, val: {len(val)}, test: {len(test)}") |
| print("Conversion completed.") |
|
|
| if __name__ == "__main__": |
| convert_gs_to_jsonl("data/GS") |
| print("GS dataset conversion to JSONL completed.") |
| |
| |