commit
6d810d2a6d
@ -0,0 +1,40 @@
|
||||
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
import wave
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def wav2pcm(wavfile, pcmfile, data_type=np.int16):
|
||||
f = open(wavfile, "rb")
|
||||
f.seek(0)
|
||||
f.read(44)
|
||||
data = np.fromfile(f, dtype=data_type)
|
||||
data.tofile(pcmfile)
|
||||
|
||||
|
||||
def pcm2wav(pcm_file, wav_file, channels=1, bits=16, sample_rate=16000):
|
||||
pcmf = open(pcm_file, 'rb')
|
||||
pcmdata = pcmf.read()
|
||||
pcmf.close()
|
||||
|
||||
if bits % 8 != 0:
|
||||
raise ValueError("bits % 8 must == 0. now bits:" + str(bits))
|
||||
|
||||
wavfile = wave.open(wav_file, 'wb')
|
||||
wavfile.setnchannels(channels)
|
||||
wavfile.setsampwidth(bits // 8)
|
||||
wavfile.setframerate(sample_rate)
|
||||
wavfile.writeframes(pcmdata)
|
||||
wavfile.close()
|
Loading…
Reference in new issue