How to implement the mach_absolute_time Swift api? - ios

This may be a bit of a silly question, but I'm looking for a reliable way to create a repeating timer in Swift that's as completely accurate as possible. I've researched this quite a bit and have found that the mach_absolute_time api seems to be what I'm looking for.
However, while I think I understand mach_absolute_time, there's not much out there on mach_wait_until (if I understand correctly that's the mach api version of using Timer.scheduledTimer() to call a selector).
So my goal is to turn something like Martin R's answer to this post: Measuring Time Accurately in Swift for Comparison Across Devices into a timer that can measure out an exact amount of time, call a selector, and repeat with little to no loss of time between ticks.
Thanks!
Update
The other thing I ran into is that the timer can't lock up the app while the it's running either – I know since mach is pretty low level, calling mach_wait_until will stop everything else and wait for the time to elapse (I think).

Related

CAN Bus Communication: Same Bit Rate, Different Time Segments

I would like to preface that I am new to CAN, so I apologize if this is an obvious question.
I am using an STM32 microprocessor that has CAN communication, whose bit rate I have set to 500 kBit/s. I am trying to communicate with another node (whose source code I do not have access to) and their bit rates are the same (500 kBit/s). I am wondering if they're using the same bit rate but different bit time parameters (Prescaler, SyncJumpWidth, TimeSeg1, TimeSeg2), will they still be able to communicate with each other?
Possibly, but it isn't certain. Propagation delay caused by long wires for example, will make it more or less critical where the sample point is located. Stubs or poorly terminated buses may suffer from signal reflections that make the sample point location more or less critical. Higher baudrate leads to more bit length inaccuracies. As does a poor clock source. And so on. Also if you go with some strange and exotic settings, nothing tends to work but you get error frames.
I have good experience from following the requirements of the CANopen standard, which is to place the sample point as close to 87.5% as possible. The easiest way to achieve that is by a total of 16tq where phase seg2 is 2tq long. 16 tends to work well with most prescaler clocks as well. Note that there's a hard requirement by the CAN standard not to use more than 25tq.

High-precision timer in iOS

What is the most precise way to measure time intervals in iOS? So far I have been using NSDate to "mark" events and then timeIntervalSinceDate method to calculate the interval, but is there more precise approach? E.g. Windows has so called QPC for that kind of thing. Is there something similar in iOS (or MacOSX) world?
You can get down to nanoseconds using mach_absolute_time(). Here is an article on using it:
https://developer.apple.com/library/mac/qa/qa1398/_index.html
It also exists on iOS.

Fastest Inverse Square Root on iPhone

