Cocos2d on application start gives random fps drop? - cocos2d-iphone

I am developing an iphone game using cocos2d and have it set to to show the FPS. Now most of the time the application will start and run at 60 fps, However apparently randomly sometimes the application will start and top at only 40 fps.
This does not happen often and seems to happen only when you quit the application and restart it as soon as possible, allthough this does not trigger the effect most of the time. But after a few quick quit-restart sequences the game eventually starts and has only 40 fps instead of the normal 60. If I quit the application at this point and wait for about 3 seconds it will again start with the normal 60 fps.
I am assuming this has something to do with the application not being able to free all of its memory before starting again?, this problem seems to be related to the iPhone OS.
It is important to note that this never happens if I quit the application wait 5 seconds and restart. Has anyone had this sort of problem before?. Any ideas of why this might be happening?.

If anyone is wondering this is because there used to be a bug when emulating the OPEN GL 2.0 in the iPhone 3GS/iPod Touch 3G, this has been fixed for OS 3.1. And all you have to do is the following:
// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use Threaded director
if( ! [Director setDirectorType:CCDirectorTypeDisplayLink] )
[Director setDirectorType:CCDirectorTypeThreadMainLoop];

Related

Low Latency Audio Output C++

I am currently working on a project, wherein I am supposed to process frames. The task at hand is to read the frame data, detect if the data crosses a particular threshold and then make a sound.
The frame per second is about 45-50.
I did that with the function Beep () and tried 2 ms for the duration but then the beeps were in a queue. It still takes for a while even though the input does not have values ​​above the threshold. So I just made a delay of 175ms and then it worked fine, but now I'm just processing 5 samples per second out of the 45-50 samples.
Now I'm using the function PlaySound() and playing a 1ms sound file in the player. Now it takes about 250 ms to play the sound. So now even without delay 4 samples.
I am completely new to real-time programming like this. I am facing the Latency issue.
I would really appreciate it if any of you can help me with the project.
Thank you in advance.
OS:- Windows 10
IDE:- Visual Studio 2017 Community
Language:- C++

Changing volume in pygame.mixer (almost) no effect

I'm trying to make an application in tkinter that has a number of buttons you can assign sound on and play it later. The click of the button itself only calls play() method, so loading of the sound is done beforehand.
I tried making some kind of volume control with sliders (tk.Scale) and I noticed there is no noticeable difference between most volume values until I get very close to zero (take into consideration that slider resolution is 0.01 from 0.0 to 0.1).
At around 0.02 I think I notice the sound volume is significantly lower and if I get to zero, the sound is muted. Please note that this happens if I move the slider while no sounds are playing.
The interesting thing is, if I try playing a sound that is long enough to let me move the slider while it's playing, I can notice the difference right away, but if the sound stops playing and I try playing it again, it goes to the "default" volume again.
Since I divided my application into multiple scripts according to what they do (recording sound, playing sound, GUI) I thought it could be the problem that I haven't directly initialized pygame mixer, but rather from the imported module, so I made a new python script and typed this code in:
import pygame
import time
pygame.mixer.pre_init(frequency=44100, size=-16, channels=1, buffer=512)
pygame.mixer.init()
sound1=pygame.mixer.Sound("sound.wav")
sound1.set_volume(1.0)
print sound1.get_volume()
sound1.play()
time.sleep(sound1.get_length())
sound1.set_volume(0.5)
print sound1.get_volume()
sound1.play()
time.sleep(sound1.get_length())
sound1.set_volume(0.08)
print sound1.get_volume()
sound1.play()
time.sleep(sound1.get_length())
The output is the following: 1.0,0.5,0.078125 (one below the other) confirming that the volume has indeed been set (I hope properly).
The only time I can notice the difference is the third case, which is not that noticeable really, I want the volume increase to be linear, this is far from it.
I tried the same thing with a channel:
sound1=pygame.mixer.Sound("sound.wav")
channel=pygame.mixer.find_channel(True)
channel.set_volume(1.0)
channel.play(sound1)
time.sleep(sound1.get_length()/2)
channel.set_volume(0.5)
print "Volume set"
time.sleep(sound1.get_length()/2)
No luck, the same thing happens here too.
I spent all day googling "pygame mixer volume problem" "pygame mixer volume set problem" and similar phrases, but no luck. Hopefully someone here can be of help, considering my diploma depends on a python method. :)
Thanks in advance.
I found the answer (thank you Gummbum from PyGame IRC).
The problem is not in Python or Pygame itself, but rather in Windows. It seems sound enhancements are somehow fiddling with the way the sound my script is playing (or any other Pygame script for that matter).
I'm on Windows 10 and this is how I did it:
Right click on the speaker icon in the taskbar
Select Playback Devices
Select Speakers and Properties
Go to Enhancements tab and uncheck Equalizer and Loudness Equalization
That's it.
Music on Raspberry Pi:
using Pygame to program playing music on my Raspberry Pi, I found the volume way too low at settings 0.0 to 1.0. Then I tried setting the value higher up to 10.0(pygame.mixer.music.set_volume(vol)) and it works Great!
Maybe you need to change the file format to mp3 for running the music because when i copied this code got an alarm music in mp3 extension and ran it in Spyder(anaconda) python 3.8 , it works , There might be 2 solutions :
Change your python version to 3.8
Convert the .wav extension into mp3
I am not sure it would work or not but with these situations , it might work at your end.

