L
L
LakeForest2022-01-30 14:28:50
Python
LakeForest, 2022-01-30 14:28:50

How to get digital sound from bytes received by open(file, 'rb'), same as if read by scipy.io.wavfile.read() method?

filename = args.filename
    target_sr = 16000
    sample_rate_hertz, samples = wavfile.read(filename)
    number_of_samples = round(
        len(samples) * float(target_sr) / sample_rate_hertz)
    samples = scipy.signal.resample(samples, number_of_samples)
    print(samples[:10])
# [ 8.         -6.15779362  8.         17.90166576  8.          0.39760882
#  8.         14.16452768  8.          2.81953318]
   with open(filename, "rb") as fileb:
        abon = base64.b64encode(fileb.read())
        audio_bytes = io.BytesIO(base64.b64decode(abon))
        audio = (
            AudioSegment.from_raw(
                audio_bytes,
                sample_width=2,
                frame_rate=target_sr,
                channels=1,
            ))#.set_frame_rate(16000)
    np_chunk = np.frombuffer(audio.get_array_of_samples(), dtype=np.int16)
    print(np_chunk[:10])
#  [ 18770  17990 -19036      1  16727  17750  28006   8308     16      0]
    print(len(np_chunk)) # 56022
    print(len(samples)) #112000

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question