summaryrefslogtreecommitdiff
path: root/pokemontools/pcm.py
diff options
context:
space:
mode:
authoryenatch <yenatch@gmail.com>2017-02-13 18:09:31 -0500
committerGitHub <noreply@github.com>2017-02-13 18:09:31 -0500
commit979c98a7c0f67ad6b9685b2d532c66a1f76ffb22 (patch)
treec67cc7b8500aac4e400d4e8bfdbef57a57b63eb1 /pokemontools/pcm.py
parent74c620d01ad59bfb09cf4111ace549b925fcb9ab (diff)
parent766dea11bd63dee939db2b47198410e6c6e0fc7e (diff)
Merge pull request #103 from eevee/py3
Python 3 compatibility, sort of, maybe
Diffstat (limited to 'pokemontools/pcm.py')
-rwxr-xr-xpokemontools/pcm.py15
1 files changed, 9 insertions, 6 deletions
diff --git a/pokemontools/pcm.py b/pokemontools/pcm.py
index 2e936d9..3a2920b 100755
--- a/pokemontools/pcm.py
+++ b/pokemontools/pcm.py
@@ -1,5 +1,8 @@
# pcm.py
# Converts between .wav files and 1-bit pcm data. (pcm = pulse-code modulation)
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
import argparse
import os
@@ -72,7 +75,7 @@ def convert_to_pcm(filenames=[]):
# Pack the 1-bit samples together.
packed_samples = bytearray()
- for i in xrange(0, len(clamped_samples), 8):
+ for i in range(0, len(clamped_samples), 8):
# Read 8 pcm values to pack one byte.
packed_value = 0
for j in range(8):
@@ -103,14 +106,14 @@ def get_wav_samples(filename):
# Unpack the values based on the sample byte width.
unpacked_samples = []
- for i in xrange(0, len(samples), sample_width):
+ for i in range(0, len(samples), sample_width):
if sample_width == 1:
fmt = 'B'
elif sample_width == 2:
fmt = 'h'
else:
# todo: support 3-byte sample width
- raise Exception, "Unsupported sample width: " + str(sample_width)
+ raise Exception("Unsupported sample width: " + str(sample_width))
value = struct.unpack(fmt, samples[i:i + sample_width])[0]
unpacked_samples.append(value)
@@ -122,7 +125,7 @@ def get_wav_samples(filename):
# Also find the average amplitude of the samples.
resampled_samples = []
total_value = 0
- interval = float(sample_rate) / BASE_SAMPLE_RATE
+ interval = sample_rate / BASE_SAMPLE_RATE
index = 0
while index < sample_count:
sample = unpacked_samples[int(index)]
@@ -131,7 +134,7 @@ def get_wav_samples(filename):
resampled_samples.append(sample)
index += interval
- average_sample = float(total_value) / len(resampled_samples)
+ average_sample = total_value / len(resampled_samples)
return resampled_samples, average_sample
@@ -148,7 +151,7 @@ def main():
}.get(args.mode, None)
if method == None:
- raise Exception, "Unknown conversion method!"
+ raise Exception("Unknown conversion method!")
method(args.filenames)