Overlapping sound data with Numpy

Asked 2 years ago, Updated 2 years ago, 336 views

The graph that appears to be the sum of the elements by calculating the sum of the numpy arrays in Python was affected.

In order to avoid this, I would like to use something from numpy to superimpose the sound data.

Assuming song1 has 10 seconds of song data and song2 has 1 second of
How do I stack the noise in one 5-6 second of song1 so that I can listen to the sound that reflects it?

mixmusic=np.insert(song1,song1[5*sr:6*sr],song2)
IPython.display.Audio(mixmusic, rate=sr)#Enables you to listen to the finished sound data

I've tried various things like this, but no matter how I try, noise is inserted at the beginning and end of a 10-second song.

I would like to superimpose the following noise waveforms and song1 waveforms so that they do not affect each other.
The maximum noise value matches the maximum value of song1.
If you simply set song1[5sr:6sr]+=song2, the amplitude value will exceed the maximum values of song1 and song2, so I don't want to do that.

song2

song1 waveform

Results in simple sum

python numpy matplotlib

2022-09-30 21:51

2 Answers

There are song data song1 for 10 seconds and noise data song2 for 1 second.
I want to superimpose noise (song2) in song1 5-6 seconds.
So,

The finished song is
Same as 0-5 seconds for 0-5 seconds song1 (nothing has changed)
5-6 seconds song1 5-6 seconds and song2 superimposed
Same as 6-10 seconds for 6-10 seconds for song 1 (nothing has changed)
should be .

0-5 seconds for song1 and 5-6 seconds for song1 and 6-10 seconds for song1 are
You can get it from a slice of song1 such as song1 [0:5*sr], song1 [5*sr:6*sr], and song1 [6*sr:10*sr].

Wouldn't it be the song data that the questioner wants if song1[0:5*sr], song1[5*sr:6*sr] and song2 are superimposed and song1[6*sr:10*sr] are combined?


2022-09-30 21:51

You can start and end the wav file you want to insert with the silent interval, align the number of elements in the matrix, and synthesize.

import numpy as np
from scipy.io import wavfile

rate = 44100
pad_start_sec=5#Data insertion location

# Sample data (sin.wav, noise.wav) creation
def create_samples():
    seconds = 10 # sine wave data 10 seconds
    sin_wave=[i/20 for i in range(0, rate*seconds)]#i/20 to keep it low
    sin=np.int16 (np.sin(sin_wave)*32767)
    wavfile.write('sin.wav',rate,sin)
    noise=np.int16(np.random.uniform (-0.1, 0.1, rate)*32767)#noise data 1 second
    wavfile.write('noise.wav', rate, noise)

create_samples()
fs,sin=wavfile.read('sin.wav')
_, noise=wavfile.read('noise.wav')

# Add silence interval before and after noise
pad=np.int16 (np.zeros(sin.size)*32767)
pad_start=rate*5
pad [pad_start:pad_start+noise.size] = noise
# Synthesize and write
z=sin+pad
wavfile.write('out.wav',fs,z)

However, in a library environment, it is recommended that you use a library such as pydub.

Sample code using pydub.AudioSegment.overlay

from pydub import AudioSegment

sin=AudioSegment.from_file("sin.wav")
noise=AudioSegment.from_file("noise.wav")
output=sin.overlay (noise, position=5000)
output.export("out.wav", format='wav')


2022-09-30 21:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.