jstrait / wavefile

A Ruby gem for reading and writing sound files in Wave format (*.wav)
https://wavefilegem.com
MIT License
209 stars 24 forks source link

Example here - https://github.com/jstrait/wavefile/wiki/WaveFile-Tutorial#copying-a-wave-file-to-different-format working correctly? #15

Closed dbreaker closed 9 years ago

dbreaker commented 9 years ago

@jstrait sorry if this is a total newbie issue, but when I run the example here - https://github.com/jstrait/wavefile/wiki/WaveFile-Tutorial#copying-a-wave-file-to-different-format with a voice recording, the resulting audio file sound like chipmunks. Here's my code example. Any clue on what I am doing wrong? Great gem, love working with it so far.

require 'wavefile'
include WaveFile

new_file = 'data/copy.wav'

Writer.new(new_file, Format.new(:mono, :pcm_16, 16000)) do |writer|
  Reader.new('data/twilio_recording.wav').each_buffer(8000) do |buffer|
    writer.write(buffer)
  end
end
movstox commented 9 years ago

@jstrait I've also tried same sample but a bit different way. Seems like playback is just accelerated proportionally.

Writer.new("output.wav", Format.new(:stereo, :pcm_16, 44100)) do |writer|
                       Reader.new("input.wav").each_buffer(4096) do |buffer|
                         writer.write(buffer.convert!(Format.new(:stereo, :pcm_16, 44100)))
                       end
                     end

info('input.wav')
info('output.wav')

Output:

  File:        input.wav
  Audio Format:        1
  Channels:            1
  Bits per sample:     16
  Samples per second:  8000
  Bytes per second:    16000
  Block align:         2
  Sample frame count:  1134880
  Play time:           00:02:21:860

  File:        output.wav
  Audio Format:        1
  Channels:            2
  Bits per sample:     16
  Samples per second:  44100
  Bytes per second:    176400
  Block align:         4
  Sample frame count:  1134880
  Play time:           00:00:25:734

Btw, conversion worked for me via SoX library sox input.wav -b 32 -r 44100 output.wav:

 File:        input.wav
  Audio Format:        1
  Channels:            1
  Bits per sample:     16
  Samples per second:  8000
  Bytes per second:    16000
  Block align:         2
  Sample frame count:  1134880
  Play time:           00:02:21:860

  File:        output.wav
  Audio Format:        1
  Channels:            1
  Bits per sample:     16
  Samples per second:  44100
  Bytes per second:    88200
  Block align:         2
  Sample frame count:  6256026
  Play time:           00:02:21:860

Thank you in advance for any help.

jstrait commented 9 years ago

Hey @dbreaker, do you know what the sample rate of data/twilio_recording.wav is? Is it 16,000Hz, or something different? The reason I ask is that in your example data/copy.wav is being written with a sample rate of 16,000Hz (i.e. the 3rd argument to Format.new):

Writer.new(new_file, Format.new(:mono, :pcm_16, 16000))

If the sample rate of data/twilio_recording.wav is lower than 16,000Hz, then data/copy.wav would sound sped up, like you mentioned. Conversely, if data/twilio_recording.wav has a sample rate higher than 16,000Hz it will sound slowed down.

jstrait commented 9 years ago

@movstox the gem just writes whatever sample rate you give it into the resulting wave file. For example, if you have incoming sample data that had an original sample rate of 44,100Hz, and you write it out to a file using Writer with a sample rate of 22,050Hz, then the resulting file will have the same sample data as the original, but the sample rate will be set to 22,050Hz. This means that when played back with an audio player it will sound twice as slow with a lower pitch. (Conversely, a sample rate of 88,200Hz would sound twice as fast with a higher pitch). Unlike other parameters like bits per sample or number of channels, the gem won't attempt to convert the sample data so that it sounds the same with the new sample rate.

From your example Sox appears to be re-sampling the file to use the new sample rate (i.e. 44,100Hz instead of 8,000Hz) so that it has the same pitch. I say that because in the original, there are 1,134,880 sample frames, while in the output there are 6,256,026 sample frames. This would be consistent would adding additional samples so that the file sounds the same, even though the sample rate is higher.

jstrait commented 9 years ago

Since I believe that I answered the questions, and there haven't been any follow up comments in several months, closing this out.

movstox commented 9 years ago

Thanks for your help.

On 31.10.2015 at 19:19 GMT None wrote:

Since I believe that I answered the questions, and there haven't been any follow up comments in several months, closing this out.

— Reply to this email directly or https://github.com/jstrait/wavefile/issues/15#issuecomment-152765135<>.