From fd08d3926b4e2dd0b835afe53ee1c2476c3965f0 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sat, 7 Nov 2015 10:39:57 -0800 Subject: Only read samples from first .wav channel when converting to pcm. --- pokemontools/pcm.py | 10 +++++++--- 1 file 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 = [] -- cgit v1.2.3