diff options
author | Marcus Huderle <huderlem@gmail.com> | 2015-11-07 10:39:57 -0800 |
---|---|---|
committer | Marcus Huderle <huderlem@gmail.com> | 2015-11-07 10:39:57 -0800 |
commit | fd08d3926b4e2dd0b835afe53ee1c2476c3965f0 (patch) | |
tree | 3a37495b160e3b94cbc13135b3b332bf4d212ad0 | |
parent | 965dd32443568d4dbafbcd11fcc5256f69dcc8a3 (diff) |
Only read samples from first .wav channel when converting to pcm.
-rwxr-xr-x | pokemontools/pcm.py | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/pokemontools/pcm.py b/pokemontools/pcm.py index f9f6064..2e936d9 100755 --- a/pokemontools/pcm.py +++ b/pokemontools/pcm.py @@ -49,10 +49,10 @@ def convert_to_pcm(filenames=[]): Converts a .wav file into 1-bit pcm data. Samples in the .wav file are simply clamped to on/off. - TODO: This currently only works correctly on .wav files with the following attributes: + This currently works correctly on .wav files with the following attributes: 1. Sample Width = 1 or 2 bytes (Some wave files use 3 bytes per sample...) - 2. 1 Channel - It can be improved to account for these factors. + 2. Arbitrary sample sample_rate + 3. Mono or Stereo (1 or 2 channels) """ for filename in filenames: samples, average_sample = get_wav_samples(filename) @@ -97,6 +97,7 @@ def get_wav_samples(filename): sample_width = wav_file.getsampwidth() sample_count = wav_file.getnframes() sample_rate = wav_file.getframerate() + num_channels = wav_file.getnchannels() samples = bytearray(wav_file.readframes(sample_count)) @@ -114,6 +115,9 @@ def get_wav_samples(filename): value = struct.unpack(fmt, samples[i:i + sample_width])[0] unpacked_samples.append(value) + # Only keep the samples from the first audio channel. + unpacked_samples = unpacked_samples[::num_channels] + # Approximate the BASE_SAMPLE_RATE. # Also find the average amplitude of the samples. resampled_samples = [] |