File size: 3,068 Bytes
890de26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding:utf-8 -*-
# @FileName  :audioHelper.py
# @Time      :2023/8/8 20:27
# @Author    :lovemefan
# @Email     :[email protected]

import array
import struct
import time

import numpy as np


class AudioReader:
    """

    read audio from sanic request
    """

    def __init__(self):
        pass

    @staticmethod
    def get_info(self, path: str):
        with open(path, "rb") as f:
            (
                name,
                data_lengths,
                _,
                _,
                _,
                _,
                channels,
                sample_rate,
                bit_rate,
                block_length,
                sample_bit,
                _,
                pcm_length,
            ) = struct.unpack_from("<4sL4s4sLHHLLHH4sL", f.read(44))
            assert sample_rate == 16000, "sample rate must be 16000"
            nframes = pcm_length // (channels * 2)
        return nframes

    @staticmethod
    def read_wav_bytes(data: bytes):
        """
        convert bytes into array of pcm_s16le data
        :param data: PCM format bytes
        :return:
        """

        # header of wav file
        info = data[:44]
        frames = data[44:]
        (
            name,
            data_lengths,
            _,
            _,
            _,
            _,
            channels,
            sample_rate,
            bit_rate,
            block_length,
            sample_bit,
            _,
            pcm_length,
        ) = struct.unpack_from("<4sL4s4sLHHLLHH4sL", info)
        # shortArray each element is 16bit
        data = AudioReader.read_pcm_byte(frames)
        return data, sample_rate

    @staticmethod
    def read_wav_file(audio_path: str):
        with open(audio_path, "rb") as f:
            data = f.read()
        return AudioReader.read_wav_bytes(data)

    @staticmethod
    def read_pcm_byte(data: bytes):
        short_array = array.array("h")
        short_array.frombytes(data)
        data = np.array(short_array, dtype="float16") / (1 << 15)
        return data

    @staticmethod
    def export_pcm_to_wav(
        data: bytes, sample_rate=44100, num_channels=1, sample_width=2
    ):
        pcm_length = len(data)
        block_length = int(num_channels * sample_width)
        bits_rate = num_channels * sample_rate * sample_width * 8
        bits_per_sample = sample_width * 8
        header = struct.pack(
            "<4sL4s4sLHHLLHH4sL",
            b"RIFF",
            36 + pcm_length,
            b"WAVE",
            b"fmt ",
            16,
            1,
            1,
            sample_rate,
            bits_rate,
            block_length,
            bits_per_sample,
            b"data",
            pcm_length,
        )
        return header + data

    @staticmethod
    def export_pcm_to_wav_file(data, file_path=None):
        head = AudioReader.export_pcm_to_wav(data)
        file_path = file_path or f"/tmp/{time.time()}.wav"
        with open(file_path, "wb") as f:
            f.write(head)
            f.write(data)
        return file_path