Class: DTAS::Format
Overview
Constant Summary
collapse
- NATIVE_ENDIAN =
[1].pack("l") == [1].pack("l>") ? "big" : "little"
- FORMAT_DEFAULTS =
{
"type" => "s32",
"channels" => 2,
"rate" => 44100,
"bits" => nil,
"endian" => nil,
}
- SIVS =
FORMAT_DEFAULTS.keys
Constants included
from Process
Process::PIDS
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Serialize
#ivars_to_hash
Methods included from Process
#dtas_spawn, #qx, reaper, #xs
Constructor Details
#initialize ⇒ Format
Returns a new instance of Format.
38
39
40
41
42
|
# File 'lib/dtas/format.rb', line 38
def initialize
FORMAT_DEFAULTS.each do |k,v|
instance_variable_set("@#{k}", v)
end
end
|
Instance Attribute Details
#bits ⇒ Object
only set for playback on 16-bit DACs
17
18
19
|
# File 'lib/dtas/format.rb', line 17
def bits
@bits
end
|
#channels ⇒ Object
15
16
17
|
# File 'lib/dtas/format.rb', line 15
def channels
@channels
end
|
#endian ⇒ Object
Returns the value of attribute endian.
18
19
20
|
# File 'lib/dtas/format.rb', line 18
def endian
@endian
end
|
#rate ⇒ Object
44100, 48000, 88200, 96000, 176400, 192000 …
16
17
18
|
# File 'lib/dtas/format.rb', line 16
def rate
@rate
end
|
#type ⇒ Object
s32, f32, f64 … any point in others?
14
15
16
|
# File 'lib/dtas/format.rb', line 14
def type
@type
end
|
Class Method Details
.load(hash) ⇒ Object
29
30
31
32
33
34
35
36
|
# File 'lib/dtas/format.rb', line 29
def self.load(hash)
fmt = new
return fmt unless hash
(SIVS & hash.keys).each do |k|
fmt.instance_variable_set("@#{k}", hash[k])
end
fmt
end
|
Instance Method Details
#bits_per_sample ⇒ Object
86
87
88
89
90
91
|
# File 'lib/dtas/format.rb', line 86
def bits_per_sample
return @bits if @bits
/\A[fst](8|16|24|32|64)\z/ =~ @type or
raise TypeError, "invalid type=#@type (must be s32/f32/f64)"
$1.to_i
end
|
#bytes_per_sample ⇒ Object
93
94
95
|
# File 'lib/dtas/format.rb', line 93
def bytes_per_sample
bits_per_sample / 8
end
|
#bytes_to_samples(bytes) ⇒ Object
114
115
116
|
# File 'lib/dtas/format.rb', line 114
def bytes_to_samples(bytes)
bytes / bytes_per_sample / @channels
end
|
#bytes_to_time(bytes) ⇒ Object
118
119
120
|
# File 'lib/dtas/format.rb', line 118
def bytes_to_time(bytes)
Time.at(bytes_to_samples(bytes) / @rate.to_f)
end
|
#endian2 ⇒ Object
returns ‘be’ or ‘le’ depending on endianess
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/dtas/format.rb', line 51
def endian2
case e = @endian || NATIVE_ENDIAN
when "big"
"be"
when "little"
"le"
else
raise"unsupported endian=#{e}"
end
end
|
#from_file(path) ⇒ Object
78
79
80
81
82
83
|
# File 'lib/dtas/format.rb', line 78
def from_file(path)
@channels = qx(%W(soxi -c #{path})).to_i
@type = qx(%W(soxi -t #{path})).strip
@rate = qx(%W(soxi -r #{path})).to_i
end
|
#hhmmss_to_samples(hhmmss) ⇒ Object
HH:MM:SS.frac (don’t bother with more complex times, too much code) part of me wants to drop this feature from playq, feels like bloat…
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/dtas/format.rb', line 132
def hhmmss_to_samples(hhmmss)
time = hhmmss.dup
rv = 0
if time.sub!(/\.(\d+)\z/, "")
rv = ("0.#$1".to_f * @rate).to_i
end
t = time.split(/:/)
raise ArgumentError, "Bad time format: #{hhmmss}" if t.size > 3
mult = 1
while part = t.pop
rv += part.to_i * mult * @rate
mult *= 60
end
rv
end
|
#inspect ⇒ Object
66
67
68
|
# File 'lib/dtas/format.rb', line 66
def inspect
"<#{self.class}(#{xs(to_sox_arg)})>"
end
|
#to_eca_arg ⇒ Object
62
63
64
|
# File 'lib/dtas/format.rb', line 62
def to_eca_arg
%W(-f #{@type}_#{endian2},#@channels,#@rate)
end
|
#to_env ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/dtas/format.rb', line 97
def to_env
rv = {
"SOX_FILETYPE" => @type,
"CHANNELS" => @channels.to_s,
"RATE" => @rate.to_s,
"ENDIAN" => @endian || NATIVE_ENDIAN,
"SOXFMT" => to_sox_arg.join(' '),
"ECAFMT" => to_eca_arg.join(' '),
"ENDIAN2" => endian2,
}
begin
rv["BITS_PER_SAMPLE"] = bits_per_sample.to_s
rescue TypeError
end
rv
end
|
#to_hash ⇒ Object
74
75
76
|
# File 'lib/dtas/format.rb', line 74
def to_hash
ivars_to_hash(SIVS)
end
|
#to_hsh ⇒ Object
70
71
72
|
# File 'lib/dtas/format.rb', line 70
def to_hsh
to_hash.delete_if { |k,v| v == FORMAT_DEFAULTS[k] }
end
|
#to_sox_arg ⇒ Object
44
45
46
47
48
|
# File 'lib/dtas/format.rb', line 44
def to_sox_arg
rv = %W(-t#@type -c#@channels -r#@rate)
rv.concat(%W(-b#@bits)) if @bits
rv
end
|
#valid_endian?(endian) ⇒ Boolean
126
127
128
|
# File 'lib/dtas/format.rb', line 126
def valid_endian?(endian)
!!(endian =~ %r{\A(?:big|little|swap)\z})
end
|
#valid_type?(type) ⇒ Boolean
122
123
124
|
# File 'lib/dtas/format.rb', line 122
def valid_type?(type)
!!(type =~ %r{\A[us](?:8|16|24|32)\z} || type =~ %r{\Af?:(32|64)})
end
|