File: resample_stream.py

package info (click to toggle)
python-soxr 0.5.0.post1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 256 kB
  • sloc: python: 378; cpp: 318; makefile: 15
file content (31 lines) | stat: -rw-r--r-- 768 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import soundfile as sf
import soxr

TARGET_RATE = 16000
CHUNK_SIZE = 96000

# Open input audio file
in_file = sf.SoundFile('very_long.flac', 'r')
source_rate = in_file.samplerate
channels = in_file.channels

# Config ResampleStream
resampler = soxr.ResampleStream(source_rate, TARGET_RATE, channels, dtype='float32')

# Open output audio file
with sf.SoundFile('output.flac', 'w', TARGET_RATE, channels) as out_file:
    while True:
        # Read chunk of audio
        x = in_file.read(CHUNK_SIZE, dtype='float32')
        is_last = (in_file.tell() == in_file.frames)

        # Resample the chunk
        y = resampler.resample_chunk(x, last=is_last)

        # Write to output file
        out_file.write(y)

        if is_last:
            break

in_file.close()