I making an app that uses EZAudio library, and it have to get data from the microphone and store it in variable (i already have done this) :
func microphone(microphone: EZMicrophone!,
hasAudioReceived buffer: UnsafeMutablePointer<UnsafeMutablePointer<Float>>,
withBufferSize bufferSize: UInt32,
withNumberOfChannels numberOfChannels: UInt32)
{
let new = (buffer[0].memory)
audioStack.append(new)
}
As you can see, i`m using EZMicrophoneDelegate, and func above, to get buffer data from microphone.
The future idea is to send the values by WebSocket and play immediately one by one.
I want playback that array, using EZOuput and EZOuputDataSource`s method:
func output(output: EZOutput!,
shouldFillAudioBufferList audioBufferList: UnsafeMutablePointer<AudioBufferList>,
withNumberOfFrames frames: UInt32,
timestamp: UnsafePointer<AudioTimeStamp>) -> OSStatus
{
if audioStack.count > 0 {
let data = audioBufferList.memory.mBuffers.mData
// "data" = is pointer to mData, that i want to fill with float
// and I have do it here
return noErr
}
But the type of "mData" is UnsafeMutablePointer, and I don`t know how to fill it, I saw some examples in objective-C like this, exactly this line:
Float32 *buffer = (Float32 *)audioBufferList->mBuffers[0].mData;
and I can`t understand how I have to store my Float type from array to mData with Void type, in swift?
Casting like:
(audioBufferList.memory.mBuffers.mData) as Float = audioStack[0]
fails..
Looks like i found how to cast an UnsafeMurtablePointer to UnsafeMutablePointer :
let data = UnsafeMutablePointer<Float>(audioBufferList.memory.mBuffers.mData)
Related
I'm working on a Swift 3 project that involves using some C APIs that I bridged from Objective-C.
Here is a sample snippet of the structure of the API:
typedef struct
{
StructMode mode;
StructLevel level;
} TargetStruct;
typedef struct
{
. . .
TargetStruct *targetStruct;
OtherStruct *otherStruct;
NonPointerStructA nonPointerStructA;
NonPointerStructB nonPointerStructB;
. . .
} InnerStruct;
typedef struct
{
InnerStruct innerStruct;
OtherStructB otherStructB;
} OuterStruct;
In my Swift code, my goal is to set a value of the TargetStruct from the OuterStruct, like the following:
// run function that returns an instance of TargetStruct
var targetStruct: TargetStruct = initializeTargetStruct()
// assign targetStruct to outerStruct
outerStruct.innerStruct.targetStruct = &targetStruct
However, I am getting the following error:
Cannot pass immutable value of TargetStruct as inout argument
If I set a value of a struct without the *, it will work fine:
var nonPointerStructA: NonPointerStructA = initializeNonPointerStructA()
outerStruct.innerStruct.nonPointerStructA = nonPointerStructA
I have tried setting the value of targetStruct like this, but for now I have no way to test it:
var targetStruct: TargetStruct = initializeTargetStruct()
outerStruct.innerStruct.targetStruct.initialize(from: &targetStruct, count: 0)
How to solve this problem? Thank you.
In Swift, prefix & is not an address-of operator. It is just needed to clarify that some expression is passed to an inout parameter. So, your first code is syntactically invalid in Swift.
Your C-structs are imported to Swift as follows:
struct TargetStruct {
var mode: StructMode
var level: StructLevel
//some auto generated initializers...
}
struct InnerStruct {
//...
var targetStruct: UnsafeMutablePointer<TargetStruct>!
var otherStruct: UnsafeMutablePointer<OtherStruct>!
var nonPointerStructA: NonPointerStructA
var nonPointerStructB: NonPointerStructB
//some auto generated initializers...
}
struct OuterStruct {
var innerStruct: InnerStruct
var otherStructB: OtherStructB
//some auto generated initializers...
}
(If something wrong, please tell me.)
As you see, targetStruct in your InnerStruct is a pointer, and initialize(from:count:) tries to write to the pointed region, but at the time you call initialize(from:count:), targetStruct holds its initial value nil, you know what happens when dereferencing null-pointer.
One way is to allocate a memory for the TargetStruct and use the pointer to the allocated region.
func allocateAndInitializeTargetStruct() -> UnsafeMutablePointer<TargetStruct> {
let targetStructRef = UnsafeMutablePointer<TargetStruct>.allocate(capacity: 1)
targetStructRef.initialize(to: initializeTargetStruct())
return targetStructRef
}
outerStruct.innerStruct.targetStruct = allocateAndInitializeTargetStruct()
This is a more general way than below, but you need to explicitly deinitialize and deallocate the allocated region. That's sort of hard to manage.
If you can confine the usage of the outerStruct in a single code-block, you can write something like this:
var targetStruct = initializeTargetStruct()
withUnsafeMutablePointer(to: &targetStruct) {targetStructPtr in
outerStruct.innerStruct.targetStruct = targetStructPtr
//Use `outerStruct` only inside this code-block
//...
}
In this case, the pointer held in outerStruct.innerStruct.targetStruct (== targetStructPtr) is only valid inside the closure and you cannot use it outside of it.
If any of the codes above does not fit for your use case, you may need to provide more context to find the best solution.
An example of nested use of withUnsafeMutablePointer(to:_:):
var targetStruct = initializeTargetStruct()
var otherStruct = initializeOtherStruct()
withUnsafeMutablePointer(to: &targetStruct) {targetStructPtr in
withUnsafeMutablePointer(to: &otherStruct) {otherStructPtr in
outerStruct.innerStruct.targetStruct = targetStructPtr
outerStruct.innerStruct.otherStruct = otherStructPtr
//Use `outerStruct` only inside this code-block
//...
}
}
When you need more pointers to set, this nesting would be a mess, but it's the current limitation of Swift.
An example of deinitialize and deallocate:
extension InnerStruct {
func freeMemberStructs() {
if let targetStructRef = targetStruct {
targetStructRef.deinitialize()
targetStructRef.deallocate(capacity: 1)
targetStruct = nil
}
if let otherStructRef = otherStruct {
otherStructRef.deinitialize()
otherStructRef.deallocate(capacity: 1)
otherStruct = nil
}
}
}
outerStruct.innerStruct.targetStruct = allocateAndInitializeTargetStruct()
outerStruct.innerStruct.otherStruct = allocateAndInitializeOtherStruct()
// Use `outerStruct`
//...
outerStruct.innerStruct.freeMemberStructs()
The code may not seem to be too complex (just a bunch of boilerplate codes), but it's hard to find when or where to do it. As your InnerStruct may be embedded in another struct which may need to be deinitilized and deallocated...
Hope you can find your best solution.
I am looking for a way to determine the position and dimension of a window that is not part of my application in macOS using Swift (prefered) or Objective-C to program an overlay/hud.
Is it possible to read a list of all processes and start from there or is there something similar to the getWindowHandle() function in the Windows api?
Take a look at CGWindowListCopyWindowInfo:
import CoreGraphics
if let windowList = CGWindowListCopyWindowInfo([.optionAll], kCGNullWindowID) as? [[String: AnyObject]] {
for window in windowList {
let number = window[kCGWindowNumber as String]!
let bounds = CGRect(dictionaryRepresentation: window[kCGWindowBounds as String] as! CFDictionary)!
let name = window[kCGWindowName as String] as? String ?? ""
print("number = \(number), name = \(name), bounds = \(bounds)")
}
} else {
print("Can't get window list")
}
The function returns an array of CFDictionary, which the code above bridged into [String: AnyObject] for easy working in Swift. Here are required and optional keys for the dictionaries. The keys are all defined as CFString you must bridge them into String.
please help with syntax:
__weak typeof (self) weakSelf = self;
[self.audioFile getWaveformDataWithCompletionBlock:^(float **waveformData,
int length)
{
[weakSelf.audioPlot updateBuffer:waveformData[0]
withBufferSize:length];
}];
The waveform data itself will be an array of float arrays, one for each channel, and the length indicates the total length of each float array.
#param waveformData An array of float arrays, each representing a channel of audio data from the file
#param length An int representing the length of each channel of float audio data
in swift I have:
cell.audioFile.getWaveformDataWithCompletionBlock { (UnsafeMutablePointer<UnsafeMutablePointer<Float>>, Int32) -> Void
}
I stuck on UnsafeMutablePointer>
I need to use this arg. in:
cell.audioWaveView.updateBuffer(buffer: UnsafeMutablePointer, withBufferSize: Int32)
I know this may be an old question, but I was struggling with the same thing and I finally solved:
You need to pass the argument to the block as a WaveformDataCompletionBlock closure, and your parameters should be an UnsafeMutablePointer and a UInt32. So the code should be something like this:
self.audioFile = EZAudioFile(URL: self.soundFileURL)
var waveClosure: WaveformDataCompletionBlock = {
(waveForData: UnsafeMutablePointer<Float>, length: UInt32) in
//Do something
}
self.audioFile.getWaveformDataWithCompletionBlock(waveClosure)
I hope this could be useful to somebody :)
Similar to what #FelipeDev.- posted, this question is old but still helped me today (Thanks Felipe!). However, there is a slightly updated answer to this now with the newer versions of EZAudio and Swift. I got this to work with the following code:
var waveClosure: EZAudioWaveformDataCompletionBlock = {
(waveformData: UnsafeMutablePointer<UnsafeMutablePointer<Float>>, length: Int32) in
//Do something like update the audio plot buffer if you are plotting the waveform
self.audioPlot.updateBuffer(waveformData[0], withBufferSize: UInt32(length))
}
self.audioFile.getWaveformDataWithCompletionBlock(waveClosure)
Hope this helps!
Fellow Devs,
I'm trying to implement a polygon overlay on a mapview as follows:
private func drawOverlayForObject(object: MyStruct) {
if let coordinates: [CLLocationCoordinate2D] = object.geometry?.coordinates {
let polygon = MKPolygon(coordinates: coordinates, count: coordinates.count)
self.mapView.addOverlay(polygon)
}
}
The following error is presented:
Missing argument for parameter 'interiorPolygons' in call
According to the documentation:
Apple Docu:
Mutable Pointers
When a function is declared as taking an UnsafeMutablePointer
argument, it can accept any of the following:
nil, which is passed as a null pointer
An UnsafeMutablePointer value
An in-out expression whose operand is a stored lvalue of type Type, which is passed as the address of the lvalue
An in-out [Type] value, which is passed as a pointer to the start of the array, and lifetime-extended for the duration of the call
Now I think that my approach then would be correct, providing a [CLLocationCoordinate2D] array. Did anyone experience the same problem and found a workaround?
thanks
Ronny
The error you're getting is Swift's cryptic way of saying that it can't find a method which matches your parameters. If you did try passing the interiorPolygons parameter, you'd get an equally confusing:
Extra argument 'interiorPolygons' in call
Your code is pretty close though; you just need a couple of minor changes. In the doc you reference, it says one of the things you can pass is:
An in-out [Type] value, which is passed as a pointer to the start of
the array, and lifetime-extended for the duration of the call
So, it's looking for an in-out parameter. Which is done by passing coordinates prefixed with an &, like so:
MKPolygon(coordinates: &coordinates, count: coordinates.count)
But, in-out parameters can't be constants. From the docs:
You can only pass a variable as the argument for an in-out parameter.
You cannot pass a constant or a literal value as the argument, because
constants and literals cannot be modified.
So, you need to define coordinates with a var first:
if var coordinates: [CLLocationCoordinate2D] = object.geometry?.coordinates
Which makes the entire function look like this:
private func drawOverlayForObject(object: MyStruct) {
if var coordinates: [CLLocationCoordinate2D] = object.geometry?.coordinates {
let polygon = MKPolygon(coordinates: &coordinates, count: coordinates.count)
self.mapView.addOverlay(polygon)
}
}
My final solution by cherry-picking from several tutorials and integrating:
func setPolylineFromPoints(locations:[CLLocation]){
if locations.count == 0 {
return;
}
// while we create the route points, we will also be calculating the bounding box of our route
// so we can easily zoom in on it.
var pt : UnsafeMutablePointer<MKMapPoint>? // Optional
pt = UnsafeMutablePointer.alloc(locations.count)
for idx in 0..<locations.count-1 {
let location = locations[idx]
let point = MKMapPointForCoordinate(location.coordinate);
pt![idx] = point;
}
self.polyline = MKPolyline(points:pt!, count:locations.count-1)
// clear the memory allocated earlier for the points
pt?.destroy()
pt?.dealloc(locations.count)
}
When I return a direct ByteBuffer to JNI, how long until it can get reclaimed by the JVM/GC?
Suppose I have a function like this:
void* func()
{
[ ... ]
jobject result = env->CallStaticObjectMethod(testClass, doSomethingMethod);
void* pointerToMemory = env->GetDirectBufferAddress(result);
return pointerToMemory;
}
The JVM can't possibly know how long I'm going to use that pointerToMemory, right? What if I want to hold on to that address and the corresponding memory for a while?
Suppose I want to circumvent this issue and return a byte[] from Java to JNI like this:
ByteBuffer buf;
byte[] b = new byte[1000];
buf = ByteBuffer.wrap(b);
buf.order(ByteOrder.BIG_ENDIAN);
return buf.array();
AND THEN do the same as above, I store a pointer to that byte[] and want to hold on to it for a while. How / when / why is the JVM going to come after that backing byte[] from Java?
void* function()
{
jbyteArray byteArr = (jbytearray)env->CallStaticObjectMethod(testClass, doSomethingMethod);
jbyte *b= env->GetByteArrayElements(byteArr, 0);
return b;
}
The short answer is: If the function implements a native method, the pointer will be invalid as soon as you return.
To avoid this, you should get a global reference for all objects that you intend to keep valid after returning. See the documentation on local and global references for more information.
To understand better how JNI manages references from native code, see the documentation on PushLocalFrame/PopLocalFrame.