반응형
import wave
import soundfile as sf
# WAV 파일의 형식 확인 함수
def check_wave_format(file_path):
with wave.open(file_path, 'rb') as wav_file:
# WAV 파일의 형식 확인
print(f"Format: {wav_file.getsampwidth() * 8} bits per sample,
{wav_file.getnchannels()} channels, {wav_file.getframerate()} frames per second")
# 볼륨을 증폭하는 함수
def increase_volume(input_path, output_path, gain):
# WAV 파일의 형식 확인
check_wave_format(input_path)
with wave.open(input_path, 'rb') as wav_file:
params = wav_file.getparams()
frames = wav_file.readframes(params.nframes)
# WAV 파일의 각 샘플에 gain을 곱하여 음량을 조절합니다.
new_frames = bytearray()
for i in range(0, len(frames), params.sampwidth):
sample = int.from_bytes(frames[i:i+params.sampwidth], byteorder='little', signed=True)
new_sample = int(min(max(sample * gain, -2**(params.sampwidth*8-1)), 2**(params.sampwidth*8-1) - 1))
new_frames.extend(new_sample.to_bytes(params.sampwidth, byteorder='little', signed=True))
# 수정된 프레임을 쓰기
with wave.open(output_path, 'wb') as wav_file:
wav_file.setparams(params)
wav_file.writeframes(new_frames)
# '1.wav' 파일을 읽어 PCM_16 형식으로 다시 쓰기
data, samplerate = sf.read('in.wav')
sf.write('in.wav', data, samplerate, subtype='PCM_16')
# 처리할 파일 경로, 출력 파일 경로, 증폭값 설정
input_file = "input.wav"
output_file = "output.wav"
gain = 10
# 볼륨 증폭 함수 호출
increase_volume(input_file, output_file, gain)
|
cs |
참고! 위코드는 wav 인코딩을
IEEE Float -> PCM 으로 바꾼다.
반응형
'파이썬 > 그외' 카테고리의 다른 글
아나콘다 파이썬 jupyter notebook에서 이미지가 open이 안된다면 (0) | 2023.07.13 |
---|---|
파이썬 - 클래스 - 상속 , 출력 (0) | 2023.07.07 |
파이썬 간단한 exe 파일 만들기 (0) | 2023.05.07 |
파이썬 단어 찾기 or 문장 찾기 .count() (0) | 2022.09.30 |
파이썬 문자열에 공백 제거하기 리스트에서 리스트 (0) | 2022.05.10 |
댓글