-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.rb
More file actions
57 lines (49 loc) · 1.65 KB
/
main.rb
File metadata and controls
57 lines (49 loc) · 1.65 KB
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
require 'aimastering'
require 'optparse'
require 'securerandom'
# Parse command line arguments
options = {}
OptionParser.new do |opt|
opt.on('-i', '--input VAL', 'Input audio file path') { |v| options[:input] = v }
opt.on('-o', '--output VAL', 'Output audio file path') { |v| options[:output] = v }
opt.parse!(ARGV)
end
# Setup authorization
Aimastering.configure do |config|
# Configure API key authorization: bearer
# config.api_key['Authorization'] = ENV['AIMASTERING_ACCESS_TOKEN']
# API key must be 'guest_' + [arbitrary string]
# Unless the API key is leaked, the data will not be visible to others.
config.api_key['Authorization'] = 'guest_' + SecureRandom.base64(8)
end
audio_api = Aimastering::AudioApi.new
mastering_api = Aimastering::MasteringApi.new
begin
# Upload audio
input_audio = File.open(options[:input]) do |input_file|
audio_api.create_audio(
file: input_file
)
end
warn 'Input audio created'
warn input_audio.to_s
# Start the mastering
mastering = mastering_api.create_mastering(
input_audio.id,
mode: 'default'
)
warn 'Mastering created'
warn mastering.to_s
# Wait for the mastering completion
while mastering.status == 'processing' || mastering.status == 'waiting'
mastering = mastering_api.get_mastering(mastering.id)
warn("waiting for the mastering completion #{(100 * mastering.progression).round}%")
sleep(5)
end
# Download output audio
output_audio_data = audio_api.download_audio(mastering.output_audio_id)
File.binwrite(options[:output], output_audio_data)
warn "Output audio save to #{options[:output]}"
rescue Aimastering::ApiError => e
warn "Api error: #{e}"
end