From 21204192a912c76cb3bd099084a1c6d4de2fd03a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Tue, 6 Oct 2020 09:17:55 +0200 Subject: [PATCH] Audio --- README.md | 10 +++++----- index.html | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 849d8b4..54048a0 100644 --- a/README.md +++ b/README.md @@ -2828,13 +2828,13 @@ nframes = .getnframes() # Number of frames. ```python def read_wav_file(filename): def get_int(bytes_obj): - an_int = int.from_bytes(bytes_obj, 'little', signed=width!=1) - return an_int - 128 * (width == 1) + an_int = int.from_bytes(bytes_obj, 'little', signed=sampwidth!=1) + return an_int - 128 * (sampwidth == 1) with wave.open(filename, 'rb') as file: - width = file.getsampwidth() + sampwidth = file.getsampwidth() frames = file.readframes(-1) - bytes_samples = (frames[i: i + width] for i in range(0, len(frames), width)) - return [get_int(b) / pow(2, width * 8 - 1) for b in bytes_samples] + bytes_samples = (frames[i: i + sampwidth] for i in range(0, len(frames), sampwidth)) + return [get_int(b) / pow(2, sampwidth * 8 - 1) for b in bytes_samples] ``` ### Write Float Samples to WAV File diff --git a/index.html b/index.html index 453e553..3efa699 100644 --- a/index.html +++ b/index.html @@ -2423,13 +2423,13 @@ nframes = <Wave_read>.getnframes()

Read Float Samples from WAV File

def read_wav_file(filename):
     def get_int(bytes_obj):
-        an_int = int.from_bytes(bytes_obj, 'little', signed=width!=1)
-        return an_int - 128 * (width == 1)
+        an_int = int.from_bytes(bytes_obj, 'little', signed=sampwidth!=1)
+        return an_int - 128 * (sampwidth == 1)
     with wave.open(filename, 'rb') as file:
-        width  = file.getsampwidth()
+        sampwidth = file.getsampwidth()
         frames = file.readframes(-1)
-    bytes_samples = (frames[i: i + width] for i in range(0, len(frames), width))
-    return [get_int(b) / pow(2, width * 8 - 1) for b in bytes_samples]
+    bytes_samples = (frames[i: i + sampwidth] for i in range(0, len(frames), sampwidth))
+    return [get_int(b) / pow(2, sampwidth * 8 - 1) for b in bytes_samples]
 

Write Float Samples to WAV File

def write_to_wav_file(filename, float_samples, nchannels=1, sampwidth=2, framerate=44100):