Skip to content
Snippets Groups Projects
Commit 7267e730 authored by David Huss's avatar David Huss :speech_balloon:
Browse files

Fix wavetable script, include from header

- Fix an error in the wav_to_wavetable script where it accidentally
  acted as a wavefolder (use `signed=False` and subtract 511 from the
  resampled result to get that behavior back – the sounds were nice)
- Include the resulting wavetable from the header file, to avoid copy
  paste mistakes
parent fa981f98
No related branches found
No related tags found
No related merge requests found
Source diff could not be displayed: it is too large. Options to address this: view the blob.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
...@@ -59,13 +59,17 @@ path = args.input ...@@ -59,13 +59,17 @@ path = args.input
w = wave.open(path, "rb") w = wave.open(path, "rb")
channels = w.getnchannels() channels = w.getnchannels()
samplewidth = w.getsampwidth() samplewidth = w.getsampwidth()
print(f"Audio has {channels} channels and each sample is {samplewidth} bytes wide") print(
f"Audio has {channels} channels and each sample is {samplewidth} bytes wide ({samplewidth * 8} bit audio)"
)
resampled = [] resampled = []
def resample(sample: int) -> int: def resample(sample: int, bits=24) -> int:
return min(1023, int(sample / 2**14)) - 511 limit = 2**10
steps = 2 ** (bits - 10)
return max(-limit, min(limit, int(sample / steps)))
for n in range(w.getnframes()): for n in range(w.getnframes()):
...@@ -73,7 +77,9 @@ for n in range(w.getnframes()): ...@@ -73,7 +77,9 @@ for n in range(w.getnframes()):
if frame != b"": if frame != b"":
# Convert the frame into a list of integers, assuming little endian encoding # Convert the frame into a list of integers, assuming little endian encoding
frame_data = [ frame_data = [
int.from_bytes(frame[i : i + samplewidth], "little") int.from_bytes(
frame[i : i + samplewidth], byteorder=sys.byteorder, signed=True
)
for i in range(0, len(frame), samplewidth) for i in range(0, len(frame), samplewidth)
] ]
# If we have more than one channel the samples of each channel # If we have more than one channel the samples of each channel
...@@ -83,7 +89,7 @@ for n in range(w.getnframes()): ...@@ -83,7 +89,7 @@ for n in range(w.getnframes()):
for sample in frame_data: for sample in frame_data:
# print(sample) # print(sample)
# 24 bit - 14 bit = 10 bit # 24 bit - 14 bit = 10 bit
resampled.append(resample(sample)) resampled.append(resample(sample, samplewidth * 8))
elif channels == 2: elif channels == 2:
# Iterate in steps of 2 over the frames and deinterleave # Iterate in steps of 2 over the frames and deinterleave
# them into the samples for left and right # them into the samples for left and right
...@@ -96,7 +102,7 @@ for n in range(w.getnframes()): ...@@ -96,7 +102,7 @@ for n in range(w.getnframes()):
f"{bcolors.FAIL}Error:{bcolors.ENDC} Sorry, we do not support wave files with {channels} channels.. yet", f"{bcolors.FAIL}Error:{bcolors.ENDC} Sorry, we do not support wave files with {channels} channels.. yet",
file=sys.stderr, file=sys.stderr,
) )
exit() exit(1)
print(f"Resampled {len(resampled)} samples") print(f"Resampled {len(resampled)} samples")
# shrink_to = 48000*2 # shrink_to = 48000*2
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment