コンピュータを楽しもう!!

今、自分が面白くていろいろやってみたことを書き綴りたいと思います。連絡先はtarosa.yでgmail.comです。

Luaridaサウンド編 (2)

LuaridaサンプルとしてリリースしたSoundSampleについて説明します。実行すると、下記のような画面になります。

サウンドサンプルには、フリー音楽素材サイトSHW (エス エイチ ダブリュ)さんより頂いた「漣華」「月華」を使わせていただきました。
サウンドファイルの読み込みは下記のようです。

------------------------------------------
--サウンドデータの読み込み
------------------------------------------
function setSound()
 --漣華ファイルを多重有りSEモードで、チャネル0にセットします
 if( sound.setSoundFile(Path.."renka-vo1.ogg", 0, 1 )==-1 )then
   dialog( Path.."renka-vo1.ogg", "ロードに失敗しました",1 )
   do return(-1) end
 end
 --月華ファイルを多重有りBGMモードで、チャネル1にセットします
 if( sound.setSoundFile(Path.."gekka-ml.ogg", 1, 0 )==-1 )then
   dialog( Path.."gekka-ml.ogg", "ロードに失敗しました",1 )
   do return(-1) end
 end
 return( 0 )
end

sound.setSoundFile()を使用して、漣華は効果音モードで、月華はBGMモードでセットしています。
メインのループは単純で下記のように書いています。

 while(true)do
   p = selectmode( rep,sta,sto,pau,owari )
   if( p==0 )then
     canvas.drawText( "[Exit]", owari.x0,owari.y0, 24, color(255,255,255), color(255,0,0) )
     return
   elseif( p==1 )then
     sound.restart(0)
     canvas.drawText( "[Restart] ", rep[1].x0, rep[1].y0, 24, color(255,255,255), color(255,0,0) )
   elseif( p==2 )then
     sound.start(0,0)
     canvas.drawText( "[Start]", sta[1].x0, sta[1].y0, 24, color(255,255,0), color(255,0,0) )
   elseif( p==3 )then
     sound.stop(0)
     canvas.drawText( "[Stop]", sto[1].x0, sto[1].y0, 24, color(255,255,255), color(255,0,0) )
   elseif( p==4 )then
     sound.pause(0)
     canvas.drawText( "[Pause]", pau[1].x0, pau[1].y0, 24, color(255,255,255), color(255,0,0) )
   elseif( p==5 )then
     sound.restart(1)
     canvas.drawText( "[Restart] ", rep[2].x0, rep[2].y0, 24, color(255,255,255), color(255,0,0) )
   elseif( p==6 )then
     sound.start(1,0)
     canvas.drawText( "[Start]", sta[2].x0, sta[2].y0, 24, color(255,255,0), color(255,0,0) )
   elseif( p==7 )then
     sound.stop(1)
     canvas.drawText( "[Stop]", sto[2].x0, sto[2].y0, 24, color(255,255,255), color(255,0,0) )
   elseif( p==8 )then
     sound.pause(1)
     canvas.drawText( "[Pause]", pau[2].x0, pau[2].y0, 24, color(255,255,255), color(255,0,0) )
   end
   touch(2)
   for i=1,2 do
     canvas.drawText( "[Restart] ", rep[i].x0, rep[i].y0, 24, color(0,0,0), color(255,255,255) )
     canvas.drawText( "[Start]", sta[i].x0, sta[i].y0, 24, color(0,0,255), color(255,255,255) )
     canvas.drawText( "[Stop]", sto[i].x0, sto[i].y0, 24, color(0,0,0), color(255,255,255) )
     canvas.drawText( "[Pause]", pau[i].x0, pau[i].y0, 24, color(0,0,0), color(255,255,255) )
   end
 end

画面にタッチした場所を、selectmode()関数で取得してサウンド処理を振り分けています。

 p = selectmode( rep,sta,sto,pau,owari )

Luaは関数にテーブルごと参照渡しできます。selectmode()関数は下記です。

------------------------------------------
--サウンドボタンのセレクト
------------------------------------------
function selectmode( rep,sta,sto,pau,owari )
local i
local x,y
 while(true)do
   x, y = touch(1)
   for i=1,2 do
     if( x>=rep[i].x0 and x<=rep[i].x1 and y>=rep[i].y0 and y<=rep[i].y1 )then
       return(1+(i-1)*4)
     elseif( x>=sta[i].x0 and x<=sta[i].x1 and y>=sta[i].y0 and y<=sta[i].y1 )then
       return(2+(i-1)*4)
     elseif( x>=sto[i].x0 and x<=sto[i].x1 and y>=sto[i].y0 and y<=sto[i].y1 )then
       return(3+(i-1)*4)
     elseif( x>=pau[i].x0 and x<=pau[i].x1 and y>=pau[i].y0 and y<=pau[i].y1 )then
       return(4+(i-1)*4)
     end
   end
   if( x>=owari.x0 and x<=owari.x1 and y>=owari.y0 and y<=owari.y1 )then
     return(0)
   end
 end
end

サウンドサンプルの動画

サウンドサンプルの実行動画です。エミュレータで動かしたもののキャプチャです。

多重で鳴らすイメージが判ると思います。

サンプルソース

最後にSoundSample.luaのソースを全て載せておきます。

------------------------------------------
--サウンドサンプル
--フリー音楽素材は、SHWさんより頂きました
------------------------------------------
--関数宣言--------------------------------
main={}        --mainメソッド
setSound={}      --サウンドデータの読み込み
selectmode={}    --サウンドボタンのセレクト
--グローバル変数宣言----------------------
Path="/sdcard/luarida/soundsample/"  --luaファイルを保存しているPath
------------------------------------------
mt={}
mt.__newindex=function(mtt,mtn,mtv)
  dialog( "Error Message", "宣言していない変数 "..mtn.." に値を入れようとしています", 0 )
  toast("画面タッチで実行を続けます", 1)
  touch(3)
end
mt.__index=function(mtt,mtn)
  dialog( "Error Message", "変数 "..mtn.." は宣言されていません", 0 )
  toast("画面タッチで実行を続けます", 1)
  touch(3)
end
setmetatable(_G,mt)
--------以下が実プログラム----------------
------------------------------------------
--サウンドデータの読み込み
------------------------------------------
function setSound()
  --漣華ファイルを多重有りSEモードで、チャネル0にセットします
  if( sound.setSoundFile(Path.."renka-vo1.ogg", 0, 1 )==-1 )then
    dialog( Path.."renka-vo1.ogg", "ロードに失敗しました",1 )
    do return(-1) end
  end
  --月華ファイルを多重有りBGMモードで、チャネル1にセットします
  if( sound.setSoundFile(Path.."gekka-ml.ogg", 1, 0 )==-1 )then
    dialog( Path.."gekka-ml.ogg", "ロードに失敗しました",1 )
    do return(-1) end
  end
  return( 0 )
end
------------------------------------------
--サウンドボタンのセレクト
------------------------------------------
function selectmode( rep,sta,sto,pau,owari )
local i
local x,y
  while(true)do
    x, y = touch(1)
    for i=1,2 do
      if( x>=rep[i].x0 and x<=rep[i].x1 and y>=rep[i].y0 and y<=rep[i].y1 )then
        return(1+(i-1)*4)
      elseif( x>=sta[i].x0 and x<=sta[i].x1 and y>=sta[i].y0 and y<=sta[i].y1 )then
        return(2+(i-1)*4)
      elseif( x>=sto[i].x0 and x<=sto[i].x1 and y>=sto[i].y0 and y<=sto[i].y1 )then
        return(3+(i-1)*4)
      elseif( x>=pau[i].x0 and x<=pau[i].x1 and y>=pau[i].y0 and y<=pau[i].y1 )then
        return(4+(i-1)*4)
      end
    end
    if( x>=owari.x0 and x<=owari.x1 and y>=owari.y0 and y<=owari.y1 )then
      return(0)
    end
  end
