Skip to content

Commit

Permalink
- put audio processor under BigBlueButton module
Browse files Browse the repository at this point in the history
  • Loading branch information
ritzalam committed May 9, 2011
1 parent bd1b93d commit 7cc30ca
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 44 deletions.
16 changes: 4 additions & 12 deletions record-and-playback/rap/lib/recordandplayback/generators/audio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'nokogiri'
require 'builder'

module Generator
module BigBlueButton
class AudioEvents
# Generate a silent wav file.
#
Expand All @@ -24,10 +24,7 @@ def self.generate_silence(millis, filename, sampling_rate)

f.close();
command = "sox #{temp_file} -b 16 -r #{sampling_rate} -c 1 -s #{filename}"
BigBlueButton.logger.info("Executing #{command}\n")
proc = IO.popen(command, "w+")
# Wait for the process to finish before removing the temp file
Process.wait()
BigBlueButton.execute(command)
# Delete the temporary raw audio file
FileUtils.rm(temp_file)
end
Expand All @@ -39,10 +36,7 @@ def self.generate_silence(millis, filename, sampling_rate)
def self.concatenate_audio_files(files, outfile)
file_list = files.join(' ')
command = "sox #{file_list} #{outfile}"
BigBlueButton.logger.info("Executing #{command}\n")
proc = IO.popen(command, "w+")
# Wait for the process to finish
Process.wait()
BigBlueButton.execute(command)
end

# Convert a wav file to an ogg file
Expand All @@ -51,9 +45,7 @@ def self.concatenate_audio_files(files, outfile)
# ogg_file - resulting ogg file
def self.wav_to_ogg(wav_file, ogg_file)
command = "oggenc -Q -o #{ogg_file} #{wav_file} 2>&1"
BigBlueButton::Archive.logger.info("Executing #{command}\n")
proc = IO.popen(command, "w+")
Process.wait()
BigBlueButton.execute(command)
end

# Extracts the length of the audio file as reurned by running
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

