Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dbreaker opened this issue Dec 23, 2014 · 5 comments

Comments

@dbreaker
Copy link

@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
Copy link

movstox commented Dec 27, 2014

@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
Copy link
Owner

jstrait commented Feb 24, 2015

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
Copy link
Owner

jstrait commented Feb 24, 2015

@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
Copy link
Owner

jstrait commented Oct 31, 2015

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

@jstrait jstrait closed this as completed Oct 31, 2015
@movstox
Copy link

movstox commented Oct 31, 2015

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 #15 (comment)<>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants