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!
Related
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)
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)
}
I am a developer in C-like languages (Java/JavaScript/C#) and I am attempting to convert some Objective-C code into Java.
For the most part, it is relatively straightforward but I have hit a stumbling block with the following bit of code:
typedef struct {
char *PAGE_AREA_ONE;
char *PAGE_AREA_TWO;
char *PAGE_AREA_THREE;
} CODES;
- (CODES*) getOpCode {
CODES *result = NULL;
result = malloc(sizeof(CODES));
result->PAGE_AREA_ONE = "\x1b\x1b\x1b";
result->PAGE_AREA_TWO = "\x2d\x2d\x2d";
result->PAGE_AREA_THREE = "\x40\x40";
return result;
}
What would the Java equivalent of this be? From what I can tell in other areas of the code, it is being used to store constants. But I am not 100% certain.
Thanks.
The typedef is just creating a structure that contains three string properties. The getOpCode method is apparently trying to create a new structure and assign values to those three properties. C# code would be:
public class Codes
{
public string PageAreaOne;
public string PageAreaTwo;
public string PageAreaThree;
}
public Codes GetCodes()
{
Codes result = new Codes();
result.PageAreaOne = "\x1b\x1b\x1b"; // three ESC characters
result.PageAreaTwo = "---";
result.PageAreaThree = "##";
return result;
}
The code in question is allocating a block of memory that the size of the CODES structure, filling it with some data, and returning a pointer to the new block. The data is apparently some operation codes (that is, instructions) for something, so perhaps the data is being sent to some other device where the instructions will be executed.
I have two objective c methods. One needs to return an int[][] and the other which needs to take int[][] as a parameter. I was originally using an NSMutableArray with NSMutableArrays as values however I was told to redo it like this in order to be compatible with some current code. I can't figure out how to make this work. I'm not sure I'm even googling the right thing. Anyway here is what I have now.
+(int [][consantValue]) getCoefficients
{
int coefficiennts [constantValue2][constantValue1] = { {0,1,2}, {3,4,5}, {6,7,8} };
return coefficients;
}
At the return statement I get the Error "Array initilizer must be an initializer list'
I also have to take the int[][] and rebuild it into an NSMutableArray of NSMutableArrays in another method but I'm hoping if someone can give me a hint on the first part I can work the second part out myself although if anyone has any advice on that I would appreciate it as well. Thanks.
The easy way to do this for fixed size array(s) is to use a struct for storage:
typedef struct {
int at[constantValue2][constantValue1];
} t_mon_coefficients;
And then you'd declare the method which returns by value:
+ (t_mon_coefficients)coefficients;
And passes by value as a parameter:
- (void)setCoefficients:(const t_mon_coefficients)pCoefficients;
If the struct is large, you should pass by reference:
// you'd use this like:
// t_mon_coefficients coef;
// [SomeClass getCoefficients:&coef];
+ (void)getCoefficients:(t_mon_coefficients* const)pOutCoefficients;
- (void)setCoefficients:(const t_mon_coefficients*)pCoefficients;
But there are multiple ways one could accomplish this.
Let's say I have an array containing Blocks, and I need to assert that all of them expect a given number of arguments.
Is there a way to find this out programmatically?
This is indeed possible, for any recent version of Clang.
The Apple ABI for Blocks is private but also published. Since that document tells us the layout the compiler will use for a Block object, we can duplicate that information in a header file and use it to access the components of a Block.
Mike Ash's MABlockForwarding project does just that (see also the article) -- much of the stuff at the top of this file is a copy-paste from the ABI doc. The thing that he created which we are interested in is the BlockSig() function:
static const char *BlockSig(id blockObj)
{
struct Block *block = (__bridge void *)blockObj;
struct BlockDescriptor *descriptor = block->descriptor;
assert(block->flags & BLOCK_HAS_SIGNATURE);
int index = 0;
if(block->flags & BLOCK_HAS_COPY_DISPOSE)
index += 2;
return descriptor->rest[index];
}
which will return (for Blocks that have it (which they all do with recent Clang)), a type encoding string describing the Block's return and argument types. From there, you can create an NSMethodSignature object, and ask it for its numberOfArguments:
NSString * (^block)(int, NSArray *) = ^NSString * (int i, NSArray * a){
return #"Oh, yeah!";
};
const char * types = BlockSig(block);
NSMethodSignature * sig = [NSMethodSignature signatureWithObjCTypes:types];
[sig numberOfArguments];
The result there is 3, because it includes a hidden argument for the Block itself (and Blocks don't use the hidden _cmd argument or it would be 4).
The answer is you cannot. See the comment on Mike Ash's page regarding this:
Search for Intropection which sends you here
So, what is your real problem? If you structure the arguments properly, you can insure that your system functions properly. For instance, you can do what C++ does with default values for arguments, and cast each block to a type that takes the max number of args, and always push that many items on the stack. Or you could always have the first argument be the number of arguments you are pushing on the stack. If you push objects and not numbers/pointers, then you r blocks can look at the class of each argument and dynamically adapt.