Datasets:
Download code/exhibition_v3/normalize.py from Heiheihaha17/MESH-Benchmark: direct link, hf CLI and curl.
- Browser
- Download file 2.76 kB
-
https://huggingface.co/datasets/Heiheihaha17/MESH-Benchmark/resolve/main/code/exhibition_v3/normalize.py
- Command line
-
hf download hf://datasets/Heiheihaha17/MESH-Benchmark/code/exhibition_v3/normalize.py
-
curl -L -o normalize.py https://huggingface.co/datasets/Heiheihaha17/MESH-Benchmark/resolve/main/code/exhibition_v3/normalize.py
2.76 kB
| """Shared base peak scaling, with restart-safe journals and power provenance.""" | |
| from __future__ import annotations | |
| import copy | |
| from pathlib import Path | |
| import numpy as np | |
| import soundfile as sf | |
| from .common import * | |
| def normalize_base(root,index): | |
| ensure_compute() | |
| report_path=root/'reports/bases'/f'{index:04d}.json';report=read(report_path) | |
| desired=.9/max(r['raw_peak'] for r in report['recordings']) | |
| journal_path=root/'private/normalization'/f'{index:04d}.json' | |
| journal=read(journal_path) if journal_path.exists() else dict(gain=desired,files={}) | |
| if journal['gain']!=desired:raise ValueError('Normalization gain changed') | |
| if journal.get('complete'): | |
| for record in report['recordings']: | |
| if sha(root/record['path'])!=record['sha256']:raise ValueError('Normalized audio changed') | |
| return report | |
| previous=report['common_final_gain'];ratio=desired/previous | |
| for record in report['recordings']: | |
| path=root/record['path'];key=record['recording_id'];current=sha(path) | |
| entry=journal['files'].get(key) | |
| if entry and current==entry['after_sha256']: | |
| record.update(sha256=current,common_final_gain=desired,final_peak=entry['peak']) | |
| continue | |
| if current!=(entry['before_sha256'] if entry else record['sha256']):raise ValueError('Unexpected pre-normalization audio') | |
| wave,rate=sf.read(path,dtype='float32');wave=(wave*ratio).astype(np.float32) | |
| peak=float(np.max(np.abs(wave))) | |
| if rate!=SR or not np.isfinite(wave).all() or peak>.900001:raise ValueError('Normalization overflow') | |
| temporary=path.with_suffix('.normalized.tmp.wav');write_float_wav(temporary,wave) | |
| entry=dict(before_sha256=current,after_sha256=sha(temporary),peak=peak) | |
| journal['files'][key]=entry;write(journal_path,journal) | |
| temporary.replace(path);record.update(sha256=entry['after_sha256'],common_final_gain=desired,final_peak=peak) | |
| for measurement in report['acoustic_measurements']: | |
| for key in ['target_power','competing_speech_power','environmental_noise_power']: | |
| raw=measurement.get('raw_'+key,measurement[key]) | |
| measurement['raw_'+key]=raw;measurement[key]=raw*desired**2 | |
| measurement['power_stage']='published FOA after common base peak normalization' | |
| report.update(common_final_gain=desired,final_normalization_applied=True, | |
| peak_normalization=dict(target=.9,scope='same gain across all robots and all three base derivatives',code_sha256=sha(Path(__file__)))) | |
| write(report_path,report);journal['complete']=True;write(journal_path,journal) | |
| return report | |
| def normalize(root): | |
| for p in sorted((root/'reports/bases').glob('*.json')):normalize_base(root,int(p.stem)) | |