#!ruby # -*- mode: ruby; coding: utf-8 -*- # Last updated: <2016/12/14 21:56:15 +0900> # # gosuを使ってサウンドが鳴らせるかテスト。 # wav と ogg が鳴らせるか試してみる。 # # Ubuntu Linux上で動かした際に警告が出る症状に対処してみたものの解決せず。 # # 動作確認環境: # Windows10 x64 + Ruby 2.2.6 p396 mingw32 + gosu 0.10.8 # Ubuntu 16.04 LTS + Ruby 2.3.1 p112 + gosu 0.10.8 require 'gosu' class MyWindow < Gosu::Window def initialize super 320, 240, false self.caption = 'Sound Play Test' # wavファイルを読み込む @sounds = [] @sis = [] [ "tmp_se_01.wav", "tmp_se_02.wav", "tmp_se_03.wav", "tmp_se_01.ogg", "tmp_se_02.ogg", "tmp_se_03.ogg" ].each do |fn| @sounds.push(Gosu::Sample.new(fn)) @sis.push(nil) end @cnt = 0 @close_req = 0 @se_all_stop = false end def update if @se_all_stop # SE全停止が要求されてる stop_all_se @se_all_stop = false end if @close_req > 0 # ウインドウcloseが要求されてる @close_req -= 1 if @close_req == 0 # 一定時間が経過した if se_playing? # まだ鳴ってる @close_req = 30 puts "SE playing (in update)" else # 何も鳴ってない dispose_se_resource GC.start close # ウインドウを閉じる end end return end tm = 45 if @cnt % tm == 0 # 一定フレーム数が過ぎたらサウンドを再生 n = (@cnt / tm) % @sounds.length if @sis[n] == nil or !@sis[n].playing? @sis[n] = @sounds[n].play else # @sis[n].stop end end @cnt += 1 end def draw end # キーが押された時に呼ばれる def button_down(id) if id == Gosu::KbEscape # ESCキーが押された # @se_all_stop = true # SE全停止を要求 @close_req = 30 # xxフレーム後にウインドウを閉じるように要求 end end # 何かのSEが鳴ってたら true を返す def se_playing? return false unless @sis cnt = 0 @sis.each do |si| next unless si cnt += 1 if si.playing? end return true if cnt > 0 return false end # SEを全て停止 def stop_all_se return unless @sis @sis.length.times do |i| next unless @sis[i] @sis[i].stop end puts "SE stop all" end # サウンド関連リソースを全て破棄、するはずなんだけど… def dispose_se_resource if @sis @sis.length.times { |i| @sis[i] = nil } @sfs = nil end if @sounds @sounds.length.times { |i| @sounds[i] = nil } @sounds = nil end end end wdw = MyWindow.new wdw.show # SEが鳴り終わるまで待つ while wdw.se_playing? puts "SE playing" sleep(1) end puts "SE not playing" wdw.dispose_se_resource wdw = nil GC.start exit