From f396088b6e0de42a68714516aaa7d767045ca6ce Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 2 Nov 2012 16:49:54 +1000 Subject: [PATCH] working on media controller and tests --- app/controllers/media_controller.rb | 154 ++++++++++++++-------------- app/views/media/index.html.erb | 3 +- app/views/media/item.html.erb | 6 +- lib/modules/audio.rb | 2 - lib/modules/audio_sox.rb | 2 - lib/modules/cache.rb | 8 +- lib/modules/spectrogram.rb | 10 +- 7 files changed, 96 insertions(+), 89 deletions(-) diff --git a/app/controllers/media_controller.rb b/app/controllers/media_controller.rb index b42796a5..7c0c918e 100644 --- a/app/controllers/media_controller.rb +++ b/app/controllers/media_controller.rb @@ -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 \ No newline at end of file diff --git a/app/views/media/index.html.erb b/app/views/media/index.html.erb index 4b560577..461a9934 100644 --- a/app/views/media/index.html.erb +++ b/app/views/media/index.html.erb @@ -1,3 +1,2 @@

Media#index

-

Index page for media controller.

-

<%= @testing %>

\ No newline at end of file +

Index page for media controller.

\ No newline at end of file diff --git a/app/views/media/item.html.erb b/app/views/media/item.html.erb index 5a4ede73..487bcf3f 100644 --- a/app/views/media/item.html.erb +++ b/app/views/media/item.html.erb @@ -1,4 +1,6 @@

Media#item

Individual item page for media controller.

-

<%= @avail_params %>

-

<%= @file_paths %>

\ No newline at end of file +

File Info

+<%= @requested_format1 %> +<%= @requested_format2 %> +<%= @file_info %> \ No newline at end of file diff --git a/lib/modules/audio.rb b/lib/modules/audio.rb index 2ece35d6..1f818b79 100644 --- a/lib/modules/audio.rb +++ b/lib/modules/audio.rb @@ -1,5 +1,3 @@ -require 'audio_sox' - module Audio include AudioSox, AudioMp3splt, AudioWavpack, AudioFfmpeg diff --git a/lib/modules/audio_sox.rb b/lib/modules/audio_sox.rb index a91861eb..3fd310f0 100644 --- a/lib/modules/audio_sox.rb +++ b/lib/modules/audio_sox.rb @@ -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 diff --git a/lib/modules/cache.rb b/lib/modules/cache.rb index 0571fda5..efa3ce31 100644 --- a/lib/modules/cache.rb +++ b/lib/modules/cache.rb @@ -1,5 +1,3 @@ -require 'String' - # Determines file names for cached and original files. module Cache @@ -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 diff --git a/lib/modules/spectrogram.rb b/lib/modules/spectrogram.rb index 4442b0e2..b6307fd5 100644 --- a/lib/modules/spectrogram.rb +++ b/lib/modules/spectrogram.rb @@ -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.