I'm working on an audio debug feature and trying to get the AudioUnit.AudioUnitPropertyIDType.Latency property value of my audio unit using Xamarin.iOS. Unfortunately I don't see related method to retrieve the value property value like audioUnit.GetParameter.
Though I can see and successfully set properties using audioUnit.SetParameter method.
Did I miss something?
Not an answer but AudioUnit has private AudioUnitSetProperty method which is exposed as SetAudioFormat, SetCurrentDevice and a number of other methods, but not for Latency. Looks like it wasn't implemented by Xamarin. But you always can use reflection ;)
public void SetAudioFormat(AudioStreamBasicDescription audioFormat, AudioUnitScopeType scope, uint audioUnitElement = 0U)
{
int k = AudioUnit.AudioUnitSetProperty(this.handle, AudioUnitPropertyIDType.StreamFormat, scope, audioUnitElement, ref audioFormat, (uint) Marshal.SizeOf<AudioStreamBasicDescription>(audioFormat));
if (k != 0)
throw new AudioUnitException(k);
}
Related
Learning Dart and using dart_code_metrics to ensure that I write code that meets expectations. One of the rules that is active is avoid-non-null-assertion.
Note, the code below was created to recreate the problem encountered in a larger code base where the value of unitString is taken from a JSON file. As such the program cannot control what is specified in the JSON file.
From pubspec.yaml
environment:
sdk: '>=2.15.0 <3.0.0'
// ignore_for_file: avoid_print
import 'package:qty/qty.dart';
void main() {
const String unitString = 'in';
// unit.Width returns null if unitString is not a unit of Length.
if (Length().unitWith(symbol: unitString) == null) {
print('units $unitString not supported.');
} else {
// The following line triggers avoid-non-null-assertion with the use of !.
final Unit<Length> units = Length().unitWith(symbol: unitString)!;
final qty = Quantity(amount: 0.0, unit: units);
print('Qty = $qty');
}
}
If I don't use ! then I get the following type error:
A value of type 'Unit<Length>?' can't be assigned to a variable of type 'Unit<Length>'.
Try changing the type of the variable, or casting the right-hand type to 'Unit<Length>'.
Casting the right-hand side to
Unit<Length>
fixes the above error but cause a new error when instantiating Quantity() since the constructor expects
Unit<Length>
and not
Unit<Length>?
I assume there is an solution but I'm new to Dart and cannot formulate the correct search query to find the answer.
How can I modify the sample code to make Dart and dart_code_metrics happy?
Your idea of checking for null before using a value is good, it's just not implemented correctly. Dart does automatically promote nullable types to non-null ones when you check for null with an if, but in this case you need to use a temporary variable.
void main() {
const String unitString = 'in';
//Use a temp variable, you could specify the type instead of using just using final
final temp = Length().unitWith(symbol: unitString);
if (temp == null) {
print('units $unitString not supported.');
} else {
final Unit<Length> units = temp;
final qty = Quantity(amount: 0.0, unit: units);
print('Qty = $qty');
}
}
The basic reason for that when you call your unitWith function and see that it's not null the first time, there's no guarantee that the when you call it again that it will still return a non-null value. I think there's another SO question that details this better, but I can't seem to find.
I'm new to Vala and trying to understand how the language works. I usually use script languages like Python or JavaScript.
So, my question is why are there three ways of class constructor definition and how does the GObject style constructor work?
For the best understanding lets make an analogy with python:
Python class definition
class Car(object):
speed: int
def __init__(self, speed): # default constructor
self.speed = speed # property
And Vala
class Car : GLib.Object {
public int speed { get; construct; }
// default
internal Car(int speed) {
Object(speed: speed)
}
construct {} // another way
}
I was reading the Vala tutorial section about GObject style construction, but still do not understand how Object(speed: speed) works and for what construct is needed?
Vala was developed as a replacement for the manual effort needed to write GLib based C code.
Since C has no classes in GLib based C code object construction is done in a different way than in say C# or Java.
Here is a fragment of the output of valac -C car.vala for your example code:
Car*
car_construct (GType object_type,
gint speed)
{
Car * self = NULL;
self = (Car*) g_object_new (object_type, "speed", speed, NULL);
return self;
}
So Vala emits a car_construct function that calls the g_object_new () method. This is the GLib method used to create any GLib based class, by passing its type and construct parameters by name and value arguments one after another, terminated by NULL.
When you don't use construct properties it won't be possible to pass parameters via g_object_new () and you'd have to call the setter, for example:
Car*
car_construct (GType object_type,
gint speed)
{
Car * self = NULL;
self = (Car*) g_object_new (object_type, NULL);
car_set_speed (self, speed);
return self;
}
Here car_set_speed () is called instead of passing the value via g_object_new ().
Which one you prefer depends on a few factors. If you do interop with C code often and the C code uses construct parameters, you want to use GObject style construction. Otherwise you are probably fine with the C#/Java style constructors.
PS: The setter is also auto generated by valac and will not only set the property value, but notify any listeners through the g_object_notify () system.
I noticed over and over again that if i did something like:
#property int x = 1;
...code...
set("x", 2);
print(x);
it will show it as 1. The reason was that it doesnt necessarily execute this immediately. So if i ever set a property I have always made it a point to never use it through the rest of the function. I always believed that set was just called at the end of the current execution.
When dealing with functions a similar approach happens.
It will be assigned but outside the scope of the function. So i would try something like observing it and awaiting for a change in state.
#property Function myFunc = null;
#reflectable
_myFunc(_) => true;
attached(){
set("myFunc", _myFunc);
print("is myFunc null: ${myFunc == null}");
}
will return True.
So I then would also try:
#Observe("myFunc")
functionObservation(_)=>print("Function Called");
but this would not fire.
My desired end state is that when i pass myFunc into another polymer element and try to do stuff with it on that class' attached, as such:
#property Function execFunc = null;
attached(){
if(execFunc != null)
execFunc();
}
so when passing it into another component there are issues.
I am not 100% sure if this is a life cycle issue, or a set error but it seems that when i do it in attached, OR define a future:
attached(){
new Future((){
execFunc()
});
}
It will still not seem to be assigned.
you should rewrite your props as setters and getters and use the setter for notification :
int _myProp;
#reflectable
int get myProp => _myProp;
set myProp(v) {
_myProp = v;
notifyPath('myProp');
}
Or use something like autonotify to automatically get properties notified for you.
This way you are guaranteed that this code always stamp 5 in the console:
myProp=5
print("${myProp}");
regardless the possible async polymer setter behavior.
Is there an easy way to create a new pthread each time a method is called?
I have a method activating in certain circumstances and it is the only way to commumicate with another program. I need to engage sleep and execute another method after said method is called, there is an option of another call during waiting - this is the reason i wanted to use threads.
I wanted to use standard:
pthread_create(&new_thread, NULL, threadbody() );
Put like this
std::vector<phtread_t> thread(20)
...
pthread_t new_thread;
int rc;
rc = pthread_create(&new_thread, NULL, threadbody() );
threads.push_back(new_thread);
But i either get the errors of bad using of (void *) functions.
argument of type ‘void* (App::)(void*)’ does not match ‘void* (*)(void*)
What am I doing wrong?
Your function is a non-static member of the class. This is a no-no since pthread_create is only meant to work in the C realm.
To offset this, you can have
class X
{
public:
static void* callback(void* p)
{
X* x = reinterpret_cast<X*>(p);
x->function();
return 0;
}
void function(void)
{
// do work here
}
};
X obj;
pthread_create(&new_thread, &obj, &X::callback);
I am trying to save some settings but the tutorial I am following(android tutorial) is not helping as I am stuck on the first line of code since it seems monodroid does it differently?
select your mode to be either private or public.
int mode= Activity.MODE.PRIVATE;
// get the sharedPreference of your context.
SharedPreference s mySharedPreferences ; mySharedPreferences=getSharedPreferences(“Name_of_your_preference”,mode);
// retrieve an editor to modify the shared preferences
SharedPreferences.Editor editor= mySharedPreferences.edit();
/* now store your primitive type values. In this case it is true, 1f and Hello! World */
editor.putBolean(“myBoolean”,true);
editor.putFloat(“myFloat”,1f);
editor.putString(“myString”,” Hello! World”);
//save the changes that you made
editor.commit();
I don't see Activity.MODE.PRIVATE; in monodroid.
Here is my func to do this:
protected void SaveSetting(string name, string value)
{
var prefences = GetSharedPreferences(Consts.Application.SETTINGS_FILE, FileCreationMode.Private);
var editor = prefences.Edit();
editor.Remove(name);
editor.PutString(name, value);
editor.Commit();
}
Assuming you mean MODE_PRIVATE, it should be Android.Content.FileCreationMode.Private.
Fortunately you don't really have to know that, as we mapped the int in GetSharedPreferences to take the Android.Content.FileCreationMode enum, so intellisense should help you out.