I'm working on an iPhone app that involves certain physics calculations that are done thousands of times per second. I am working on optimizing the code to improve the framerate. One of the pieces that I am looking at improving is the inverse square root. Right now, I am using the Quake 3 fast inverse square root method. After doing some research, however, I heard that there is a faster way by using the NEON instruction set. I am unfamiliar with inline assembly and cannot figure out how to use NEON. I tried implementing the math-neon library but I get compiler errors because most of the NEON-based functions lack return.
EDIT: I've suddenly been getting some "unclear question" close votes. Although I think its quite clear and those who answered obviously understood, maybe some people need it stated explicitly:
How do you use Neon to perform faster calculations? And is it really the fastest method for getting the inverse square root on the iPhone?
EDIT: I did some more formal testing on Neon VS Quake today, but If anything, I'm even more uncertain about the outcome now:
In-App Testing: (An app that is currently in the app store with its invsqrt method modified)
Quake Method (leading by a marginal increase in average FPS under stressful conditions)
Neon (It was a really close call but it seemed that Quake was slightly faster)
1/sqrtf() (a bit more noticeable difference, 1-3 FPS drop).
"Formal" Testing (An app that devours my Phone's CPU. Times how long it takes each method to get through an array of 10000000 randomly generated floats)
Neon (clearly the fastest, and double the speed if it is used to do two sqrts at once).
1/sqrtf() (Only marginally slower than Neon. This surprising result leads me to deem this test "inconclusive" until I investigate further)
Quake (This method, surprisingly, was a few orders of magnitude slower than the other two methods. This is especially surprising given its performance in the other test.)
While quake vs neon was too close to say anything for sure in the app performance test, the quake vs 1/sqrtf() was quite clearly cut out in the first test, and the second test was extremely consistent with the values it outputted. What is important in the end, though, is app performance, so I'm going to make my final decision based on that test.
The accepted answer of the question you've linked already provides the answer, but doesn't spell it out:
#import <arm_neon.h>
void foo() {
float32x2_t inverseSqrt = vrsqrte_f32(someFloat);
}
Header and function are already provided by the iOS SDK.
https://code.google.com/p/math-neon/source/browse/trunk/math_sqrtf.c <- there's a neon implementation of invsqrt there, you should be able to copy the assembly bit as-is

Access audio buffer of an AVAudioSession

Hope this question makes some sense, I'm completely lost....
In my proto-app I'm recording micro input and saving it, and so far no problems at all.
I now need to access the buffer while I'm recording it in order to pass chunks of data to another class (written in C, not by me) that will do some analysis.
I spent the whole day browsing and reading, and looks like I need use Audio Queues in order to access the buffer.
The problem is that the syntax is C, and I don't understand it at all :)
So my questions are:
1) Is there any other way to achieve what I'm looking for? I don't need in-depth explanation, just some hints and I will browse my way through :) I'm asking because I'm not 100% sure that Audio Queues are the only way to go
2) Any good tutorial or example about Audio Queues? The aurioTouch tutorial by Apple wasn't very useful (again, I don't know C). I could bypass my problems in C by following a good tutorial that a noob like me can understand
Thanks a lot, and for any help you could offer.
Good question.
You can use code written by other people like:
Novocaine - pretty straightforward. (but there are some bugs, at least in older version I used ~ 6 months ago. Something with mono and stereo.)
Momu - quite a good thing in C++ (you need to use .mm extension for you files)
Those will save you time if you want some low level audio programming. Some basic skills in C still required though. Check out this guy. His explanations and enthusiasm are excellent.
With all mentioned above you can be ready in a 1-2 days of work carrying away good skills in C.
EDIT
Basically, everywhere you work with low-level audio you deal with a C array of numbers (represented like float *audioBuffer;) called audio samples. You cycle through it in a loop, do some operations, copy it, send somewhere, analyze.
To copy it you have to allocate space for it. Actual byte size of the buffer can be calculated like this: numberOfSamples*sizeof(type).

Really simple wave synth/table in ios

I want to make a really simple synth.
In short, i want to play a wav file, and have it loop at certain points until touch is released.
I am looking for some example code, (doesn't need to be free).
Sorry for such a basic question, i have been googling this, though there seems to be nothing on this exact topic, unless I'm missing some important term.
Also, is what i'm describing, a wavetable synth, or a soundboard?
I'd call it a sampler.
Here's a sample project that will get you started:
https://sites.google.com/site/iphonecoreaudiodevelopment/remoteio-playback
See also:
The Audio Programming Book
The Core Audio Book
A sample project of mine
You need to store the sound data in memory, and have some sort of read() command that fills an array of bytes to send to the sound card. The read() command has to keep track of its position between reads, so a persistent pointer must be maintained. You will test the position of the pointer and see if you've reached the end or not, and reset to the beginning when needed.
The specifics are going to depend upon your chosen language, of course.
I did this with Java, with the added the possibility of playback at different speeds.
http://www.hexara.com/VSL/VSL2.htm
It's a little laggy. I've learned a bit since posting that, but haven't gone back to fix it yet. The program asks permission and has you load a wav file from your computer. It should be 16-bit, stereo, 44100fps, little-endian.
WaveTable synthesis is a bit different, in that only a single iteration of a wave is stored and used as source data.
Here is a short discussion, from Stanford's CCRMA website:
https://ccrma.stanford.edu/~bilbao/booktop/node9.html
I used this method to make a Java "Theremin"
http://www.hexara.com/VSL/JTheremin.htm
With a WaveTable, you decide on the size of the array. If it is a power of 2, one can bitmask the pointer after every increment, which is faster than doing a comparison and reset.

Resources