Skip to content

Commit

Permalink
add support for 24bit wav
Browse files Browse the repository at this point in the history
  • Loading branch information
jopohl committed May 6, 2018
1 parent 5b4885c commit defefb4
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/urh/signalprocessing/Signal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import csv
import math
import os
import struct
import tarfile
import wave

Expand All @@ -13,7 +12,7 @@
from urh.signalprocessing.Filter import Filter
from urh.util import FileOperator
from urh.util.Logger import logger
import math


class Signal(QObject):
"""
Expand Down Expand Up @@ -95,14 +94,22 @@ def __load_wav_file(self, filename: str):
params = {"min": 0, "max": 255, "fmt": np.uint8} # Unsigned Byte
elif sample_width == 2:
params = {"min": -32768, "max": 32767, "fmt": np.int16}
elif sample_width == 3:
params = {"min": -2147483648, "max": 2147483647, "fmt": np.int32}
elif sample_width == 4:
params = {"min": -2147483648, "max": 2147483647, "fmt": np.int32}
else:
raise ValueError("Can't handle sample width {0}".format(sample_width))

params["center"] = (params["min"] + params["max"]) / 2

data = np.fromstring(wav.readframes(num_frames * num_channels), dtype=params["fmt"])
if sample_width == 3:
raw_bytes = wav.readframes(num_frames * num_channels)
data = np.empty(len(raw_bytes) // 3, dtype=np.int32)
for i in range(0, len(raw_bytes), 3):
data[i // 3] = (raw_bytes[i] << 8 | raw_bytes[i + 1] << 16 | raw_bytes[i + 2] << 24)
else:
data = np.fromstring(wav.readframes(num_frames * num_channels), dtype=params["fmt"])
if num_channels == 1:
self._fulldata = np.zeros(num_frames, dtype=np.complex64, order="C")
self._fulldata.real = np.multiply(1 / params["max"], np.subtract(data, params["center"]))
Expand Down Expand Up @@ -405,8 +412,8 @@ def estimate_frequency(self, start: int, end: int, sample_rate: float):
:return:
"""
# ensure power of 2 for faster fft
length = 2 ** int(math.log2(end-start))
data = self.data[start:start+length]
length = 2 ** int(math.log2(end - start))
data = self.data[start:start + length]

try:
w = np.fft.fft(data)
Expand Down

0 comments on commit defefb4

Please sign in to comment.