Permalink
Browse files

Revised video method with javax.sound.sampled.Clip interface. Impleme…

…nted Video#play, start, stop, length, time, time=, position, position= and playing? methods. Added new sample58.
  • Loading branch information...
1 parent 422e74b commit dd082ba5eb1ad41ee86453d1d9b85cf425422a36 @ashbb committed May 14, 2012
Showing with 78 additions and 57 deletions.
  1. +1 −1 VERSION
  2. +49 −54 lib/plugins/video.rb
  3. +4 −2 purple_shoes.gemspec
  4. +24 −0 samples/sample58.rb
  5. BIN snapshots/sample58.png
View
@@ -1 +1 @@
-0.6.163
+0.6.164
View
@@ -1,5 +1,5 @@
# Original code was written by pjfitzgibbons (Peter Fitzgibbons) in Brown Shoes
-# Edited a little bit for Purple Shoes by ashbb
+# Revised for Purple Shoes by ashbb
require File.join(DIR, '../static/sound_jars/tritonus_share.jar')
require File.join(DIR, '../static/sound_jars/mp3spi1.9.5.jar')
@@ -15,74 +15,69 @@ class Video
import javax.sound.sampled
import java.io.IOException
- BufferSize = 4096
-
- def initialize args
- @initials = args
- args.each do |k, v|
- instance_variable_set "@#{k}", v
- end
- Video.class_eval do
- attr_accessor *args.keys
+ def initialize app, path
+ if path =~ /^(http|https):\/\//
+ file = File.basename path
+ app.download(path, save: file){init file}
+ else
+ init path
end
end
- def play
- Thread.new do
- audio_input_stream = AudioSystem.getAudioInputStream JFile.new(@path)
- audio_format = audio_input_stream.getFormat
- rawplay *decode_input_stream(audio_format, audio_input_stream)
- audio_input_stream.close
- end
+ def init file
+ audio = AudioSystem.getAudioInputStream JFile.new file
+ format = audio.getFormat
+ af = AudioFormat.new AudioFormat::Encoding::PCM_SIGNED, format.getSampleRate, 16,
+ format.getChannels, format.getChannels * 2, format.getSampleRate, false
+ as = AudioSystem.getAudioInputStream af, audio
+ @line = AudioSystem.getLine DataLine::Info.new(Clip.java_class, af)
+ @line.open as
end
- def decode_input_stream audio_format, audio_input_stream
- case audio_format.encoding
- when Java::JavazoomSpiVorbisSampledFile::VorbisEncoding, Java::JavazoomSpiMpegSampledFile::MpegEncoding
- decoded_format = AudioFormat.new(AudioFormat::Encoding::PCM_SIGNED, audio_format.getSampleRate(), 16,
- audio_format.getChannels(), audio_format.getChannels() * 2, audio_format.getSampleRate(), false)
- decoded_audio_input_stream = AudioSystem.getAudioInputStream(decoded_format, audio_input_stream)
- return decoded_format, decoded_audio_input_stream
- else
- return audio_format, audio_input_stream
- end
+ def play
+ self.time = 0
+ start
end
- def rawplay(decoded_audio_format, decoded_audio_input_stream)
- sampled_data = Java::byte[BufferSize].new
- line = getLine(decoded_audio_format)
- if line != nil
- line.start()
- bytes_read = 0, bytes_written = 0
- while bytes_read != -1
- bytes_read = decoded_audio_input_stream.read(sampled_data, 0, sampled_data.length)
- if bytes_read != -1
- bytes_written = line.write(sampled_data, 0, bytes_read)
- end
- end
- line.drain()
- line.stop()
- line.close()
- decoded_audio_input_stream.close()
- end
+ def stop
+ @line.stop if @line
end
+ alias :pause :stop
- def getLine(audioFormat)
- res = nil
- info = DataLine::Info.new(SourceDataLine.java_class, audioFormat)
- res = AudioSystem.getLine(info)
- res.open(audioFormat)
- res
+ def start
+ @line.start if @line
+ end
+
+ def length
+ @line.getMicrosecondLength / 1000 if @line
+ end
+
+ def time
+ @line.getMicrosecondPosition / 1000 if @line
+ end
+
+ def time=(n)
+ @line.setMicrosecondPosition n * 1000 if @line
+ end
+
+ def position
+ time / length.to_f if @line
+ end
+
+ def position=(f)
+ self.time = length * f if @line
+ end
+
+ def playing?
+ @line.isRunning if @line
end
end
end
class Shoes
class App
- def video file
- args = {}
- args[:real], args[:app], args[:path] = nil, self, file
- Video.new args
+ def video path
+ Video.new self, path
end
end
end
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = "purple_shoes"
- s.version = "0.6.162"
+ s.version = "0.6.164"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["ashbb"]
- s.date = "2012-05-10"
+ s.date = "2012-05-14"
s.description = "Purple Shoes is one of colorful Shoes, written in JRuby and SWT."
s.email = "ashbbb@gmail.com"
s.executables = ["pshoes"]
@@ -146,6 +146,7 @@ Gem::Specification.new do |s|
"samples/sample50.rb",
"samples/sample51.rb",
"samples/sample52.rb",
+ "samples/sample58-1.rb",
"samples/sample58.rb",
"samples/sample6.rb",
"samples/sample7.rb",
@@ -203,6 +204,7 @@ Gem::Specification.new do |s|
"snapshots/sample50.png",
"snapshots/sample51.png",
"snapshots/sample52.png",
+ "snapshots/sample58-1.png",
"snapshots/sample58.png",
"snapshots/sample6.png",
"snapshots/sample7.png",
View
@@ -0,0 +1,24 @@
+require 'purple_shoes'
+
+Shoes.app width: 300, height: 100, title: 'Teeny-weeny Audio player' do
+ space = ' '
+ background gold..cyan, angle: 30
+ song = para 'song.ogg', stroke: firebrick, left: 0, top: 70
+ file = 'http://www.rin-shun.com/shoes/song.ogg'
+ v = video file
+
+ para link('select'){
+ unless v.playing?
+ f = ask_open_file
+ file = f if f
+ v = video file
+ song.text = file
+ end
+ }, space, link('play'){v.play}, space, link('start'){v.start}, space, link('stop'){v.stop}
+
+ img = image File.join(DIR, '../samples/loogink.png')
+ n = 0
+ animate 5 do
+ img.move (n+=1) % 300 , 40 - rand(10) if file && v.playing?
+ end
+end
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dd082ba

Please sign in to comment.