It is currently Sun Oct 25, 2020 5:56 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Delay problem
PostPosted: Wed Aug 26, 2015 9:06 am 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 20, 2009 9:24 am
Posts: 573
Location: Almaty (Kazakhstan)
i have one small problem with Delay. i try to read midi file and play it from array. in this array stores "time" of midi. it is a very big array some times. so when play this array - i used delay, but some times Delay(1) is too big. it is in millisec - but i need microsec. have some one same like problem and how you solved it?


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Wed Aug 26, 2015 9:09 am 
Offline
Moderator
Moderator

Joined: Thu Dec 31, 2009 11:05 pm
Posts: 1112
Location: Gernsbach (Germany)
http://www.purebasic.fr/german/viewtopi ... 40#p322040

_________________
ImageImageImageImage Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Wed Aug 26, 2015 9:11 am 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 20, 2009 9:24 am
Posts: 573
Location: Almaty (Kazakhstan)
thanks, will try it :)


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Thu Dec 10, 2015 12:18 am 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 20, 2009 9:24 am
Posts: 573
Location: Almaty (Kazakhstan)
Code:
Global QueryPerformanceFrequency.q, QueryPerformanceFactor.f
QueryPerformanceFrequency_(@QueryPerformanceFrequency)
QueryPerformanceFactor = 1e6/QueryPerformanceFrequency

Procedure DelayMicroSeconds(Time.q)
   Protected StartTime.q, EndTime.q
   QueryPerformanceCounter_(@StartTime)
   Repeat
      Delay(0)
      QueryPerformanceCounter_(@EndTime)
   Until (EndTime-StartTime)*QueryPerformanceFactor > Time
EndProcedure



a = ElapsedMilliseconds()
For n = 1 To 1000
   DelayMicroSeconds(300)
Next
Debug ElapsedMilliseconds()-a

this code from german forum is super, but it can be changed to more small pause? i need 1/44100 some kind of:
0. 000 022 675s


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Thu Dec 10, 2015 7:09 am 
Offline
Addict
Addict
User avatar

Joined: Sat Apr 26, 2003 8:26 am
Posts: 2991
Location: Planet Earth
You should remove the Delay(0) from the loop.

A delay of 0 doesn't make sense. Because of that, at least in Windows
a delay of 0 has a special meaning, see Sleep function.
Sleep() was used for Delay on Windows, but I have something in mind
that it was changed some versions ago. Not sure how PB handles a Delay of 0 now.
Maybe it just does nothing, so a delay of 0 wouldn't make sense.

Anyway, with so high precision you don't want any delay.

Under heavy OS load (many running applications and all cores and CPU's busy),
you will probably get timing problems because of task switching.
Setting a higher priority for the thread that runs the timing and MIDI output code
could at least help a little bit. See also this discussion at StackOverflow
and Microsecond Resolution Time Services for Windows.


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Thu Dec 10, 2015 8:13 am 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4980
Location: Germany
Hi,

I optimized it a bit (I hope):
Code:
Procedure DelayMicroSeconds(Time.q)
 
  Protected StartTime.q, EndTime.q
 
  QueryPerformanceCounter_(@StartTime)
  Time / QueryPerformanceFactor
  Repeat
    QueryPerformanceCounter_(@EndTime)
  Until EndTime - StartTime >= Time
 
EndProcedure


Bernd


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Thu Dec 10, 2015 9:54 am 
Offline
Addict
Addict
User avatar

Joined: Sat Apr 26, 2003 8:26 am
Posts: 2991
Location: Planet Earth
By the way, does anybody know if calling QueryPerformanceFrequency() only once (at application start)
could make problems?
Modern processors often provide a way to change CPU frequency while running, AFAIK.
Lower Frequency for power saving in Notebooks, or higher frequency for a limited time to boost performance.

Could this influence high precision timer codes?


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Thu Dec 10, 2015 10:06 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Sep 17, 2015 3:39 pm
Posts: 147
try this 8)

Code:
Procedure DelayEx(timeMicroSeconds.q)
  ; Name...........: DelayEx()
  ; Description ...: Sleeps down to 0.1 microseconds
  ; Parameters ....:  timeMicroSeconds      - Amount of microseconds to sleep
  ; Author ........: Celtic88
  timeMicroSeconds=-1*(timeMicroSeconds*10)
  ProcedureReturn ZwDelayExecution_(0,@timeMicroSeconds)
EndProcedure

Test= ElapsedMilliseconds()
DelayEx(1000000)
Debug Str(ElapsedMilliseconds()-Test) + " Ms"

_________________
interested in Cybersecurity..


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Thu Dec 10, 2015 10:50 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1907
infratec wrote:
Time / QueryPerformanceFactor

Division by zero error. :shock:


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Thu Dec 10, 2015 11:28 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Apr 12, 2006 7:59 pm
Posts: 173
Location: Germany
@Danilo: For modern CPUs: Invariant TSC/TscInvariant!
Intel wrote:
CPUID 80000007H, EDX Bit8 Invariant TSC
The invariant TSC will run at a constant rate in all ACPI P-, C-, and T-states. This is the architectural behavior
moving forward. On processors with invariant TSC support, the OS may use the TSC for wall clock timer services
(instead of ACPI or HPET timers). TSC reads are much more efficient and do not incur the overhead associated with
a ring transition or access to a platform resource.
AMD wrote:
CPUID 80000007H, EDX Bit8 TscInvariant
TscInvariant: TSC invariant. The TSC rate is ensured to be invariant across all P-States, C-States,
and stop grant transitions (such as STPCLK Throttling); therefore the TSC is suitable for use as a
source of time.


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Thu Dec 10, 2015 11:38 am 
Offline
Addict
Addict
User avatar

Joined: Sat Apr 26, 2003 8:26 am
Posts: 2991
Location: Planet Earth
Thanks, Helle!

- Time Stamp Counter


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Thu Dec 10, 2015 2:00 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 20, 2009 9:24 am
Posts: 573
Location: Almaty (Kazakhstan)
thanks for a lot answers :) i have no succes...
0. 000 022 675s
delay millisec that DelayMicroSeconds proc microsec even dont know

problem is - when i play sound - tempo is drunk. delay pause count is a little wrong. i make attach to illustrate this drunk tempo. you will need to fix path to Array.txt and choice one of three method of pause by comment and uncomment in code: delay(1), infratec and CELTIC88 (how work CELTIC88's variant i am even dont understand)

for compare with correct tempo i add winamp plugin and that original melody.

https://www.dropbox.com/s/sdvel9hsu6yq9 ... r.zip?dl=1

can you help?


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Thu Dec 10, 2015 2:54 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4980
Location: Germany
Dude wrote:
Time / QueryPerformanceFactor
Division by zero error. :shock:


Try to replace QueryPerformanceFactor.f with
QueryPerformanceFactor.d

Bernd


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Thu Dec 10, 2015 3:27 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4980
Location: Germany
And
Code:
DelayMicroSeconds(MusArr(i)\pause * 22.675)

Bernd


Top
 Profile  
Reply with quote  
 Post subject: Re: Delay problem
PostPosted: Thu Dec 10, 2015 3:42 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 20, 2009 9:24 am
Posts: 573
Location: Almaty (Kazakhstan)
and with .f and with .d and with 22.675 and with 22 and with 23 - same drunk tempo :(((


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 61 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye