Skip to content

Commit

Permalink
working on media controller and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cofiem committed Nov 2, 2012
1 parent ef59459 commit f396088
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 89 deletions.
154 changes: 79 additions & 75 deletions app/controllers/media_controller.rb
Original file line number Diff line number Diff line change
@@ -1,100 +1,104 @@
class MediaController < ApplicationController
include Cache, Spectrogram, Audio

respond_to :xml, :json, :html, :png
respond_to :xml, :json, :html, :png, :ogg, :oga, :webm, :webma, :mp3

def index
# index page for media files
@testing = QubarSite::Application.config.media_file_config
#@testing = QubarSite::Application.config.media_file_config
end

def item
# page for an individual media file

@requested_format1 = request.format
@requested_format2 = params[:format]

respond_to do |format|
format.html { render 'item.html.erb'}
format.json { render 'item.html.erb' }
format.png { render 'item.html.erb' }
format.mp3 { render 'item.html.erb' }
format.oga { render 'item.html.erb' }
format.ogg { render 'item.html.erb' }
end


=begin
# determine the request format
if request.format == :html
elsif %w(png jpg).include? request.format
file_name = Cache::cached_spectrogram_file(params)
file_paths = Cache::existing_paths(Cache::cached_spectrogram_storage_paths,file_name)
elsif %w(ogg oga webm webma mp3).include? request.format
file_name = Cache::cached_audio_file(params)
file_paths = Cache::existing_paths(Cache::cached_audio_storage_paths,file_name)
else
end
# use the
@requested_format = request.format
return render
# use params to get query string or parsed route parameters

@avail_params = params

# see if a file extension was specified.
# if none was specified, default to html
if !params.include? 'format'
#@avail_params = params
if params.include? :format
else
params[:format] = 'html'
end

if params[:format] == 'png' # if the extension is png, it is a spectrogram request
# if an image format was specified, get spectrogram information, otherwise audio
if %w(png jpg).include? params[:format] # if the extension is png or jpg, it is a spectrogram request
file_name = Cache::cached_spectrogram_file(params)
file_paths = Cache::possible_paths(Cache::cached_spectrogram_storage_paths,file_name)

elsif ['mp3', 'webm', 'webma', 'ogg', 'oga'].include? params[:format] # if the extension is mp3, webma, ogg, it is an audio request
file_name = Cache::cached_audio_file(params)
file_paths = Cache::possible_paths(Cache::cached_audio_storage_paths,file_name)
file_paths = Cache::existing_paths(Cache::cached_spectrogram_storage_paths,file_name)
else
# invalid request, what to do?
file_name = Cache::cached_audio_file(params)
file_paths = Cache::existing_paths(Cache::cached_audio_storage_paths,file_name)
end

@file_paths = file_paths

mimeType = Mime::Type.lookup_by_extension(params[:format])
#=begin
# construct a hash of information to be returned
@file_info = params
@file_info.delete 'controller'
@file_info.delete 'action'
@file_info.delete 'format'
if file_paths.length > 0 && %w(html htm js json).include?(params[:format])
@file_info[:information] = Audio::info file_paths.first
end
mime_type = Mime::Type.lookup_by_extension(params[:format])
# respond to the request
respond_with do |format|
format.html { render }
format.htm { render 'index.html.erb' }
format.json { render json: params }
format.js { render json: params }
format.mp3 { send_file_response @file_paths, mimeType }
format.webma { send_file_response @file_paths, mimeType }
format.webm { send_file_response @file_paths, mimeType }
format.oga { send_file_response @file_paths, mimeType }
format.ogg { send_file_response @file_paths, mimeType }
format.png { send_file_response @file_paths, mimeType }
format.html {
render
}
format.htm {
render 'index.html.erb'
}
format.json { render json: @file_info }
format.js { render json: @file_info }
format.mp3 { send_file_response file_paths, file_name, mime_type }
format.webma { send_file_response file_paths, file_name, mime_type }
format.webm { send_file_response file_paths, file_name, mime_type }
format.oga { send_file_response file_paths, file_name, mime_type }
format.ogg { send_file_response file_paths, file_name, mime_type }
format.png { send_file_response file_paths, file_name, mime_type }
end
#=end
#render :formats => [:html]
=end
end

private

def send_file_response (file_paths, mime_type)
send_file file_paths[0], :stream => true, :buffer_size => 4096, :disposition => 'inline', :type => mime_type
end

def info
@input_path = './test/fixtures/'
# not-an-audio-file.wav
# TorresianCrow.wav
# TestAudio1.wv
# sites.yml
# this file does not exist.nope
@audio = 'TestAudio1.wv'
@input_audio = @input_path + @audio
@result = Audio::info(@input_audio)
end
def audio
print params

@input_path = './test/fixtures/'
@output_path = './public/tests/'

@audio = 'TestAudio1.wv'
@modified_audio = 'TestAudio1.wav'

@input_audio = @input_path + @audio
@output_audio = @output_path + @modified_audio


#@result = Audio::modify(@input_audio, @output_audio, [])
end
def spectrogram
print params
@input_path = './test/fixtures/'
@output_path = './public/tests/'

@audio = 'TorresianCrow.wav'
@image = 'TorresianCrow.png'

@input_audio = @input_path + @audio
@output_image = @output_path + @image

#@result = Spectrogram::generate(@input_audio, @output_image)
def send_file_response (file_path, file_name, mime_type)
#raise RuntimeError, "Can't find a source for file: #{file_name}" unless file_paths.length > 0
send_file file_path, :stream => true, :buffer_size => 4096, :disposition => 'inline', :type => mime_type, :content_type => mime_type
end
end
3 changes: 1 addition & 2 deletions app/views/media/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<h1>Media#index</h1>
<p>Index page for media controller.</p>
<p><%= @testing %></p>
<p>Index page for media controller.</p>
6 changes: 4 additions & 2 deletions app/views/media/item.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<h1>Media#item</h1>
<p>Individual item page for media controller.</p>
<p><%= @avail_params %></p>
<p><%= @file_paths %></p>
<h2>File Info</h2>
<%= @requested_format1 %>
<%= @requested_format2 %>
<%= @file_info %>
2 changes: 0 additions & 2 deletions lib/modules/audio.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'audio_sox'

module Audio
include AudioSox, AudioMp3splt, AudioWavpack, AudioFfmpeg

Expand Down
2 changes: 0 additions & 2 deletions lib/modules/audio_sox.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'OS'

module AudioSox
include OS
@sox_path = if OS.windows? then "./vendor/bin/sox/windows/sox.exe" else "sox" end
Expand Down
8 changes: 3 additions & 5 deletions lib/modules/cache.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'String'

# Determines file names for cached and original files.
module Cache

Expand Down Expand Up @@ -104,9 +102,9 @@ def self.get_parameter(parameter, modify_parameters, include_separator = true)
if modify_parameters.include? parameter
result_name = modify_parameters[parameter].to_s

if parameter == :format
result_name = result_name.trim '.', ''
end
#if parameter == :format
#result_name = result_name.trim '.', ''
#end

if include_separator
result_name = @parameter_file_name_separator+result_name
Expand Down
10 changes: 9 additions & 1 deletion lib/modules/spectrogram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ module Spectrogram
@sox_arguments_sample_rate = "rate 22050"
@sox_arguments_spectrogram = "spectrogram -m -r -l -a -q 249 -w hann -y 257 -X 43.06640625 -z 100"
@sox_arguments_output = "-o"


def self.colour_options()
{ :g => :greyscale }
end

def self.window_options()
[ 128, 256, 512, 1024, 2048, 4096 ]
end

# Generate a spectrogram image from an audio file.
# The spectrogram will be 257 pixels high, but the length is not known exactly beforehand.
# The spectrogram will be created for the entire file. Durations longer than 2 minutes are not recommended.
Expand Down

0 comments on commit f396088

Please sign in to comment.