Best way to ensure accurate time with C

I am starting a C programmer (although not a novice programmer) who wants to dive into a project in order to teach herself C. My project is based on music, and because of this I am wondering if there are any "best practices" for -se, when it comes to time functions.

Just to clarify, my project is largely an attempt to create several music programs for notes / compositions (remember, the emphasis is on barebones). I initially thought about using OSX as my platform, but I want to do it in C, not Obj-C (although I know it will probably be easier ... CoreAudio looked like a pretty powerful tool for this kind of thing). Therefore, although I do not need to create OSX applications in Obj-C, I will probably end up creating this on a Linux system (perhaps debian ...).

Thanks to everyone for your wonderful answers.

+4
source share
4 answers

There are two exact methods for synchronization functions:

  • The execution of one process.
  • Event Handler / Timer Callback

Single process execution
Most modern computers run several programs simultaneously. In fact, they execute chunks of many programs, replacing them based on priorities and other indicators, so that they look as if one program was running at the same time. These time effects affect the time in programs. Either the program is delayed while reading time, or the OS is delayed when setting its own time variables.

The solution in this case is to eliminate the performance of many tasks. The ideal environment for better accuracy is to make your program the only program. Some operating systems provide APIs for superuser applications to block all other programs or kill them.

Timer Handling / Callback
Since the OS cannot be trusted to execute your program with high precision, most OSs will provide Timer APIs. Many of these APIs include the ability to call one of your functions after a timer expires. This is called a callback function. Another OS may send a message or generate an event when the timer expires. They belong to the class of timer handlers. The callback process has less overhead than handlers, and therefore is more accurate.

Musical equipment

Although you may have your transfer of music to the speakers, many computers now have separate processors that play music. This frees up the main processor and provides longer notes, rather than sounds separated by silent breaks due to malfunctions in your program, send the following sounds to the speaker.

A quality music processor has at least these features:

  • Start playback
  • Music End Notification

Start the game This is a function in which you indicate to the music processor where your data is located and the size of the data. The processor will start playing music.

Notification of the end of music You give the processor a pointer to the function that it will call when the music data has been processed. Good processors will call the function earlier so that there are no spaces in the sounds during the reboot.

It all depends on the platform and may not be standard across platforms.

Hope this helps.

+4
source

This is a fairly large area, and depending on what you want to do, it is potentially very difficult.

You will not attach much importance to saying that your project is "based on music."

  • Is it a music score program?
  • Is it sound processing?
  • Is this filtering MIDI data?
  • Is this a sequence of MIDI data?
  • Is this the generation of sound from MIDI data.
  • Does this only perform playback?
  • Do I need to work in real time?

Your question, though, hints at real-time work, so in this case ...

A general rule when working in a real-time environment does nothing that can block the real-time stream . It includes:

  • Call free / malloc / calloc / etc (allocation / freeing of dynamic memory).
  • I / O.any File
  • Using spies / semaphores / mutexes in threads.
  • Call GUI code.
  • Call printf.

Given these considerations for a real-time music application, you will need to learn how to multithread in C and how to transfer data from a user interface / GUI stream to a real-time stream WITHOUT violating ANY of the above restrictions.

For a real-time (and MIDI) open source server (routing), see http://jackaudio.org

+2
source

gettimeofday () is the best for wall clocks. getrusage () is best for CPU time, although it may not be portable. clock () is more portable for processor synchronization, but may have integer overflow.

+1
source

It is quite system dependent. What OS are you using?

You can take a look at gettimeofday () for fairly high granularity. It should work fine if you just need to read the time once in awhile.

SIGALRM / setitimer can be used to periodically receive an interrupt. In addition, some systems have higher-level libraries to work with time.

0
source

Source: https://habr.com/ru/post/1309812/


All Articles