Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
My Mic "self.global" issues. — Gideros Forum

My Mic "self.global" issues.

myspsmysps Member
edited October 2013 in Game & application design
I've been getting some help behind the scenes from @ar2rsawseen but I don't want to worry him. I'm so close to finishing up my app (yes almost 2yrs and going) . I'm adding a microphone option to my options page and I'm confused about local and self. @ar2rsawseen told me to change from stage to self which I have done but when I hit the screen, I briefly see the images and microphone options but then it disappears. Later I'm able to click the buttons and record like normal however I guess I don't completely understand why it comes in then disppears.

Any clue? Here is some of my options code:
 
require "microphone"
 
Options = gideros.class(Sprite)
 
-- options
 
local width = application:getContentWidth()
local height = application:getContentHeight()
 
local dx = application:getLogicalTranslateX() / application:getLogicalScaleX()
local dy = application:getLogicalTranslateY() / application:getLogicalScaleY()
 
function Options:init()
 
self.levelMeter = LevelMeter.new()
self:addChild(self.levelMeter)
self.levelMeter:setY(30)
 
 
self.microphone = Microphone.new(nil, 22050, 1, 16)
 
self.microphone:addEventListener(Event.DATA_AVAILABLE, function(event)
	print("*")
	self.levelMeter:setLevel(event.peakAmplitude, self)
end)
 
self.microphone:setOutputFile("|D|record.wav")
 
self.record = Button.new(	Bitmap.new(Texture.new("gfx/record-up.png")),
							Bitmap.new(Texture.new("gfx/record-down.png")),
							Bitmap.new(Texture.new("gfx/record-disabled.png")))
self.record:setPosition(70, 130)
self:addChild(self.record)
 
 
self.recordStop = Button.new(	Bitmap.new(Texture.new("gfx/stop-up.png")),
								Bitmap.new(Texture.new("gfx/stop-down.png")))
self.recordStop:setPosition(70, 130)
 
 
self.play = Button.new(	Bitmap.new(Texture.new("gfx/play-up.png")),
							Bitmap.new(Texture.new("gfx/play-down.png")),
							Bitmap.new(Texture.new("gfx/play-disabled.png")))
self.play:setPosition(70, 200)
self.play:setDisabled(true)
self:addChild(self.play)
 
 
self.playStop = Button.new(Bitmap.new(Texture.new("gfx/stop-up.png")),
							Bitmap.new(Texture.new("gfx/stop-down.png")))
self.playStop:setPosition(70, 200)
 
 
function self:onRecord()
	self.play:setDisabled(true)
	self.record:removeFromParent()
	self:addChild(self.recordStop)
	self.microphone:start()
end
 
self.record:addEventListener(Event.CLICK, self.onRecord, self)
 
function self:onRecordStop()
	self.play:setDisabled(false)
	self.recordStop:removeFromParent()
	self:addChild(self.record)
	self.microphone:stop()
	self.levelMeter:setLevel(0)
	self.play:setDisabled(false)
end
self.recordStop:addEventListener(Event.CLICK, self.onRecordStop, self)
 
 
local sound = nil
local channel = nil
 
function self:onPlayStop()
	self.record:setDisabled(false)
	self.playStop:removeFromParent()
	self:addChild(self.play)
	self.channel:stop()
end
self.playStop:addEventListener(Event.CLICK, self.onPlayStop, self)
 
function self:onPlay()
	self.record:setDisabled(true)
	self.play:removeFromParent()
	self:addChild(self.playStop)
	self.sound = Sound.new("|D|record.wav")
	self.channel = self.sound:play()
	self.channel:addEventListener(Event.COMPLETE, self.onPlayStop, self)
end
self.play:addEventListener(Event.CLICK, self.onPlay, self)
 
 
self.background = Bitmap.new(TextureRegion.new(Texture.new("gfx/mainlandscape.png", true)))	
self:addChild(self.background)
 
--set position in center 
 
self.background:setAnchorPoint(0.5, 0.5)
self.background:setPosition(width/2, height/2)
 
local texture = Texture.new("gfx/home.png")
 