end
------------------------------------------
--メインプログラム
------------------------------------------
function main()
local sqx={ 0,  0 }
local sqy={ 64, 150 }
local rep={ {x0=40, x1=128, y0=100, y1=120}, {x0=40, x1=128, y0=184, y1=204} }
local sta={ {x0=150, x1=213, y0=100, y1=120}, {x0=150, x1=213, y0=184, y1=204} }
local sto={ {x0=240, x1=299, y0=100, y1=120}, {x0=240, x1=299, y0=184, y1=204} }
local pau={ {x0=320, x1=395, y0=100, y1=120}, {x0=320, x1=395, y0=184, y1=204} }
local owari={ x0=400, x1=450, y0=230, y1=254 }
local w,h
local p

  --サウンドデータの読み込み
  if( setSound()==-1 )then
    do return end
  end

  --画面を白色に
  canvas.drawCls( color(255,255,255) )
  
  w, h = canvas.getviewSize()
  canvas.drawTextBox( "音楽素材はSHW (エス エイチ ダブリュ)さんが提供されている曲を使わせていただきました。", 0, 260, 14, color(0,0,255), w )

  canvas.drawTextBox( " 漣華は、SEモード(効果音モード)でファイルをセットしているので、Startを押すたびに多重再生します", 0, 20, 18, color(255,0,0),w )

  canvas.drawText( "漣華: renka-vo1.ogg", sqx[1]+2, sqy[1]+1, 24, color(150,150,150) )
  canvas.drawText( "漣華: renka-vo1.ogg", sqx[1], sqy[1], 24, color(0,0,0) )

  canvas.drawText( "月華: gekka-ml.ogg", sqx[2]+2, sqy[2]+1, 24, color(150,150,150) )
  canvas.drawText( "月華: gekka-ml.ogg", sqx[2], sqy[2], 24, color(0,0,0) )

  canvas.drawText( "[Exit]", owari.x0,owari.y0, 24, color(0,0,0) )

  for i=1,2 do
    canvas.drawText( "[Restart] ", rep[i].x0, rep[i].y0, 24, color(0,0,0) )
    canvas.drawText( "[Start]", sta[i].x0, sta[i].y0, 24, color(0,0,255) )
    canvas.drawText( "[Stop]", sto[i].x0, sto[i].y0, 24, color(0,0,0) )
    canvas.drawText( "[Pause]", pau[i].x0, pau[i].y0, 24, color(0,0,0) )
  end

  while(true)do
    p = selectmode( rep,sta,sto,pau,owari )
    if( p==0 )then
      canvas.drawText( "[Exit]", owari.x0,owari.y0, 24, color(255,255,255), color(255,0,0) )
      return
    elseif( p==1 )then
      sound.restart(0)
      canvas.drawText( "[Restart] ", rep[1].x0, rep[1].y0, 24, color(255,255,255), color(255,0,0) )
    elseif( p==2 )then
      sound.start(0,0)
      canvas.drawText( "[Start]", sta[1].x0, sta[1].y0, 24, color(255,255,0), color(255,0,0) )
    elseif( p==3 )then
      sound.stop(0)
      canvas.drawText( "[Stop]", sto[1].x0, sto[1].y0, 24, color(255,255,255), color(255,0,0) )
    elseif( p==4 )then
      sound.pause(0)
      canvas.drawText( "[Pause]", pau[1].x0, pau[1].y0, 24, color(255,255,255), color(255,0,0) )
    elseif( p==5 )then
      sound.restart(1)
      canvas.drawText( "[Restart] ", rep[2].x0, rep[2].y0, 24, color(255,255,255), color(255,0,0) )
    elseif( p==6 )then
      sound.start(1,0)
      canvas.drawText( "[Start]", sta[2].x0, sta[2].y0, 24, color(255,255,0), color(255,0,0) )
    elseif( p==7 )then
      sound.stop(1)
      canvas.drawText( "[Stop]", sto[2].x0, sto[2].y0, 24, color(255,255,255), color(255,0,0) )
    elseif( p==8 )then
      sound.pause(1)
      canvas.drawText( "[Pause]", pau[2].x0, pau[2].y0, 24, color(255,255,255), color(255,0,0) )
    end
    touch(2)
    for i=1,2 do
      canvas.drawText( "[Restart] ", rep[i].x0, rep[i].y0, 24, color(0,0,0), color(255,255,255) )
      canvas.drawText( "[Start]", sta[i].x0, sta[i].y0, 24, color(0,0,255), color(255,255,255) )
      canvas.drawText( "[Stop]", sto[i].x0, sto[i].y0, 24, color(0,0,0), color(255,255,255) )
      canvas.drawText( "[Pause]", pau[i].x0, pau[i].y0, 24, color(0,0,0), color(255,255,255) )
    end
  end
end
main()