山内セミナーⅠ(2021/06/23)

関連サイトと資料

サンプルプログラム(2)

list6.py
import wave
  
wav = wave.open('sample1.wav', 'r')
  
nchannels, sampwidth, framerate, nframes, comptype, compname = wav.getparams()
print('チャネル数:{0}'.format(nchannels))
print('データサイズ:{0} [bytes]'.format(sampwidth))
print('サンプリング周波数:{0} [Hz]'.format(framerate))
print('サンプリング数:{0} [frames]'.format(nframes))
  
wav.close()
    

list7.py
import wave
import numpy as np
  
wav = wave.open('sample1.wav', 'r')
  
nchannels, sampwidth, framerate, nframes, comptype, compname = wav.getparams()
print('チャネル数:{0}'.format(nchannels))
print('データサイズ:{0} [bytes]'.format(sampwidth))
print('サンプリング周波数:{0} [Hz]'.format(framerate))
print('サンプリング数:{0} [frames]'.format(nframes))
  
data = wav.readframes(nframes)
wav_data = np.frombuffer(data, dtype='int16')
  
for n in wav_data:
    print(n)
  
wav.close()
    

list8.py
import soundfile as sf
  
data, fs = sf.read('sample1.wav')
data = data.flatten()
  
for n in data:
    print(n)
    

サンプルプログラム(3)

list9.py
import wave
import numpy as np
  
in_wav = wave.open('loop1.wav', 'r')
  
nchannels, sampwidth, framerate, nframes, comptype, compname = in_wav.getparams()
if nchannels != 2:
    print('error: input must stereo!')
    exit()
  
data = in_wav.readframes(nframes)
tmp_data = np.frombuffer(data, dtype='int16')
x = np.array(tmp_data)
  
# 左チャネルを削除
x[::2] = 0
  
# ファイルに出力
out_wav = wave.open('loop1_only_R.wav', 'w')
out_wav.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname))
out_wav.writeframes(x)
  
in_wav.close()
out_wav.close()
    

list10.py
import wave
import numpy as np
  
in_wav = wave.open('loop1.wav', 'r')
  
nchannels, sampwidth, framerate, nframes, comptype, compname = in_wav.getparams()
if nchannels != 2:
    print('error: input must stereo!')
    exit()
  
data = in_wav.readframes(nframes)
tmp_data = np.frombuffer(data, dtype='int16')
x = np.array(tmp_data)
  
# 右チャネルを削除
x[1::2] = 0
  
# ファイルに出力
out_wav = wave.open('loop1_only_L.wav', 'w')
out_wav.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname))
out_wav.writeframes(x)
  
in_wav.close()
out_wav.close()
    

サンプルプログラム(4)

list11.py
import wave
import numpy as np
  
in_wav = wave.open('loop1.wav', 'r')
  
nchannels, sampwidth, framerate, nframes, comptype, compname = in_wav.getparams()
if nchannels != 2:
    print('error: input must stereo!')
    exit()
  
data = in_wav.readframes(nframes)
tmp_data = np.frombuffer(data, dtype='int16')
  
# モノラル化
x = tmp_data[::2].copy()
  
# ファイルに出力
nchannels = 1
out_wav = wave.open('loop1_mono_fromL.wav', 'w')
out_wav.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname))
out_wav.writeframes(x)
  
in_wav.close()
out_wav.close()
    

list12.py
import wave
import numpy as np
  
in_wav = wave.open('loop1.wav', 'r')
  
nchannels, sampwidth, framerate, nframes, comptype, compname = in_wav.getparams()
if nchannels != 2:
    print('error: input must stereo!')
    exit()
  
data = in_wav.readframes(nframes)
tmp_data = np.frombuffer(data, dtype='int16')
  
# モノラル化
x = tmp_data[1::2].copy()
  
# ファイルに出力
nchannels = 1
out_wav = wave.open('loop1_mono_fromR.wav', 'w')
out_wav.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname))
out_wav.writeframes(x)
  
in_wav.close()
out_wav.close()
    

list13.py
import wave
import numpy as np
  
in_wav = wave.open('loop1.wav', 'r')
  
nchannels, sampwidth, framerate, nframes, comptype, compname = in_wav.getparams()
if nchannels != 2:
    print('error: input must stereo!')
    exit()
  
data = in_wav.readframes(nframes)
tmp_data = np.frombuffer(data, dtype='int16')
  
# モノラル化
d = tmp_data.reshape(nframes, 2)
x = d.mean(1).astype(dtype='int16')
  
# ファイルに出力
nchannels = 1
out_wav = wave.open('loop1_mono_mean.wav', 'w')
out_wav.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname))
out_wav.writeframes(x)
  
in_wav.close()
out_wav.close()