module BigBlueButton
class AudioProcessor
def self.process(archive_dir)
# Process the raw recorded audio to ogg file.
# archive_dir - directory location of the raw archives. Assumes there is audio file and events.xml present.
# ogg_file - the file name of the ogg audio output
#
def self.process(archive_dir, ogg_file)
audio_dir = "#{archive_dir}/audio"
events_xml = "#{archive_dir}/events.xml"
audio_events = Generator::AudioEvents.process_events(events_xml)
audio_events = BigBlueButton::AudioEvents.process_events(events_xml)
audio_files = []
audio_events.each do |ae|
if ae.padding
ae.file = "#{audio_dir}/#{ae.length_of_gap}.wav"
Generator::AudioEvents.generate_silence(ae.length_of_gap, ae.file, 16000)
BigBlueButton::AudioEvents.generate_silence(ae.length_of_gap, ae.file, 16000)
else
# Substitute the original file location with the archive location
ae.file = ae.file.sub(/.+\//, "#{audio_dir}/")
Expand All @@ -20,9 +24,8 @@ def self.process(archive_dir)
end

wav_file = "#{audio_dir}/recording.wav"
ogg_file = "#{audio_dir}/recording.ogg"
Generator::AudioEvents.concatenate_audio_files(audio_files, wav_file)
Generator::AudioEvents.wav_to_ogg(wav_file, ogg_file)
BigBlueButton::AudioEvents.concatenate_audio_files(audio_files, wav_file)
BigBlueButton::AudioEvents.wav_to_ogg(wav_file, ogg_file)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require 'spec_helper'
require 'digest/md5'

module Generator
module BigBlueButton
describe AudioEvents do
context "#success" do
it "should generate a complete audio file for the recording" do
dir = "resources/raw/1b199e88-7df7-4842-a5f1-0e84b781c5c8"
events_xml = "#{dir}/events.xml"
audio_dir = "#{dir}/audio"
audio_events = Generator::AudioEvents.process_events(events_xml)
audio_events = BigBlueButton::AudioEvents.process_events(events_xml)
audio_events.each do |ae|
puts ae.file
end
Expand All @@ -17,25 +17,28 @@ module Generator
it "should create a silence file" do
length = 2000
file = "/tmp/silence-audio.wav"
Generator::AudioEvents.generate_silence(length, file, 16000)
Generator::AudioEvents.determine_length_of_audio_from_file(file).should equal(length)
BigBlueButton::AudioEvents.generate_silence(length, file, 16000)
BigBlueButton::AudioEvents.determine_length_of_audio_from_file(file).should equal(length)
FileUtils.rm(file)
end

it "should concatenate files" do
dir = "resources/raw/1b199e88-7df7-4842-a5f1-0e84b781c5c8/audio"
file1 = "#{dir}/1b199e88-7df7-4842-a5f1-0e84b781c5c8-20110202-041247.wav"
file2 = "#{dir}/1b199e88-7df7-4842-a5f1-0e84b781c5c8-20110202-041415.wav"
outfile = "/tmp/concatenated.wav"
Generator::AudioEvents.concatenate_audio_files([file1, file2], outfile)
Generator::AudioEvents.determine_length_of_audio_from_file(outfile).should equal(115580)
BigBlueButton::AudioEvents.concatenate_audio_files([file1, file2], outfile)
BigBlueButton::AudioEvents.determine_length_of_audio_from_file(outfile).should equal(115580)
FileUtils.rm(outfile)
end

it "should convert wav file to ogg" do
dir = "resources/raw/1b199e88-7df7-4842-a5f1-0e84b781c5c8/audio"
file1 = "#{dir}/1b199e88-7df7-4842-a5f1-0e84b781c5c8-20110202-041247.wav"
outfile = "/tmp/wav2ogg.ogg"
Generator::AudioEvents.wav_to_ogg(file1, outfile)
Generator::AudioEvents.determine_length_of_audio_from_file(outfile).should equal(60020)
BigBlueButton::AudioEvents.wav_to_ogg(file1, outfile)
BigBlueButton::AudioEvents.determine_length_of_audio_from_file(outfile).should equal(60020)
FileUtils.rm(outfile)
end

it "should find the timestamp of the first event" do
Expand All @@ -50,68 +53,68 @@ module Generator

it "should get all start audio recording events" do
events_xml = 'resources/raw/good_audio_events.xml'
start = Generator::AudioEvents.start_audio_recording_events events_xml
start = BigBlueButton::AudioEvents.start_audio_recording_events events_xml
start.length.should == 2
start[0].start_event_timestamp.should == "100"
start[1].start_event_timestamp.should == "500"
end

it "should get all stop audio recording events" do
events_xml = 'resources/raw/good_audio_events.xml'
stop = Generator::AudioEvents.stop_audio_recording_events events_xml
stop = BigBlueButton::AudioEvents.stop_audio_recording_events events_xml
stop.length.should == 2
stop[0].stop_event_timestamp.should == "350"
stop[1].stop_event_timestamp.should == "800"
end

it "should match all start and stop events" do
events_xml = 'resources/raw/good_audio_events.xml'
se = Generator::AudioEvents.match_start_and_stop_events(Generator::AudioEvents.start_audio_recording_events(events_xml),
Generator::AudioEvents.stop_audio_recording_events(events_xml))
se = BigBlueButton::AudioEvents.match_start_and_stop_events(BigBlueButton::AudioEvents.start_audio_recording_events(events_xml),
BigBlueButton::AudioEvents.stop_audio_recording_events(events_xml))
se.length.should == 2
end

it "should not match all start and stop events" do
events_xml = 'resources/raw/unmatched_audio_events.xml'
se = Generator::AudioEvents.match_start_and_stop_events(Generator::AudioEvents.start_audio_recording_events(events_xml),
Generator::AudioEvents.stop_audio_recording_events(events_xml))
se = BigBlueButton::AudioEvents.match_start_and_stop_events(BigBlueButton::AudioEvents.start_audio_recording_events(events_xml),
BigBlueButton::AudioEvents.stop_audio_recording_events(events_xml))
se.length.should == 4
end

it "should generate audio pads" do
events_xml = 'resources/raw/good_audio_events.xml'
se = Generator::AudioEvents.match_start_and_stop_events(Generator::AudioEvents.start_audio_recording_events(events_xml),
Generator::AudioEvents.stop_audio_recording_events(events_xml))
se = BigBlueButton::AudioEvents.match_start_and_stop_events(BigBlueButton::AudioEvents.start_audio_recording_events(events_xml),
BigBlueButton::AudioEvents.stop_audio_recording_events(events_xml))
se.length.should == 2
audio_paddings = Generator::AudioEvents.generate_audio_paddings(se, events_xml)
audio_paddings = BigBlueButton::AudioEvents.generate_audio_paddings(se, events_xml)
audio_paddings.length.should == 3
end

it "should generate sorted audio events" do
Generator::AudioEvents.stub(:determine_if_recording_file_exist).and_return(true)
Generator::AudioEvents.stub(:determine_length_of_audio_from_file).and_return(50)
BigBlueButton::AudioEvents.stub(:determine_if_recording_file_exist).and_return(true)
BigBlueButton::AudioEvents.stub(:determine_length_of_audio_from_file).and_return(50)
events_xml = 'resources/raw/good_audio_events.xml'
audio_events = Generator::AudioEvents.process_events(events_xml)
audio_events = BigBlueButton::AudioEvents.process_events(events_xml)
audio_events.length.should == 5
i = 0
while i < audio_events.length - 1
(audio_events[i+1].start_event_timestamp.to_i > audio_events[i].stop_event_timestamp.to_i).should be_true
i += 1
end

# Generator::AudioEvents.to_xml_file(audio_events, "/tmp/event.audio.xml")
# BigBlueButton::AudioEvents.to_xml_file(audio_events, "/tmp/event.audio.xml")
end

it "should determine the start/stop timestamps for non-matched events" do
Generator::AudioEvents.stub(:determine_if_recording_file_exist).and_return(true)
Generator::AudioEvents.stub(:determine_length_of_audio_from_file).and_return(50)
BigBlueButton::AudioEvents.stub(:determine_if_recording_file_exist).and_return(true)
BigBlueButton::AudioEvents.stub(:determine_length_of_audio_from_file).and_return(50)
events_xml = 'resources/raw/unmatched_audio_events.xml'
se = Generator::AudioEvents.match_start_and_stop_events(Generator::AudioEvents.start_audio_recording_events(events_xml),
Generator::AudioEvents.stop_audio_recording_events(events_xml))
se = BigBlueButton::AudioEvents.match_start_and_stop_events(BigBlueButton::AudioEvents.start_audio_recording_events(events_xml),
BigBlueButton::AudioEvents.stop_audio_recording_events(events_xml))
se.length.should == 4
se.each do |e|
if not e.matched
Generator::AudioEvents.determine_start_stop_timestamps_for_unmatched_event!(e)
BigBlueButton::AudioEvents.determine_start_stop_timestamps_for_unmatched_event!(e)
e.stop_event_timestamp.to_i.should == e.start_event_timestamp.to_i + 50
end
end
Expand Down

0 comments on commit 7cc30ca

Please sign in to comment.