Skip to content

Commit 3f549c1

Browse files
committed
correctly resampling .wav export
1 parent 1bfadfb commit 3f549c1

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

Exporter.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pandas as pd
88
import numpy as np
99
from scipy.io import wavfile
10-
from scipy.signal import resample
10+
from scipy.interpolate import interp1d
1111
import wave
1212
import Config
1313

@@ -26,6 +26,7 @@ def __saveDialog(filetype):
2626
# function based on
2727
# https://stackoverflow.com/a/41586167/12231900
2828
def __exportWave(data, filename):
29+
SMPLS = 44100
2930
# normalize values to -1.0 to 1.0
3031
arr = np.array(data.interpData["y"])
3132
arr = np.divide(arr, np.max(np.abs(data.interpData["y"])))
@@ -36,12 +37,22 @@ def __exportWave(data, filename):
3637
# samples only go from -(2**15) to (2**15)
3738
# => missing one possible value at (2**15)-1
3839

39-
# resample values, otherwise length wont match (?)
40-
data_resampled = resample( arr, len(data.interpData["y"]) )
40+
# resample values with same algorithms to match sampling rate
41+
xnew = np.linspace(
42+
data.interpData["x"].min(), # from
43+
data.interpData["x"].max(), # to
44+
int(
45+
np.ceil(((data.interpData["x"].max() - data.interpData["x"].min()) / Config.DIVISION) * SMPLS)
46+
)
47+
)
48+
spl = interp1d(data.interpData["x"], data.interpData["y"], kind=data.config["interpolation"], copy=True,
49+
assume_sorted=True, bounds_error=False, fill_value=0)
50+
data_resampled = spl(xnew)
51+
4152
# convert for 16 bit PCM
4253
data_resampled = data_resampled.astype(np.int16)
4354

44-
wavfile.write(filename, 44100, data_resampled)
55+
wavfile.write(filename, SMPLS, data_resampled)
4556

4657

4758
def export(data, export):

0 commit comments

Comments
 (0)