audio_hero 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a011c7136feae07c844a2a5e40d3c3ffe8e5bf01
4
+ data.tar.gz: 5b5d6d327129a36b52c3ed89dea2feb55d19e1da
5
+ SHA512:
6
+ metadata.gz: 9ba8a92edf5eef4c73e70736148842db4aa63fa2750cb934b2eff44b5b9f3ce994aa440b9811c41741d81ee575c58751ff54601bc9ff22b8a2a891e854ad4645
7
+ data.tar.gz: 6eb38e772b7b5573cc2fa2daf957d26155475d13c38640be957aca9d6802e65c9aab9152eb290cf5f91961a384fccf5172cd5b9d1b15286342664a70e711aa1d
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in audio_hero.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 litenup
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # AudioHero
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/audio_hero`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'audio_hero'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install audio_hero
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/audio_hero. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'audio_hero/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "audio_hero"
8
+ spec.version = AudioHero::VERSION
9
+ spec.authors = ["litenup"]
10
+ spec.email = ["alanhsu@litenvoice.com"]
11
+
12
+ spec.summary = %q{Wrapper for sox for audio processing}
13
+ spec.homepage = "http://rubygems.org/gems/audio_hero"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.10"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec"
32
+ spec.add_dependency("cocaine", "~> 0.5")
33
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "audio_hero"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,156 @@
1
+ require "audio_hero/version"
2
+ require 'cocaine'
3
+ require 'tempfile'
4
+
5
+ # test file: http://s3.amazonaws.com/recordings_2013/5204aebc-a9e9-11e5-baad-842b2b17453e.mp3
6
+ # Usage: file = open("url"); AudioHero::Sox.new(file);
7
+
8
+ module AudioHero
9
+ class AudioHeroError < StandardError
10
+ end
11
+ class Sox
12
+ attr_accessor :file, :params, :output_format
13
+
14
+ def initialize(file, options={})
15
+ @file = file
16
+ @basename = File.basename(@file.path)
17
+ end
18
+
19
+ # Usage: file = AudioHero::Sox.new(file).convert({output_options: "-c 1 -b 16 -r 16k", output_format: "mp3", channel: "left"}); file.close
20
+ def convert(options={})
21
+ channel = options[:channel]
22
+ input_format = options[:input_format] ? options[:input_format] : "mp3"
23
+ output_format = options[:output_format] ? options[:output_format] : "wav"
24
+ output_options = options[:default] ? "-c 1 -b 16 -r 16k" : options[:output_options]
25
+ case channel
26
+ when "left"
27
+ channel = "remix 1 0"
28
+ when "right"
29
+ channel = "remix 0 1"
30
+ else
31
+ channel = nil
32
+ end
33
+ src = @file
34
+ # Default conversion to wav
35
+ dst = Tempfile.new(["out", ".#{output_format}"])
36
+ begin
37
+ parameters = []
38
+ parameters << "-t #{input_format}"
39
+ parameters << ":source"
40
+ parameters << output_options if output_options
41
+ parameters << ":dest"
42
+ parameters << channel if channel
43
+ parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
44
+ success = Cocaine::CommandLine.new("sox", parameters).run(:source => File.expand_path(src.path), :dest => File.expand_path(dst.path))
45
+ rescue => e
46
+ raise AudioHeroError, "There was an error converting #{@basename} to #{output_format}"
47
+ end
48
+ dst
49
+ end
50
+
51
+ # Usage: file = AudioHero::Sox.new(file).remove_silence
52
+ def remove_silence(options={})
53
+ silence_duration = options[:silence_duration] || "0.1"
54
+ silence_level = options[:silence_level] || "0.03"
55
+ effect = "silence 1 #{silence_duration} #{silence_level}% -1 #{silence_duration} #{silence_level}%"
56
+ input_format = options[:input_format] ? options[:input_format] : "mp3"
57
+ output_format = options[:output_format] ? options[:output_format] : "wav"
58
+
59
+ # Default to wav
60
+ src = @file
61
+ # Default conversion to wav
62
+ dst = Tempfile.new(["out", ".#{output_format}"])
63
+ begin
64
+ parameters = []
65
+ parameters << "-t #{input_format}"
66
+ parameters << ":source"
67
+ parameters << ":dest"
68
+ parameters << effect
69
+ parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
70
+ success = Cocaine::CommandLine.new("sox", parameters).run(:source => File.expand_path(src.path), :dest => File.expand_path(dst.path))
71
+ rescue => e
72
+ raise AudioHeroError, "There was an error converting #{@basename} to #{output_format}"
73
+ end
74
+ dst
75
+ end
76
+
77
+ # Usage: AudioHero::Sox.new(file).split_by_silence({input_format: "wav"})
78
+ # Returns an array of the full path of the splitted files, can split two input files at one go using {file2: file} option.
79
+ # Remember its good practice to remove the temp directory after use.
80
+ # FileUtils.remove_entry tempdir
81
+ def split_by_silence(options={})
82
+ silence_duration = options[:silence_duration] || "0.5"
83
+ silence_level = options[:silence_level] || "0.03"
84
+ effect = "silence 1 #{silence_duration} #{silence_level}% 1 #{silence_duration} #{silence_level}% : newfile : restart"
85
+ input_format = options[:input_format] ? options[:input_format] : "mp3"
86
+ output_format = options[:output_format]
87
+ file2 = options[:file2]
88
+ # Default to wav
89
+ dir = Dir.mktmpdir
90
+ format = output_format ? ".#{output_format}" : ".wav"
91
+ dst = "#{dir}/out#{format}"
92
+
93
+ src = @file
94
+ src2 = file2
95
+
96
+ begin
97
+ parameters = []
98
+ parameters << "-t #{input_format}"
99
+ parameters << ":source"
100
+ parameters << ":dest"
101
+ parameters << effect
102
+ parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
103
+ success = Cocaine::CommandLine.new("sox", parameters).run(:source => File.expand_path(src.path), :dest => dst)
104
+ success = Cocaine::CommandLine.new("sox", parameters).run(:source => File.expand_path(src2.path), :dest => dst) if src2
105
+ rescue => e
106
+ raise AudioHeroError, "There was an error splitting #{@basename}"
107
+ end
108
+ Dir["#{dir}/**/*#{format}"]
109
+ end
110
+
111
+ def command(options={})
112
+ global = options[:global_options]
113
+ input_options = options[:input_options]
114
+ output_options = options[:output_options]
115
+ effect = options[:effect]
116
+ output_format = options[:output_format] ? options[:output_format] : "wav"
117
+ src = @file
118
+
119
+ # Default to wav
120
+ dst = Tempfile.new(["out", ".#{output_format}"])
121
+ begin
122
+ parameters = []
123
+ parameters << global if global
124
+ parameters << input_options if input_options
125
+ parameters << ":source"
126
+ parameters << output_options if output_options
127
+ parameters << ":dest"
128
+ parameters << effect if effect
129
+ parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
130
+ sox = Cocaine::CommandLine.new("sox", parameters)
131
+ command = sox.command(source: File.expand_path(src.path), dest: File.expand_path(dst.path))
132
+ success = sox.run(source: File.expand_path(src.path), dest: File.expand_path(dst.path))
133
+ rescue => e
134
+ raise AudioHeroError, "There was an error excuting command: #{command}"
135
+ end
136
+ dir
137
+ end
138
+
139
+ end
140
+ end
141
+
142
+
143
+ # Usage: file = AudioHero::Sox.new(file).convert({output_options: "-c 1 -b 16 -r 16k", output_format: "mp3", channel: "left"}); file.close
144
+ # sox required for AudioHero to work.
145
+ # '-c 1' specifies the output file to have 1 channel
146
+ # 'remix' selects and mixes input audio channels into output audio channels
147
+ # 1 0 means output 2 channels, channel one is a copy of original channel 1, while channel two is slient.
148
+ # left channel - sox input.mp3 -c 1 -b 16 -r 16k output.wav remix 1 0
149
+ # right channel - sox input.mp3 -c 1 -b 16 -r 16k output.wav remix 0 1
150
+
151
+ # split file by sentence while cutting silence: sox -V3 original.mp3 out2.wav silence 1 0.5 0.03% 1 0.5 0.03% : newfile : restart
152
+ # Cut out silence without splitting: sox -V3 original.mp3 out2.wav silence 1 0.5 0.03% -1 0.5 0.03%
153
+ # all following read a file
154
+ # open("url")
155
+ # File.open("filepath", "rb")
156
+ # open("file")
@@ -0,0 +1,3 @@
1
+ module AudioHero
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: audio_hero
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - litenup
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: cocaine
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.5'
69
+ description:
70
+ email:
71
+ - alanhsu@litenvoice.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - audio_hero.gemspec
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/audio_hero.rb
88
+ - lib/audio_hero/version.rb
89
+ homepage: http://rubygems.org/gems/audio_hero
90
+ licenses:
91
+ - MIT
92
+ metadata:
93
+ allowed_push_host: https://rubygems.org
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.5.0
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Wrapper for sox for audio processing
114
+ test_files: []