wav-file 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +8 -0
- data/Manifest.txt +26 -0
- data/README.rdoc +90 -0
- data/Rakefile +25 -0
- data/lib/wav-file.rb +5 -0
- data/lib/wav-file/wav-file.rb +115 -0
- data/samples/adjust_wav_format.rb +25 -0
- data/samples/compare_formats.rb +24 -0
- data/samples/composite.rb +50 -0
- data/samples/connect_wav_files.rb +35 -0
- data/samples/copy_wav.rb +22 -0
- data/samples/double_speed.rb +26 -0
- data/samples/dump_wav.rb +35 -0
- data/samples/maximize_volume.rb +37 -0
- data/samples/read_data_chunk.rb +18 -0
- data/samples/read_format.rb +13 -0
- data/samples/read_format_and_chunks.rb +16 -0
- data/samples/reverse.rb +32 -0
- data/samples/split_channels.rb +47 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/wav-file_spec.rb +11 -0
- data/tasks/rspec.rake +21 -0
- metadata +126 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
README.rdoc
|
4
|
+
Rakefile
|
5
|
+
lib/wav-file.rb
|
6
|
+
lib/wav-file/wav-file.rb
|
7
|
+
script/console
|
8
|
+
script/destroy
|
9
|
+
script/generate
|
10
|
+
spec/spec.opts
|
11
|
+
spec/spec_helper.rb
|
12
|
+
spec/wav-file_spec.rb
|
13
|
+
tasks/rspec.rake
|
14
|
+
samples/reverse.rb
|
15
|
+
samples/double_speed.rb
|
16
|
+
samples/read_data_chunk.rb
|
17
|
+
samples/dump_wav.rb
|
18
|
+
samples/connect_wav_files.rb
|
19
|
+
samples/read_format_and_chunks.rb
|
20
|
+
samples/copy_wav.rb
|
21
|
+
samples/compare_formats.rb
|
22
|
+
samples/adjust_wav_format.rb
|
23
|
+
samples/split_channels.rb
|
24
|
+
samples/maximize_volume.rb
|
25
|
+
samples/composite.rb
|
26
|
+
samples/read_format.rb
|
data/README.rdoc
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
= wav-file
|
2
|
+
|
3
|
+
* http://github.com/shokai/ruby-wav-file
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Read wav file format and Edit data chunk
|
8
|
+
|
9
|
+
|
10
|
+
== TIPS:
|
11
|
+
|
12
|
+
To run samples, you need wav file.
|
13
|
+
"ffmpeg" is good tool for making wav file from mp3.
|
14
|
+
|
15
|
+
% ffmpeg -i input.mp3 output.wav
|
16
|
+
|
17
|
+
Wav-file gem cannot convert format of wav file.
|
18
|
+
Before edit data-chunk of wav files, you should adjust them using ffmpeg.
|
19
|
+
|
20
|
+
|
21
|
+
== SYNOPSIS:
|
22
|
+
|
23
|
+
|
24
|
+
Read format and chunks
|
25
|
+
|
26
|
+
require 'rubygems'
|
27
|
+
require 'wav-file'
|
28
|
+
|
29
|
+
f = open("input.wav")
|
30
|
+
format = WavFile::readFormat(f)
|
31
|
+
dataChunk = WavFile::readDataChunk(f)
|
32
|
+
f.close
|
33
|
+
|
34
|
+
puts format
|
35
|
+
|
36
|
+
|
37
|
+
Extract wav from binary
|
38
|
+
|
39
|
+
bit = 's*' if format.bitPerSample == 16 # int16_t
|
40
|
+
bit = 'c*' if format.bitPerSample == 8 # signed char
|
41
|
+
wavs = dataChunk.data.unpack(bit) # read binary
|
42
|
+
|
43
|
+
|
44
|
+
Change volume half
|
45
|
+
|
46
|
+
wavs = wavs.map{|w| w/2}
|
47
|
+
|
48
|
+
|
49
|
+
Reverse wav and encode to binary
|
50
|
+
|
51
|
+
dataChunk.data = wavs.reverse.pack(bit) # reverse
|
52
|
+
|
53
|
+
|
54
|
+
Save to file
|
55
|
+
|
56
|
+
open("output.wav", "w"){|out|
|
57
|
+
WavFile::write(out, format, [dataChunk])
|
58
|
+
}
|
59
|
+
|
60
|
+
See "samples" directory.
|
61
|
+
|
62
|
+
|
63
|
+
== INSTALL:
|
64
|
+
|
65
|
+
* gem install wav-file
|
66
|
+
|
67
|
+
== LICENSE:
|
68
|
+
|
69
|
+
(The MIT License)
|
70
|
+
|
71
|
+
Copyright (c) 2010 Sho Hashimoto
|
72
|
+
|
73
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
74
|
+
a copy of this software and associated documentation files (the
|
75
|
+
'Software'), to deal in the Software without restriction, including
|
76
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
77
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
78
|
+
permit persons to whom the Software is furnished to do so, subject to
|
79
|
+
the following conditions:
|
80
|
+
|
81
|
+
The above copyright notice and this permission notice shall be
|
82
|
+
included in all copies or substantial portions of the Software.
|
83
|
+
|
84
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
85
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
86
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
87
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
88
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
89
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
90
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'hoe', '>= 2.1.0'
|
3
|
+
require 'hoe'
|
4
|
+
require 'fileutils'
|
5
|
+
require './lib/wav-file'
|
6
|
+
|
7
|
+
Hoe.plugin :newgem
|
8
|
+
# Hoe.plugin :website
|
9
|
+
# Hoe.plugin :cucumberfeatures
|
10
|
+
|
11
|
+
# Generate all the Rake tasks
|
12
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
13
|
+
$hoe = Hoe.spec 'wav-file' do
|
14
|
+
self.developer 'Sho Hashimoto', 'hashimoto@shokai.org'
|
15
|
+
self.rubyforge_name = self.name # TODO this is default value
|
16
|
+
# self.extra_deps = [['activesupport','>= 2.0.2']]
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'newgem/tasks'
|
21
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
22
|
+
|
23
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
24
|
+
# remove_task :default
|
25
|
+
# task :default => [:spec, :features]
|
data/lib/wav-file.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
module WavFile
|
3
|
+
|
4
|
+
class WavFormatError < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
class Chunk
|
8
|
+
attr_accessor(:name, :size, :data)
|
9
|
+
|
10
|
+
def initialize(file)
|
11
|
+
@name = file.read(4)
|
12
|
+
@size = file.read(4).unpack("V")[0].to_i
|
13
|
+
@data = file.read(@size)
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_bin
|
17
|
+
@name + [@data.size].pack('V') + @data
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Format
|
22
|
+
attr_accessor(:id, :channel, :hz, :bytePerSec, :blockSize, :bitPerSample)
|
23
|
+
|
24
|
+
def initialize(chunk)
|
25
|
+
return if chunk.class != Chunk
|
26
|
+
return if chunk.name != 'fmt '
|
27
|
+
@id = chunk.data.slice(0,2)[0].to_i
|
28
|
+
@channel = chunk.data.slice(2,2)[0].to_i
|
29
|
+
@hz = chunk.data.slice(4,4).unpack('V').join.to_i
|
30
|
+
@bytePerSec = chunk.data.slice(8,4).unpack('V').join.to_i
|
31
|
+
@blockSize = chunk.data.slice(12,2)[0].to_i
|
32
|
+
@bitPerSample = chunk.data.slice(14,2)[0].to_i
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_s
|
36
|
+
<<EOS
|
37
|
+
Format ID: #{@id}
|
38
|
+
Channels: #{@channel}
|
39
|
+
Sampling Ratio: #{@hz} (Hz)
|
40
|
+
Byte per Sec: #{@bytePerSec}
|
41
|
+
Bit per Sample: #{@bitPerSample}
|
42
|
+
Block Size: #{blockSize}
|
43
|
+
EOS
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_bin
|
47
|
+
[@id].pack('v')+
|
48
|
+
[@channel].pack('v') +
|
49
|
+
[@hz].pack('V') +
|
50
|
+
[@bytePerSec].pack('V') +
|
51
|
+
[@blockSize].pack('v') +
|
52
|
+
[@bitPerSample].pack('v')
|
53
|
+
end
|
54
|
+
|
55
|
+
def ==(target)
|
56
|
+
@id == target.id &&
|
57
|
+
@channel == target.channel &&
|
58
|
+
@hz == target.hz &&
|
59
|
+
@bytePerSec == target.bytePerSec &&
|
60
|
+
@bitPerSample == target.bitPerSample &&
|
61
|
+
@blockSize == target.blockSize
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
def WavFile.readFormat(f)
|
67
|
+
f.binmode
|
68
|
+
f.seek(0)
|
69
|
+
header = f.read(12)
|
70
|
+
riff = header.slice(0,4)
|
71
|
+
data_size = header.slice(4,4).unpack('V')[0].to_i
|
72
|
+
wave = header.slice(8,4)
|
73
|
+
raise(WavFormatError) if riff != 'RIFF' or wave != 'WAVE'
|
74
|
+
|
75
|
+
formatChunk = Chunk.new(f)
|
76
|
+
Format.new(formatChunk)
|
77
|
+
end
|
78
|
+
|
79
|
+
def WavFile.readAll(f)
|
80
|
+
format = readFormat(f)
|
81
|
+
chunks = Array.new
|
82
|
+
while !f.eof?
|
83
|
+
chunk = Chunk.new(f)
|
84
|
+
chunks << chunk
|
85
|
+
end
|
86
|
+
return format, chunks
|
87
|
+
end
|
88
|
+
|
89
|
+
def WavFile.readDataChunk(f)
|
90
|
+
format, chunks = readAll(f)
|
91
|
+
chunks.each{|c|
|
92
|
+
return c if c.name == 'data'
|
93
|
+
}
|
94
|
+
return nil
|
95
|
+
end
|
96
|
+
|
97
|
+
def WavFile.read(f)
|
98
|
+
return readFormat(f), readDataChunk(f)
|
99
|
+
end
|
100
|
+
|
101
|
+
def WavFile.write(f, format, dataChunks)
|
102
|
+
header_file_size = 4
|
103
|
+
dataChunks.each{|c|
|
104
|
+
header_file_size += c.data.size + 8
|
105
|
+
}
|
106
|
+
f.write('RIFF' + [header_file_size].pack('V') + 'WAVE')
|
107
|
+
f.write("fmt ")
|
108
|
+
f.write([format.to_bin.size].pack('V'))
|
109
|
+
f.write(format.to_bin)
|
110
|
+
dataChunks.each{|c|
|
111
|
+
f.write(c.to_bin)
|
112
|
+
}
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# adjust same format using ffmpeg
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'wav-file'
|
6
|
+
|
7
|
+
if ARGV.size < 2
|
8
|
+
puts 'ruby base.wav input1.wav [..input2.wav]'
|
9
|
+
exit 1
|
10
|
+
end
|
11
|
+
|
12
|
+
base_file = ARGV.shift
|
13
|
+
|
14
|
+
base_format = WavFile::readFormat open(base_file)
|
15
|
+
puts base_format
|
16
|
+
|
17
|
+
ARGV.each{|f|
|
18
|
+
format = WavFile::readFormat open(f)
|
19
|
+
if format != base_format
|
20
|
+
puts `ffmpeg -i #{f} -ac #{base_format.channel} -ar #{base_format.hz} fixed_#{f}`
|
21
|
+
end
|
22
|
+
}
|
23
|
+
|
24
|
+
|
25
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# compare formats
|
3
|
+
require 'rubygems'
|
4
|
+
require 'wav-file'
|
5
|
+
|
6
|
+
if ARGV.size < 2
|
7
|
+
puts 'ruby compare_formats.rb input1.wav input2.wav'
|
8
|
+
exit 1
|
9
|
+
end
|
10
|
+
|
11
|
+
formats = ARGV.map{|file_name|
|
12
|
+
format = WavFile::readFormat open(file_name)
|
13
|
+
puts format.to_s
|
14
|
+
puts '==='
|
15
|
+
format
|
16
|
+
}
|
17
|
+
|
18
|
+
if formats[0] == formats[1]
|
19
|
+
puts 'same format'
|
20
|
+
else
|
21
|
+
puts 'different format'
|
22
|
+
end
|
23
|
+
|
24
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# composite wav files
|
3
|
+
require 'rubygems'
|
4
|
+
require 'wav-file'
|
5
|
+
|
6
|
+
if ARGV.size < 4
|
7
|
+
puts 'e.g. ruby composite.rb base.wav input1.wav 1 3 10 out.wav'
|
8
|
+
puts ' => put "input1.wav" on 1,3,10(sec)'
|
9
|
+
exit 1
|
10
|
+
end
|
11
|
+
|
12
|
+
out_file = ARGV.pop
|
13
|
+
base_file = ARGV.shift
|
14
|
+
input_file =ARGV.shift
|
15
|
+
times = ARGV.map{|i|i.to_f}.uniq.sort
|
16
|
+
|
17
|
+
|
18
|
+
format1, data1 = WavFile::read open(base_file)
|
19
|
+
format2, data2 = WavFile::read open(input_file)
|
20
|
+
|
21
|
+
puts format1.to_s
|
22
|
+
if format1 != format2
|
23
|
+
puts 'file formats are different!'
|
24
|
+
puts format2.to_s
|
25
|
+
exit 1
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
bit = 's*' if format1.bitPerSample == 16 # int16_t
|
30
|
+
bit = 'c*' if format1.bitPerSample == 8 # signed char
|
31
|
+
wavs1 = data1.data.unpack(bit)
|
32
|
+
wavs2 = data2.data.unpack(bit)
|
33
|
+
|
34
|
+
times.each{|t|
|
35
|
+
offset = format1.hz * t * format1.channel
|
36
|
+
next if offset+wavs2.size > wavs1.size
|
37
|
+
print "#{t}(sec)..."
|
38
|
+
for i in 0..wavs2.size-1 do
|
39
|
+
wav1 = wavs1[i+offset]
|
40
|
+
wav2 = wavs2[i] * 0.3 # volume down
|
41
|
+
wavs1[i+offset] = ((wav1+wav2)/2).to_i
|
42
|
+
end
|
43
|
+
puts ''
|
44
|
+
}
|
45
|
+
|
46
|
+
data1.data = wavs1.pack(bit)
|
47
|
+
|
48
|
+
open(out_file, "w"){|out|
|
49
|
+
WavFile::write(out, format1, [data1])
|
50
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# connect files
|
3
|
+
require 'rubygems'
|
4
|
+
require 'wav-file'
|
5
|
+
|
6
|
+
if ARGV.size < 3
|
7
|
+
puts 'ruby connect_wav_files.rb input1.wav input2.wav output.wav'
|
8
|
+
exit 1
|
9
|
+
end
|
10
|
+
|
11
|
+
out_file = ARGV.pop
|
12
|
+
|
13
|
+
formats = ARGV.uniq.map{|file_name|
|
14
|
+
WavFile::readFormat open(file_name)
|
15
|
+
}
|
16
|
+
for i in 0..formats.size-2 do
|
17
|
+
if formats[i] != formats[i+1]
|
18
|
+
puts 'different format!'
|
19
|
+
exit 1
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
format = formats.first
|
24
|
+
dataChunk = WavFile::readDataChunk(open(ARGV.shift))
|
25
|
+
|
26
|
+
ARGV.each{|f|
|
27
|
+
data = WavFile::readDataChunk(open(f))
|
28
|
+
dataChunk.data += data.data
|
29
|
+
}
|
30
|
+
|
31
|
+
puts format
|
32
|
+
|
33
|
+
open(out_file, "w"){|out|
|
34
|
+
WavFile::write(out, format, [dataChunk])
|
35
|
+
}
|
data/samples/copy_wav.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# read and write
|
3
|
+
require 'rubygems'
|
4
|
+
require 'wav-file'
|
5
|
+
|
6
|
+
if ARGV.size < 2
|
7
|
+
puts 'ruby copy_wav.rb input.wav output.wav'
|
8
|
+
exit 1
|
9
|
+
end
|
10
|
+
|
11
|
+
f = open(ARGV.shift)
|
12
|
+
format, chunks = WavFile::readAll(f)
|
13
|
+
f.close
|
14
|
+
|
15
|
+
puts format.to_s
|
16
|
+
chunks.each{|c|
|
17
|
+
puts "#{c.name} #{c.size}"
|
18
|
+
}
|
19
|
+
|
20
|
+
open(ARGV.shift, "w"){|out|
|
21
|
+
WavFile::write(out, format, chunks)
|
22
|
+
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
# double speed
|
4
|
+
require 'rubygems'
|
5
|
+
require 'wav-file'
|
6
|
+
|
7
|
+
if ARGV.size < 2
|
8
|
+
puts 'ruby double_speed.rb input.rb output.wav'
|
9
|
+
exit 1
|
10
|
+
end
|
11
|
+
|
12
|
+
f = open(ARGV.shift)
|
13
|
+
format, chunks = WavFile::readAll(f)
|
14
|
+
f.close
|
15
|
+
|
16
|
+
puts format.to_s
|
17
|
+
puts '===double speed==='
|
18
|
+
format.hz *= 2
|
19
|
+
format.bytePerSec *= 2
|
20
|
+
puts format.to_s
|
21
|
+
|
22
|
+
|
23
|
+
open(ARGV.shift, "w"){|out|
|
24
|
+
WavFile::write(out, format, chunks)
|
25
|
+
}
|
26
|
+
|
data/samples/dump_wav.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# using this script and Excel, you can make a graph.
|
3
|
+
# => http://www.flickr.com/photos/shokai/4081858478/sizes/o/
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'wav-file'
|
7
|
+
|
8
|
+
if ARGV.size < 2
|
9
|
+
puts 'ruby dump_wav.rb input.wav dump.txt'
|
10
|
+
exit 1
|
11
|
+
end
|
12
|
+
|
13
|
+
format = nil
|
14
|
+
dataChunk = nil
|
15
|
+
File.open(ARGV[0]){|file|
|
16
|
+
format, chunks = WavFile::readAll(file)
|
17
|
+
puts format.to_s
|
18
|
+
chunks.each{|c|
|
19
|
+
puts "chunk - #{c.name} #{c.size}"
|
20
|
+
dataChunk = c if c.name == 'data'
|
21
|
+
}
|
22
|
+
}
|
23
|
+
|
24
|
+
bit = 's*' if format.bitPerSample == 16 # int16_t
|
25
|
+
bit = 'c*' if format.bitPerSample == 8 # signed char
|
26
|
+
wavs = dataChunk.data.unpack(bit) # read binary
|
27
|
+
|
28
|
+
open(ARGV[1],'w'){|dump|
|
29
|
+
wavs.each{|i|
|
30
|
+
dump.puts i
|
31
|
+
puts i
|
32
|
+
}
|
33
|
+
}
|
34
|
+
puts wavs.size
|
35
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# maximize volume
|
3
|
+
require 'rubygems'
|
4
|
+
require 'wav-file'
|
5
|
+
|
6
|
+
if ARGV.size < 2
|
7
|
+
puts 'ruby maximize_volume.rb input.wav output.wav'
|
8
|
+
exit 1
|
9
|
+
end
|
10
|
+
|
11
|
+
in_file = ARGV.shift
|
12
|
+
out_file = ARGV.shift
|
13
|
+
|
14
|
+
format, data = WavFile::read open(in_file)
|
15
|
+
|
16
|
+
puts format.to_s
|
17
|
+
|
18
|
+
bit = 's*' if format.bitPerSample == 16 # int16_t
|
19
|
+
bit = 'c*' if format.bitPerSample == 8 # signed char
|
20
|
+
wavs = data.data.unpack(bit)
|
21
|
+
|
22
|
+
puts "max of this file: #{wavs.max}"
|
23
|
+
|
24
|
+
volume_ratio = 32768/wavs.max.to_f if format.bitPerSample == 16
|
25
|
+
volume_ratio = 128/wavs.max.to_f if format.bitPerSample == 8
|
26
|
+
puts "maximize ratio: #{volume_ratio}"
|
27
|
+
|
28
|
+
wavs_fixed = wavs.map{|w|
|
29
|
+
(w*volume_ratio).to_i
|
30
|
+
}
|
31
|
+
puts "maximized: #{wavs_fixed.max}"
|
32
|
+
|
33
|
+
data.data = wavs_fixed.pack(bit)
|
34
|
+
|
35
|
+
open(out_file, "w"){|out|
|
36
|
+
WavFile::write(out, format, [data])
|
37
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# wav file has multiple chunks.
|
3
|
+
# using WavFile::readDataChunk, you can find data chunk from chunks
|
4
|
+
require 'rubygems'
|
5
|
+
require 'wav-file'
|
6
|
+
|
7
|
+
if ARGV.size < 1
|
8
|
+
puts 'ruby read_data_chunk.rb input.wav'
|
9
|
+
exit 1
|
10
|
+
end
|
11
|
+
|
12
|
+
File.open(ARGV[0]){|file|
|
13
|
+
format = WavFile::readFormat(file)
|
14
|
+
puts format.to_s
|
15
|
+
|
16
|
+
dataChunk = WavFile::readDataChunk(file)
|
17
|
+
puts "#{dataChunk.name} #{dataChunk.size}"
|
18
|
+
}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'wav-file'
|
4
|
+
|
5
|
+
if ARGV.size < 1
|
6
|
+
puts 'ruby read_format_and_chanks.rb input.wav'
|
7
|
+
exit 1
|
8
|
+
end
|
9
|
+
|
10
|
+
File.open(ARGV.first){|file|
|
11
|
+
format, chunks = WavFile::readAll(file)
|
12
|
+
puts format.to_s
|
13
|
+
chunks.each{|c|
|
14
|
+
puts "chunk - #{c.name} #{c.size}"
|
15
|
+
}
|
16
|
+
}
|
data/samples/reverse.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# reverse wav file
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'wav-file'
|
6
|
+
|
7
|
+
if ARGV.size < 2
|
8
|
+
puts 'ruby reverse.rb input.rb output.wav'
|
9
|
+
exit 1
|
10
|
+
end
|
11
|
+
|
12
|
+
f = open(ARGV.shift)
|
13
|
+
format = WavFile::readFormat(f)
|
14
|
+
dataChunk = WavFile::readDataChunk(f)
|
15
|
+
f.close
|
16
|
+
|
17
|
+
puts format
|
18
|
+
|
19
|
+
if dataChunk == nil
|
20
|
+
puts 'no data chunk'
|
21
|
+
exit 1
|
22
|
+
end
|
23
|
+
|
24
|
+
bit = 's*' if format.bitPerSample == 16 # int16_t
|
25
|
+
bit = 'c*' if format.bitPerSample == 8 # signed char
|
26
|
+
wavs = dataChunk.data.unpack(bit) # read binary
|
27
|
+
dataChunk.data = wavs.reverse.pack(bit) # reverse
|
28
|
+
|
29
|
+
open(ARGV.shift, "w"){|out|
|
30
|
+
WavFile::write(out, format, [dataChunk])
|
31
|
+
}
|
32
|
+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
# split left/right channels.
|
4
|
+
require 'rubygems'
|
5
|
+
require 'wav-file'
|
6
|
+
|
7
|
+
if ARGV.size < 2
|
8
|
+
puts 'e.g. ruby splitChannelWav.rb input.rb output_left.wav output_right.wav'
|
9
|
+
puts ' => make 2 wav files'
|
10
|
+
exit 1
|
11
|
+
end
|
12
|
+
|
13
|
+
f = open(ARGV.shift)
|
14
|
+
format, chunks = WavFile::readAll(f)
|
15
|
+
f.close
|
16
|
+
|
17
|
+
puts format.to_s
|
18
|
+
|
19
|
+
dataChunk = nil
|
20
|
+
chunks.each{|c|
|
21
|
+
puts "#{c.name} #{c.size}"
|
22
|
+
dataChunk = c if c.name == 'data' # find data chank
|
23
|
+
}
|
24
|
+
if dataChunk == nil
|
25
|
+
puts 'no data chunk'
|
26
|
+
exit 1
|
27
|
+
end
|
28
|
+
|
29
|
+
bit = 's*' if format.bitPerSample == 16 # int16_t
|
30
|
+
bit = 'c*' if format.bitPerSample == 8 # signed char
|
31
|
+
wavs = dataChunk.data.unpack(bit) # read binary
|
32
|
+
|
33
|
+
off = 32768 if format.bitPerSample == 16
|
34
|
+
off = 128 if format.bitPerSample == 8
|
35
|
+
|
36
|
+
for i in 0..1 do # LR
|
37
|
+
wavs_mono = wavs.dup
|
38
|
+
for j in 0..wavs_mono.size-1 do
|
39
|
+
if j%2 != i
|
40
|
+
wavs_mono[j] = off # LRLRLR..
|
41
|
+
end
|
42
|
+
end
|
43
|
+
dataChunk.data = wavs_mono.pack(bit)
|
44
|
+
open(ARGV.shift, "w"){|out|
|
45
|
+
WavFile::write(out, format, [dataChunk])
|
46
|
+
}
|
47
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/wav-file.rb'}"
|
9
|
+
puts "Loading wav-file gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/spec/spec_helper.rb
ADDED
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
5
|
+
require 'spec'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'spec/rake/spectask'
|
9
|
+
rescue LoadError
|
10
|
+
puts <<-EOS
|
11
|
+
To use rspec for testing you must install rspec gem:
|
12
|
+
gem install rspec
|
13
|
+
EOS
|
14
|
+
exit(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run the specs under spec/models"
|
18
|
+
Spec::Rake::SpecTask.new do |t|
|
19
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
20
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wav-file
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sho Hashimoto
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-18 00:00:00 +09:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rubyforge
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
- 4
|
34
|
+
version: 2.0.4
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: hoe
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 19
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 7
|
49
|
+
- 0
|
50
|
+
version: 2.7.0
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
description: Read wav file format and Edit data chunk
|
54
|
+
email:
|
55
|
+
- hashimoto@shokai.org
|
56
|
+
executables: []
|
57
|
+
|
58
|
+
extensions: []
|
59
|
+
|
60
|
+
extra_rdoc_files:
|
61
|
+
- History.txt
|
62
|
+
- Manifest.txt
|
63
|
+
files:
|
64
|
+
- History.txt
|
65
|
+
- Manifest.txt
|
66
|
+
- README.rdoc
|
67
|
+
- Rakefile
|
68
|
+
- lib/wav-file.rb
|
69
|
+
- lib/wav-file/wav-file.rb
|
70
|
+
- script/console
|
71
|
+
- script/destroy
|
72
|
+
- script/generate
|
73
|
+
- spec/spec.opts
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- spec/wav-file_spec.rb
|
76
|
+
- tasks/rspec.rake
|
77
|
+
- samples/reverse.rb
|
78
|
+
- samples/double_speed.rb
|
79
|
+
- samples/read_data_chunk.rb
|
80
|
+
- samples/dump_wav.rb
|
81
|
+
- samples/connect_wav_files.rb
|
82
|
+
- samples/read_format_and_chunks.rb
|
83
|
+
- samples/copy_wav.rb
|
84
|
+
- samples/compare_formats.rb
|
85
|
+
- samples/adjust_wav_format.rb
|
86
|
+
- samples/split_channels.rb
|
87
|
+
- samples/maximize_volume.rb
|
88
|
+
- samples/composite.rb
|
89
|
+
- samples/read_format.rb
|
90
|
+
has_rdoc: true
|
91
|
+
homepage: http://github.com/shokai/ruby-wav-file
|
92
|
+
licenses: []
|
93
|
+
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options:
|
96
|
+
- --main
|
97
|
+
- README.rdoc
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
hash: 3
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
version: "0"
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
hash: 3
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
version: "0"
|
118
|
+
requirements: []
|
119
|
+
|
120
|
+
rubyforge_project: wav-file
|
121
|
+
rubygems_version: 1.3.7
|
122
|
+
signing_key:
|
123
|
+
specification_version: 3
|
124
|
+
summary: Read wav file format and Edit data chunk
|
125
|
+
test_files: []
|
126
|
+
|