### 前提・実現したいこと

ValueError: buffer size must be a multiple of element size

というエラーメッセージの解決方法を教えていただきたいです。

以下のプログラムは

任意のSignal-to-Noise比の音声波形をPythonで作ろう！  
<https://engineering.linecorp.com/ja/blog/voice-waveform-arbitrary-signal-to-
noise-ratio-python/>

音声処理 TypeError: 'int' object is not subscriptable  
<https://teratail.com/questions/306716>  
の質問などを参考にしています。

手順として、  
<https://github.com/Sato-Kunihiko/audio-SNR#usage>  
こちらのreadmeを参考に

pip install pipenv  
↓  
pipenv install  
↓  
pipenv shell

をした後に  
create_mixed_audio_file.py  
を実行しています。

### 発生している問題・エラーメッセージ

```

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-50-d5415e64c65c> in <module>
         46 
         47     clean_amp = cal_amp(clean_wav)
    ---> 48     noise_amp = cal_amp(noise_wav)
         49 
         50     start = random.randint(0, len(noise_amp)-len(clean_amp))
    
    <ipython-input-50-d5415e64c65c> in cal_amp(wf)
         27 def cal_amp(wf):
         28     buffer = wf.readframes(wf.getnframes())
    ---> 29     amptitude = (np.frombuffer(buffer, dtype="int16")).astype(np.float64)
         30     return amptitude
         31 
    
    ValueError: buffer size must be a multiple of element size
    
    
```

### 該当のソースコード

```

    import argparse
    import array
    import math
    import numpy as np
    import random
    import wave
    
    
    def get_args():
        parser = argparse.ArgumentParser()
        parser.add_argument('--clean_file', type=str, required=True)
        parser.add_argument('--noise_file', type=str, required=True)
        parser.add_argument('--output_clean_file', type=str, default='')
        parser.add_argument('--output_noise_file', type=str, default='')
        parser.add_argument('--output_noisy_file', type=str, default='', required=True)
        parser.add_argument('--snr', type=float, default='', required=True)
    
        args = parser.parse_args(args=['--clean_file',"sample.wav", '--noise_file', "noise.wav", '--output_clean_file',"clear.wav",'--output_noise_file',"noise.wav",'--output_noisy_file',"noisy.wav",'--snr',0])
        return args
    
    
    def cal_adjusted_rms(clean_rms, snr):
        a = float(snr) / 20
        noise_rms = clean_rms / (10**a) 
        return noise_rms
    
    
    def cal_amp(wf):
        buffer = wf.readframes(wf.getnframes())
        amptitude = (np.frombuffer(buffer, dtype="int16")).astype(np.float64)
        return amptitude
    
    
    def cal_rms(amp):
        return np.sqrt(np.mean(np.square(amp), axis=-1))
    
    
    if __name__ == '__main__':
    
        args = get_args()
        clean_file = args.clean_file
        noise_file = args.noise_file
        snr = args.snr
    
        clean_wav = wave.open(clean_file, "r")
        noise_wav = wave.open(noise_file, "r")
    
        clean_amp = cal_amp(clean_wav)
        noise_amp = cal_amp(noise_wav)
    
        start = random.randint(0, len(noise_amp)-len(clean_amp))
        clean_rms = cal_rms(clean_amp)
        split_noise_amp = noise_amp[start: start + len(clean_amp)]
        noise_rms = cal_rms(split_noise_amp)
    
        adjusted_noise_rms = cal_adjusted_rms(clean_rms, snr)
    
        adjusted_noise_amp = split_noise_amp * (adjusted_noise_rms / noise_rms) 
        mixed_amp = (clean_amp + adjusted_noise_amp)
    
    
        if (mixed_amp.max(axis=0) > 32767): 
            mixed_amp = mixed_amp * (32767/mixed_amp.max(axis=0))
            clean_amp = clean_amp * (32767/mixed_amp.max(axis=0))
            adjusted_noise_amp = adjusted_noise_amp * (32767/mixed_amp.max(axis=0))
    
    
        noisy_wave = wave.Wave_write(args.output_noisy_file)
        noisy_wave.setparams(clean_wav.getparams())
        noisy_wave.writeframes(array.array('h', mixed_amp.astype(np.int16)).tostring() )
        noisy_wave.close()
    
    
        clean_wave = wave.Wave_write(args.output_clean_file)
        clean_wave.setparams(clean_wav.getparams())
        clean_wave.writeframes(array.array('h', clean_amp.astype(np.int16)).tostring() )
        clean_wave.close()
    
    
        noise_wave = wave.Wave_write(args.output_noise_file)
        noise_wave.setparams(clean_wav.getparams())
        noise_wave.writeframes(array.array('h', adjusted_noise_amp.astype(np.int16)).tostring() )
        noise_wave.close()
    
```
CC BY-SA 4.0
2021-11-17T13:56:35.433
83685
2022-04-20T11:11:21.193
2022-04-20T11:11:21.193
3060
48401
question
python python3 anaconda
ValueError: buffer size must be a multiple of element sizeの解決方法