local back = Bitmap.new(texture)
back:setScale(1)
self.back = Button.new(back, back)
self.back:setPosition((width-self.back:getWidth())/2, 40)
self:addChild(self.back)
self.back:addEventListener("click", function()
	sc.scenes:changeScene("StartPage",1, SceneManager.fade, moveFromRightWithFade) 
end)
end
Thanks for any explanation. I've looked other sample code and some include self and local. I'm quite confused about the two and when to use.

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited October 2013
    It seems its up to me after all :)

    PMed you ;)
  • @Ar2rsawseen,
    Ha, ha! ;))

    Still, would you mind posting a brief summary of the solution here for all to see?
    Kate's Catalogue of Travelling Theatre Centres :
    Meet Kate. Grey is her favourite colour. Maths is her favourite subject. Decency is her favourite type of behaviour.
    She definitely does not like jewellery or modelling, but loves aeroplanes and other machines made of aluminium.
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Honestly, I dont know the problem
    I just took the project of @mysps I had previously, and added the code from mic example project.

    Did not post here, because it is a private @mysps project, so send through pm and let @mysps figure out what was wrong :)
  • @mysps, did you figure out what made the items disappear?
    Kate's Catalogue of Travelling Theatre Centres :
    Meet Kate. Grey is her favourite colour. Maths is her favourite subject. Decency is her favourite type of behaviour.
    She definitely does not like jewellery or modelling, but loves aeroplanes and other machines made of aluminium.
  • well @ar2rawseen sent me a pm im downloading it now. i just want to understand it. i've tried honestly. i will compare my old code to understand why it didnt work

    thanks!
  • myspsmysps Member
    edited October 2013
    @Platypus, so I studied the changes that @ar2rsawseen sent and understood it a bit.

    Questions:

    Why isn't the function Microphone not added to self?
    local microphone = Microphone.new(nil, 22050, 1, 16)
     
    	microphone:addEventListener(Event.DATA_AVAILABLE, function(event)
    		print("*")
    		levelMeter:setLevel(event.peakAmplitude)
    	end)
     
    	microphone:setOutputFile("|D|record.wav")
    When I made different changes, I believe I had it like this but I added self to the microphone which caused an error saying that the .wav wasn't found.

    Thanks again Arturs and Happy Birthday!!
  • Here is the correct code:
    Options = gideros.class(Sprite)
     
    require "microphone"
    -- options
     
    local width = application:getContentWidth()
    local height = application:getContentHeight()
     
    local dx = application:getLogicalTranslateX() / application:getLogicalScaleX()
    local dy = application:getLogicalTranslateY() / application:getLogicalScaleY()
     
    function Options:init()
     
    	self.background = Bitmap.new(TextureRegion.new(Texture.new("gfx/mainlandscape.png", true)))	
    	self:addChild(self.background)
     
    	--set position in center 
     
    	self.background:setAnchorPoint(0.5, 0.5)
    	self.background:setPosition(width/2, height/2)
     
    	local texture = Texture.new("gfx/home.png")
     
    	local back = Bitmap.new(texture)
    	back:setScale(1)
    	self.back = Button.new(back, back)
    	self.back:setPosition((width-self.back:getWidth())/2, 20)
    	self:addChild(self.back)
    	self.back:addEventListener("click", function()
    		sc.scenes:changeScene("StartPage",1, SceneManager.fade, moveFromRightWithFade) 
    	end)
     
    	local levelMeter = LevelMeter.new()
    	self:addChild(levelMeter)
    	levelMeter:setPosition((width-levelMeter:getWidth())/2, 200)
     
    	local microphone = Microphone.new(nil, 22050, 1, 16)
     
    	microphone:addEventListener(Event.DATA_AVAILABLE, function(event)
    		print("*")
    		levelMeter:setLevel(event.peakAmplitude)
    	end)
     
    	microphone:setOutputFile("|D|record.wav")
     
    	local record = Button.new(	Bitmap.new(Texture.new("gfx/record-up.png")),
    							Bitmap.new(Texture.new("gfx/record-down.png")),
    							Bitmap.new(Texture.new("gfx/record-disabled.png")))
    	record:setPosition((width - record:getWidth())/2, 260)
    	self:addChild(record)
     
     
    	local recordStop = Button.new(	Bitmap.new(Texture.new("gfx/stop-up.png")),
    								Bitmap.new(Texture.new("gfx/stop-down.png")))
    	recordStop:setPosition((width - recordStop:getWidth())/2, 260)
     
     
    	local play = Button.new(	Bitmap.new(Texture.new("gfx/play-up.png")),
    							Bitmap.new(Texture.new("gfx/play-down.png")),
    							Bitmap.new(Texture.new("gfx/play-disabled.png")))
    	play:setPosition((width - play:getWidth())/2, 360)
    	play:setDisabled(true)
    	self:addChild(play)
     
     
    	local playStop = Button.new(Bitmap.new(Texture.new("gfx/stop-up.png")),
    							Bitmap.new(Texture.new("gfx/stop-down.png")))
    	playStop:setPosition((width - playStop:getWidth())/2, 360)
     
     
    	local function onRecord()
    		play:setDisabled(true)
    		record:removeFromParent()
    		self:addChild(recordStop)
    		microphone:start()
    	end
    	record:addEventListener(Event.CLICK, onRecord)
     
    	local function onRecordStop()
    		play:setDisabled(false)
    		recordStop:removeFromParent()
    		self:addChild(record)
    		microphone:stop()
    		levelMeter:setLevel(0)
    		play:setDisabled(false)
    	end
    	recordStop:addEventListener(Event.CLICK, onRecordStop)
     
     
    	local sound = nil
    	local channel = nil
     
    	local function onPlayStop()
    		record:setDisabled(false)
    		playStop:removeFromParent()
    		self:addChild(play)
    		channel:stop()
    	end
    	playStop:addEventListener(Event.CLICK, onPlayStop)
     
    	local function onPlay()
    		record:setDisabled(true)
    		play:removeFromParent()
    		self:addChild(playStop)
    		sound = Sound.new("|D|record.wav")
    		channel = sound:play()
    		channel:addEventListener(Event.COMPLETE, onPlayStop)
    	end
    	play:addEventListener(Event.CLICK, onPlay)
    end
  • That's great!
    Thanks, @mysps.
    Kate's Catalogue of Travelling Theatre Centres :
    Meet Kate. Grey is her favourite colour. Maths is her favourite subject. Decency is her favourite type of behaviour.
    She definitely does not like jewellery or modelling, but loves aeroplanes and other machines made of aluminium.
Sign In or Register to comment.