2.77M

Sound processing

1.

Wave library. Sound
processing.
use commands of the Wave library to process sound
files.

2.

Success criteria
can demonstrate sound file structures and basic wave library methods;
can open and read a sound file using wave.open() and output the file
parameters;
can use methods to analyze WAV file parameters and output all required data
(sample rate, number of channels, etc.).
can create a new sound file with changed parameters (channels, sampling rate,
etc.).
can apply sound processing (e.g. change the playback speed), the result is
saved in a new file

3.

UNDERSTANDING SOUND
WAVES
Sound waves are vibrations that
travel through the air. They can
be represented as waveforms,
which are visual depictions of
sound. Understanding these
fundamentals is crucial for
effective audio manipulation in
Python.

4.

Python Libraries for Audio
Python offers several powerful
libraries for audio processing,
such as Wave, Pydub, and
Librosa These tools allow you to
analyze, manipulate, and create
sound les with ease, making
audio processing accessible and
fun!

5.

LOADING AUDIO FILES
To start working with audio in
Python, you rst need to load
your sound les. Using Wave,
you can easily load and
manipulate audio data,
turning complex audio into
manageable arrays for further
processing.

6.

BASIC AUDIO
MANIPULATIONS
Once your audio is loaded, you
can perform basic
manipulations like trimming,
normalizing, and changing
pitch. These techniques allow
you to create unique
soundscapes and enhance
your audio projects creatively.

7.

ADDING EFFECTS WITH
PYTHON
Python allows you to add
various effects to your audio,
such as reverb, echo, and
distortion. These effects can
signi cantly change the
character of your sound,
making your projects more
engaging and dynamic.

8.

Wave library. Sound processing.
The wave module provides a convenient
interface to the WAV audio format. It does
not support compression/decompression,
but it does support mono/stereo.
wave.open()
This function opens a file for
reading/writing audio data. The function
requires two parameters - first, the file
name, and second, the mode. The mode can
be 'wb' for writing audio data or 'rb' for
reading.

9.

obj = wave.open('sound.wav','wb')
The 'rb' mode returns a Wave_read object and the 'wb' mode returns a Wave_write
object.
The Wave_write object has the following methods.
Code snippets for discussion
The following code reads some parameters from a .wav file.
import wave
obj = wave.open('sound.wav','r')
print("Number of channels",obj.getnchannels())
print ("Sample width",obj.getsampwidth())
print ("Frame rate.",obj.getframerate())
print ("Number of frames",obj.getnframes())
print ("parameters:",obj.getparams())
obj.close()

10.

Opening and reading a file
(P) Explanation of how to open a sound file in WAV format.
Using methods in practice: wave.open(), getparams(), readframes().
Assignment for students: Open a WAV file, output its parameters and the number of frames.
Example code:
import wave
# Открытие файла
with wave.open('example.wav', 'rb') as wave_file:
params = wave_file.getparams() # Получение параметров файла
print(params)
# Чтение данных
frames = wave_file.readframes(wave_file.getnframes())
print(f'Количество кадров: {len(frames)}')

11.

Analyzing sound file parameters
(P) Organize students work in groups to use the getparams() command to analyze a
file.
Discuss sound file parameters: sampling rate, number of channels, frame length.
Assignment for students: Analyze the parameters of the provided WAV file and
write code.
Example code:
with wave.open('example.wav', 'rb') as wave_file:
channels, sampwidth, framerate, nframes, comptype, compname =
wave_file.getparams()
print(f'Количество каналов: {channels}')
print(f'Частота дискретизации: {framerate}')

12.

Writing and creating a sound file.
(P) Learning how to create and write a sound file using the wave.write() command.
Examine the methods setnchannels(), setsampwidth(), setframerate().
Assignment for students: Create a new WAV file with modified parameters based on
the original file.
Example code:
with wave.open('new_sound.wav', 'wb') as new_wave_file:
new_wave_file.setnchannels(1) # Моно звук
new_wave_file.setsampwidth(2) # 16-битный звук
new_wave_file.setframerate(44100) # Частота #дискретизации 44.1 кГц
new_wave_file.writeframes(frames) # Запись фреймов

13.

Processing a sound file.
(W) Discussion of sound processing: changing volume, playback speed.
Demonstration of an example of changing the playback speed of a sound file.
Assignment for students: Create a file with a modified playback speed.
Example code:
with wave.open('example.wav', 'rb') as wave_file:
params = wave_file.getparams()
frames = wave_file.readframes(wave_file.getnframes())
# Увеличение скорости воспроизведения
with wave.open('faster_sound.wav', 'wb') as new_wave_file:
new_wave_file.setparams(params)
new_wave_file.setframerate(params.framerate * 2) #Удвоение частоты дискретизации
new_wave_file.writeframes(frames)

14.

Task 1: Extract data from a part of an audio file
Condition:
You have a sound.wav file and you need to extract the first 5 seconds of sound. Your task is to
read the first 5 seconds of sound data and save it to a new file sound_short.wav.
Hint:
To do this, you need to know the sampling rate of the file (number of samples per second) and
the number of channels.
Task 2: Creating a stereo sound based on two monaural files
Condition:
You have two monaural sound files: left_channel.wav and right_channel.wav. You need to
combine them and create a stereo sound by writing it to a new file stereo_sound.wav.
Hint:
Each channel in stereo sound has its own track: left and right.

15.

Task 3: Decreasing the volume of an audio file
Condition:
You need to reduce the volume of the sound file loud_sound.wav by 50%. To do this, halve the
amplitude values of each frame and save the result to a new file soft_sound.wav.
Hint:
Frames are represented by byte data, which you need to translate into numeric values, reduce
them, and encode them back.
Task 4: Changing the sampling rate
Condition:
You need to change the sampling rate of the sound file original.wav from 44100 Hz to 22050 Hz
(a 2-fold reduction), without changing the sound itself (it should just play faster).
Hint:
Change the sample rate, but leave the original frame data.

16.

EXPORTING YOUR
CREATIONS
After transforming your audio
les, it's time to export them.
Python makes it easy to save
your creations in various
formats, such as WAV or MP3,
ensuring your work is
accessible and shareable.

17.

Real-World Applications
The techniques we've discussed
have many real-world
applications. From music
production to sound design in
lms and games, mastering
audio manipulation with Python
opens up endless creative
possibilities.

18.

Homework
-Repetition of the basic
commands of the wave library
-Implement a program that will
change the volume of an audio
file

19.

Reflection

20.

Thanks!
English     Русский Rules