Increase performance of Windows apps

I have imported an app from Visual Studio compiler to MinGW and I faced a problem – performance degradation. Usage of CPU increased from 30% to 100%.
There is one interesting thing. If before running my app or during, I’ve run Windows Media Player – performance of my app is going to fine. CPU usage is going down till 30% and works faster (about 10 times faster).
I’ve googled it and found. It relates to a service, which names as a Multimedia Class Scheduler Service (MMCSS). The main problem is: this service forks under Windows Vista and later, but I’ve tested and imported my app under Win XP.
So, does anyone know how to use this feature under XP? And how Windows Media Player increases performance of my app?
Windows Media Player changes the resolution of the system multimedia timer. Basically, this occurs when your application really should be using something like the High Performance Timer but is using the multimedia timer instead, which simply doesn't have and isn't intended to have the necessary accuracy or resolution to be a high-performance timer. As a result, any timings in your program essentially don't work as they should, which is especially bad if you're trying to sleep or block for a fixed time.

Firefox interacts with my timers somehow ?!?! This is crazy ^^

I have a problem with the timers in my win32 C++ DirectX game (a little demo). I am using "timeGetTime" to get the current time and use it to playback the animations or for logic by using the delta time (I also use a constant for normalization when animating). I use a tickrate as low as 2 miliseconds sometimes. I am using only ULONGs when working with time. The game works just fine, but if I start Mozilla Firefox and I start the game after all is playing very fast (the animations and the game logic). It's like fast forwarding. The strange thing is that it seems that a few timers are not affected by this. Does someone have a clue ? What is the connection between Firefox and my timers ? After exiting Firefox is stays the same, but after some undefined time or a PC restart it goes back to normal. Any suggestions are appreciated, even if it is a long shot. Thank you.
Yes the default precision of TimeGetTIme is 5ms or more.
This can be altered with the calls timeBeginPeriod and timeEndPeriod functions.
Alterning the precision of TimeGetTime affects all running applications.
I guess Firefox is calling those functions which affect your application.
Change to use the QueryPerformanceCounter/QueryPerformanceFrequency methods instead which provide high res timing and will not be subject to the issues of TimeGetTime.
EDIT:
A couple of links that show you how to use the functions.
http://www.programmersheaven.com/mb/windows/311148/311148/using-queryperformancecounter/
And also note the remarks on the MSDN page:
http://msdn.microsoft.com/en-us/library/ms644904(v=vs.85).aspx

Off screen rendering when laptop shuts screen down?

I have a lengthy number-crunching process which takes advantage of quite abit of OpenGL off-screen rendering. It all works well but when I leave it to work on its own while I go make a sandwich I would usually find that it crashed while I was away.
I was able to determine that the crash occurs very close to the moment The laptop I'm using decides to turn off the screen to conserve energy. The crash itself is well inside the NVIDIA dlls so there is no hope to know what's going on.
The obvious solution is to turn off the power management feature that turns the screen and video card off but I'm looking for something more user friendly.
Is there a way to do this programatically?
I know there's a SETI#home implementation which takes advantage of GPU processing. How does it keep the video card from going to sleep?
I'm not sure what OS you're on, but windows sends a message that it is about to enter a new power state. You can listen for that and then either start processing on the CPU or deny the request to enter a lower-power state.
For the benefit of Linux users encountering a similar issue, I thought I'd add that, you can obtain similar notifications and inhibit power state changes using the DBUS API. An example script in Python, taken from the link, to inhibit power state change:
#!/usr/bin/python
import dbus
import time
bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
devobj = bus.get_object('org.freedesktop.PowerManagement',
'/org/freedesktop/PowerManagement')
dev = dbus.Interface (devobj, "org.freedesktop.PowerManagement.Inhibit")
cookie = dev.Inhibit('Nautilus', 'Copying files from /media/SANVOL')
time.sleep(10)
dev.UnInhibit(cookie)
According to MSDN, there is an API that allows an application to tell Windows that it is still working and that Windows should not go to sleep or turn off the display.
The function is called SetThreadExecutionState (MSDN). It works for me, using the flags ES_SYSTEM_REQUIRED and ES_CONTINUOUS.
Note, however, that using this function does not stop the screen saver from running, which might interfere with your OpenGL app if the screen saver also uses OpenGL (oder Direct3D).