[파이썬] pydub 오디오 감마 조절

In this blog post, we will explore how to adjust audio gamma using the PyDub library in Python. Gamma adjustment is a technique used to modify the brightness or contrast of an image or in this case, an audio file. By applying gamma adjustment, we can enhance or attenuate certain frequency ranges, thus manipulating the overall audio quality.

Installation

Before we proceed, make sure you have PyDub library installed in your Python environment. You can install it using pip:

pip install pydub

Loading the Audio File

First, we need to load the audio file that we want to adjust. PyDub supports various audio file formats such as WAV, MP3, etc. Here, we will assume that our audio file is in WAV format.

from pydub import AudioSegment

# Load the audio file
audio = AudioSegment.from_wav("audio.wav")

Adjusting the Gamma

To adjust the gamma of the audio, we need to manipulate the amplitude of the audio samples. PyDub provides a convenient method called apply_gain to apply gain in decibels to the audio.

# Adjust the gamma by applying gain
gamma = 1.5  # gamma value can be adjusted as per requirement
adjusted_audio = audio.apply_gain(gamma)

In the above code, we set the gamma value to 1.5. Increasing the gamma value will amplify the audio, while decreasing it will attenuate the audio.

Exporting the Adjusted Audio

After adjusting the gamma, we can export the modified audio to a new file using the export method. We need to specify the desired output format and the filename.

# Export the adjusted audio to a new file
adjusted_audio.export("adjusted_audio.wav", format="wav")

Conclusion

Adjusting the audio gamma using PyDub library in Python is a simple yet powerful technique to enhance or attenuate specific frequency ranges in an audio file. By varying the gamma value, we can experiment with different audio effects and create unique audio experiences.

Experiment with different gamma values and explore the impact on your audio files. Let your creativity guide you in achieving the desired audio adjustments. Happy coding!