Closed ksbek closed 4 years ago
Thanks! I’m glad the gem is useful.
Yes, two wave files can be mixed, and the volume for each file adjusted. The WaveFile gem doesn’t directly do this for you, but you can do it by modifying the sample data you read out of the individual files.
First, if you need to know the basics of how digital audio works, check out https://www.joelstrait.com/digital_audio_primer/.
You can adjust the volume of a file by multiplying each sample by a scale factor. For example, if you read data from a file as floating point data (i.e. using :float
when calling Format.new()
), then each sample will be a value between -1.0 and 1.0. If you multiply each sample by a number greater than 1.0 it will make the file louder, and if you multiply by a number between 0.0 and 1.0 it will make the file quieter.
However, if any resulting samples are larger than 1.0 or less than -1.0, the file will sound distorted. This is because -1.0 and 1.0 are the minimum and maximum sample values, so any value below/above will be clamped to be -1.0 or 1.0. This results in clipping distortion.
Some example code:
require "wavefile"
include WaveFile
SCALE_FACTOR = 1.5
Writer.new("modified.wav", Format.new(:mono, :pcm_16, 44100)) do |writer|
Reader.new("original.wav", Format.new(:mono, :float, 44100)).each_buffer do |buffer|
buffer.samples.map! {|sample| sample * SCALE_FACTOR }
writer.write(buffer)
end
end
To combine two files, take each pair of samples from the two files, add them together, and then write out the combined samples to a new file. For example:
[-0.3, 0.5, 0.75, -0.2]
[0.1, 0.6, 0.2, -0.5]
[-0.3 + 0.1, 0.5 + 0.6, 0.75 + 0.2, -0.2 + -0.5]
= [-0.2, 1.1, 0.95, -0.7]
.However, like above, you’ll want to make sure the samples don’t go below -1.0 or above 1.0, or the audio will sound distorted (like the 2nd combined sample above). One simple way to avoid this is to average each pair of samples. That is, set each output sample to (sample1 + sample2) / 2
.
Hope this helps!
Hello @jstrait,
Thank you for your reply. This is so useful information. It helps me.
Thanks again!
Hello,
This gem is useful and I'm very thanks to you. I have one question. Using your gem, can I mix 2 wave files and set the volume for each track? Looking forward your answer.