Skip to content

Commit 7f9cd47

Browse files
author
Benjamin Moody
committed
wr_dat_file: support FLAC output formats.
Writing FLAC formats, like reading FLAC formats, is done using the soundfile package, which is currently treated as an optional dependency (since on some systems it requires an external copy of libsndfile.) This code performs, in effect, the inverse of _rd_compressed_file. As with _rd_compressed_file, this has room to be optimized somewhat.
1 parent 211329c commit 7f9cd47

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

wfdb/io/_signal.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,8 @@ def wr_dat_file(
22852285
N/A
22862286
22872287
"""
2288+
file_path = os.path.join(write_dir, file_name)
2289+
22882290
# Combine list of arrays into single array
22892291
if expanded:
22902292
n_sig = len(e_d_signal)
@@ -2416,9 +2418,54 @@ def wr_dat_file(
24162418
b_write = b_write.reshape((1, -1))[0]
24172419
# Convert to un_signed 8 bit dtype to write
24182420
b_write = b_write.astype("uint8")
2421+
2422+
elif fmt in ("508", "516", "524"):
2423+
import soundfile
2424+
2425+
if any(spf != samps_per_frame[0] for spf in samps_per_frame):
2426+
raise ValueError(
2427+
"All channels in a FLAC signal file must have the same "
2428+
"sampling rate and samples per frame"
2429+
)
2430+
if n_sig > 8:
2431+
raise ValueError(
2432+
"A single FLAC signal file cannot contain more than 8 channels"
2433+
)
2434+
2435+
d_signal = d_signal.reshape(-1, n_sig, samps_per_frame[0])
2436+
d_signal = d_signal.transpose(0, 2, 1)
2437+
d_signal = d_signal.reshape(-1, n_sig)
2438+
2439+
if fmt == "508":
2440+
d_signal = d_signal.astype("int16")
2441+
np.left_shift(d_signal, 8, out=d_signal)
2442+
subtype = "PCM_S8"
2443+
elif fmt == "516":
2444+
d_signal = d_signal.astype("int16")
2445+
subtype = "PCM_16"
2446+
elif fmt == "524":
2447+
d_signal = d_signal.astype("int32")
2448+
np.left_shift(d_signal, 8, out=d_signal)
2449+
subtype = "PCM_24"
2450+
else:
2451+
raise ValueError(f"unknown format ({fmt})")
2452+
2453+
sf = soundfile.SoundFile(
2454+
file_path,
2455+
mode="w",
2456+
samplerate=96000,
2457+
channels=n_sig,
2458+
subtype=subtype,
2459+
format="FLAC",
2460+
)
2461+
with sf:
2462+
sf.write(d_signal)
2463+
return
2464+
24192465
else:
24202466
raise ValueError(
2421-
"This library currently only supports writing the following formats: 80, 16, 24, 32"
2467+
"This library currently only supports writing the "
2468+
"following formats: 80, 16, 24, 32, 508, 516, 524"
24222469
)
24232470

24242471
# Byte offset in the file
@@ -2433,7 +2480,7 @@ def wr_dat_file(
24332480
b_write = np.append(np.zeros(byte_offset, dtype="uint8"), b_write)
24342481

24352482
# Write the bytes to the file
2436-
with open(os.path.join(write_dir, file_name), "wb") as f:
2483+
with open(file_path, "wb") as f:
24372484
b_write.tofile(f)
24382485

24392486

0 commit comments

Comments
 (0)