- Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathsample_encoder.rb
executable file
·91 lines (72 loc) · 1.75 KB
/
sample_encoder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/ruby
require'wavefile'
classSampleEncoder
attr_accessor:filename
definitialize(params)
self.filename=params[0]
end
defusage
puts<<-EOS
Usage:
#{$0} <filename>
Where <filename> is a path to a valid WAV format file
EOS
end
defgenerate!
ifreader
print_encoded_structure
else
usage
end
rescue=>e
loge.message
usage
end
defreader
@reader ||= WaveFile::Reader.new(filename)
end
deftarget_format
@target_format ||= WaveFile::Format.new(:mono,:pcm_8,8000)
end
defplaying_time(duration)
duration.hours.to_s.rjust(2,"0") << ":" <<
duration.minutes.to_s.rjust(2,"0") << ":" <<
duration.seconds.to_s.rjust(2,"0") << "." <<
duration.milliseconds.to_s.rjust(3,"0")
end
defprint_header
puts<<-EOS
#pragma once
/*
SAMPLE DATA
generated by : sample_generator.rb
Source file : #{filename}
Format : #{reader.native_format.audio_format}
Channels : #{reader.native_format.channels}
Bits/sample : #{reader.native_format.bits_per_sample}
Sample rate : #{reader.native_format.sample_rate}
Byte rate : #{reader.native_format.byte_rate}
Frame count : #{reader.total_sample_frames}
Playing time : #{playing_time(reader.total_duration)}
*/
EOS
end
defprint_encoded_structure
print_header
print_samples
end
defprint_samples
puts"const unsigned char sample[] PROGMEM = {"
reader.each_bufferdo |buffer|
converted_buffer=buffer.convert(target_format)
converted_buffer.samples.eachdo |value|
puts" #{sprintf("0x%X",value)}, // #{value}"
end
end
puts"};"
end
deflog(message)
STDERR.putsmessage
end
end
SampleEncoder.new(ARGV).generate!