Detect frequency value using iPhone - ios

I'm developing an app that should detect a frequency of a certain sound. I based my app on Pitch Detector. I imported the files that are in the Pitch Detector example, then I fixed my code to accept this new classes. I post here my code to explain you my issue:
ViewController.h
#import <UIKit/UIKit.h>
#class RIOInterface;
#interface ViewController : UIViewController {
BOOL isListening;
float currentFrequency;
RIOInterface *rioRef; // HERE I'M GETTING ISSUE
}
- (IBAction)startListenWatermark:(UIButton *)sender;
#property(nonatomic, assign) RIOInterface *rioRef;
#property(nonatomic, assign) float currentFrequency;
#property(assign) BOOL isListening;
#pragma mark Listener Controls
- (void)startListener;
- (void)stopListener;
- (void)frequencyChangedWithValue:(float)newFrequency;
#end
ViewController.m
#synthesize isListening;
#synthesize rioRef;
#synthesize currentFrequency;
- (IBAction)startListenWatermark:(UIButton *)sender {
if (isListening) {
[self stopListener];
} else {
[self startListener];
}
isListening = !isListening;
}
- (void)startListener {
[rioRef startListening:self];
}
- (void)stopListener {
[rioRef stopListening];
}
- (void)frequencyChangedWithValue:(float)newFrequency {
NSLog(#"FREQUENCY: %f", newFrequency);
}
In the code you can see where's my issue and Xcode says: Existing instance variable 'rioRef' with assign attribute must be __unsafe_unretained. If I delete the row that give this errore the app doesn't calls the method [rioRef startListening:self]; and [rioRef stopListening];.
In the file RIOInterface.mm I'm getting another error in line 97 and Xcode suggested me to change it from:
RIOInterface* THIS = (RIOInterface *)inRefCon; --> RIOInterface* THIS = (RIOInterface *)CFBridgingRelease(inRefCon);
The it gives me this other error on the line 283:
callbackStruct.inputProcRefCon = self;
It says me this: Assigning to 'void' from incompatible type 'RIOInterface *const__strong', so I looked to the web and I found this solution:
callbackStruct.inputProcRefCon = (__bridge void*)self;
I'm not sure if it's right to do so or not, I hope you can help me to solve this issues, thank you in advice.

For the 2nd and the 3rd problem I solved by disabling the ARC for the file in which there are the code I provided above. For the first problem I solved by writing this code:
rioRef = [RIOInterface sharedInstance];

Related

Changing Variable in another class iOS

I have follow countless similar situations on here ( stack overflow ). I have applied those write ups for my situation and have come close but haven't found a solution that seems work for me. As this is something very trivial I am baffled at how it isn't working for me.
Anyways, I am trying to set a Int variable from inside the appDelegate class and the variable actually belongs, if that is the right word, to another class (mainViewController).
For the MainViewController class .h file I have
#interface MainWindowControllerViewController :... {
int relayState;
}
#property (readwrite, nonatomic) int relayState;
For the MainViewController class .m file I have
#implementation MainWindowControllerViewController
#synthesize relayState = _relayState;
-(void)quickConnect { // Call for all UI bypassing and quick kill
NSLog(#"Relay state in MainWindow = %d", relayState);
if (_relayState == 1) {
NSLog(#"TURNING ON KILL SWITCH");
self.writeData = [[NSString stringWithFormat:#"%#", #"e"] dataUsingEncoding:NSASCIIStringEncoding];
[sensor write:sensor.activePeripheral data:writeData];
} else {
NSLog(#"TURNING OFF KILL SWITCH");
self.writeData = [[NSString stringWithFormat:#"%#", #"o"] dataUsingEncoding:NSASCIIStringEncoding];
[sensor write:sensor.activePeripheral data:writeData];
}
}
For the appDelegate.h file I have
#import "MainWindowControllerViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
MainWindowControllerViewController *relayState;
}
#property (nonatomic, assign) MainWindowControllerViewController *relayState;
Then for the appDelegate.m file I have
#import "MainWindowControllerViewController.h"
#implementation AppDelegate
#synthesize relayState = _relayState;
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *) sourceApplication annotation:(id)annotation {
NSLog(#" Calling application: %#", sourceApplication);
NSLog(#" URL scheme: %#", [url scheme]);
if ([sourceApplication isEqualToString:#"URL-Scheme-Test"]) {
if ([[url scheme] isEqualToString:#"TurnOffRelay"]) {
_relayState =[[MainWindowControllerViewController alloc] init];
_relayState.relayState = 1;
NSLog(#"Relay State BEFORE = %d", _relayState.relayState);
}
return YES;
if ([[url scheme] isEqualToString:#"TurnOnRelay"]) {
_relayState =[[MainWindowControllerViewController alloc] init];
_relayState.relayState = 0;
NSLog(#"Relay State BEFORE = %d", _relayState.relayState);
}
return YES;
}
else
return NO;
}
This is pretty much it. When I do the logs I find that the relayState that I want to change the value of shows that I was able to change the value. But when I do the If statement above that checks weather the relayState is equal to 1 the variable is always 0.
I'm not the most proficient in Obj C. Some help would be appreciated! Thanks. I'll gladly answer any extra questions about code if needed.
You are kind of mixing some older style obj-c with how it usually looks currently and I think you are hiding a variable (one declaration is hiding another)
Take out:
{
int relayState;
}
From your MainWindowControllerViewController interface
Take out the #synthesize in the MainWindowControllerViewController implementation
Don't use _relayState (just use relayState)
Do the same with the MainWindowControllerViewController variable in the AppDelegate.
If that doesn't fix it, we need to see what is happening to the VC you are creating.
The problem I believe is this line
_relayState =[[MainWindowControllerViewController alloc] init];
You are initializing MainWindowControllerViewController in both the if blocks which is resetting the value to the default value (In this case 0) every time.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MainWindowControllerViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"MainWindowControllerViewControllerIdentifier"];
vc.relayState = 1;
For the MainViewController class .h
#interface MainWindowControllerViewController :... {
int relayState; //remove this line
}
#property (nonatomic) int relayState; //use this
try this
You are making a mistake that will create endless problems for you.
You have a property named relayState and an instance variable named relayState.
The property "relayState" is backed by an instance variable named _relayState. You have another instance variable named relayState. Two different variables. Assigning to self.relayState changes one, assigning to relayState changes the other. Don't do it.
Just get rid of the "int relayState;" and don't use #synthesize and you'll be fine. At least a lot finer than you are now. You can then either use self.relayState or _relayState. At least you will always know what you are doing.
PS. Having an int relayState and a MainWindowViewController* relayState is just asking for trouble. You seem to be intentionally trying to confuse yourself.
PS. You seem to do the same dangerous game with the writeData property/variable.

Can't change "int" in Objective C

I have had a very weird experience with Objective C and xCode these last couple of days. I'm now turning to you guys for some quick help.
I'm simply trying to set up and int, call in damage(the amount of damage this object is supposed to do) and increase it if a void function is called.
-(void) increaseDagage{
damage = damage + 100;
NSLog(#"%f", damage);
}
I have tried setting the int damage up as a int and also as
#property (nonatomic, assign) float damage;
The problem is that when I print "damage" it hasn't increased...
I also have a function that returns the amount of damage this object does and it returns the wrong value.
I can't figure out why this isn't doing what I want...
I also have an int called health, which is basically the same thing and works fine.
Here's the full class if you want to see that too,
//
// Character.m
// TD
//
// Created by Victor Appelgren on 2014-12-23.
// Copyright (c) 2014 Victor Appelgren. All rights reserved.
//
#import "Character.h"
#import "constants.h"
#import "Level.h"
#import "GameViewController.h"
#interface Character (){
SKSpriteNode *character;
float health;
float maxHealth;
Level *level;
GameViewController *gVC;
BOOL dead;
int damage;
}
//#property (nonatomic, assign) float damage;
#end
#implementation Character
-(id) init {
if (self = [super init]){
damage = 100;
dead = NO;
// Load health
maxHealth = 200;
health = 200.0;
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:character.frame.size.width / 2];
[self addChild:character];
}
return self;
}
-(void) doDamageType:(int)type{ // here a type is passed in to tell how much damage is being done.
if (type == 1){
health = health - 20;
} else if (type == 2){
health = health - 40;
}
if (health <= 0){
[self removeFromParent];
dead = YES;
}
}
-(void) increaseDagage{
damage = damage + 100.0;
// NSLog(#"%f", _damage);
}
-(int) returnDamage{
return damage;
}
-(BOOL) returnDead{
return dead;
}
-(float) returnHealth{
return health;
}
-(float) returnMaxHealth{
return maxHealth;
}
#end
Here's the output I'm getting,
2015-02-22 01:28:27.722 TD[279:43180] 200.000000
2015-02-22 01:28:30.327 TD[279:43180] 200
2015-02-22 01:28:30.496 TD[279:43180] 200
2015-02-22 01:28:30.644 TD[279:43180] 200
2015-02-22 01:28:30.809 TD[279:43180] 200
first is initial value and the rest is from when the function is being called
What is wrong....
I might be missing something here but can't seem to find the problem
Thanks for the help
The problem is that you've created two different damage variables -- one in your .h and another in your .m. The one in your .h is public and accessible from your .m using self.damage. The one you've declared in your .m is private and accessible simply using damage. So the problem here is that you're accessing the public version of your damage from the other class, but you're actually manipulating the private version within your class.
So I'd recommend changing the following methods and property declarations of your .m as follows:
// ***Remove the private declaration of damage***
#interface Character (){
SKSpriteNode *character;
float health;
float maxHealth;
Level *level;
GameViewController *gVC;
BOOL dead;
}
#end
#implementation Character
// ***And add "self." before each instance of damage***
-(id) init {
if (self = [super init]){
self.damage = 100;
dead = NO;
// Load health
maxHealth = 200;
health = 200.0;
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:character.frame.size.width / 2];
[self addChild:character];
}
return self;
}
-(void) increaseDagage{
self.damage = self.damage + 100.0;
// NSLog(#"%f", self.damage);
}
-(int) returnDamage{
return self.damage;
}
#end
You have a spelling error in your method name : increaseDagage
I am guessing this should be increaseDamage
You also seem to be creating your own setters and getters. I would read up on the usage of properties.
Several things jump out:
Your method is called increaseDagage. Do you get any error message in Xcode's console when you call it?
You are not telling us whether you are getting any error or warning messages from the code. You should be getting some.
You keep talking about ints, but the log statement and property you define are floats.
You are not #synthesizeing your float damage property. So that means that it will create its own _damage instance variable to store the value and not use your damage instance variable (which would be the wrong type anyway).
You are logging _damage, not damage.
In short, the code, as written, mixes up two completely different things.

ARC semantic issue "multiple methods named 'setRotation' " while archiving only

My project in cocos2dv3 is throwing ARC Sematic Issue
Multiple methods named 'setRotation:' found with mismatched result, parameter type or attributes
while archiving(release mode). It runs fine while deploying to simulator/device (debug mode).
In release mode compiler gets confused between the implementation of rotation in UIRotationGestureRecognizer and CCNode.
When I got the error in CCBAnimationManager.m , I typecasted the object calling the selector setRotation to (CCNode*) but then the error crept up in CCActionInterval. I'm hoping there is a better solution than typecasting everywhere in cocos2d library.
What am i doing wrong?
Thankyou for your time.
EDIT
#interface CCAction : NSObject <NSCopying> {
id __unsafe_unretained _originalTarget;
id __unsafe_unretained _target;
NSInteger _tag;
}
#property (nonatomic,readonly,unsafe_unretained) id target;
#property (nonatomic,readonly,unsafe_unretained) id originalTarget;
#property (nonatomic,readwrite,assign) NSInteger tag;
in
CCAction.m
#synthesize tag = _tag, target = _target, originalTarget = _originalTarget;
-(void) startWithTarget:(id)aTarget
{
_originalTarget = _target = aTarget;
}
-(void) startWithTarget:(id)aTarget
{
_originalTarget = _target = aTarget;
}
Class Hierarchy
#interface CCActionFiniteTime : CCAction <NSCopying>
#interface CCActionInterval: CCActionFiniteTime <NSCopying>
#interface CCBRotateTo : CCActionInterval <NSCopying>
CCBRotateTo.m {
-(void) startWithTarget:(CCNode *)aTarget
{
[super startWithTarget:aTarget];
startAngle_ = [self.target rotation];
diffAngle_ = dstAngle_ - startAngle_;
}
-(void) update: (CCTime) t
{
[self.target setRotation: startAngle_ + diffAngle_ * t];
}
}
This problem gave me a big headache. Though I've upgraded cocos2d to v2.2 version for my old project (too complex to update to v3), I still got this warning. And any animation I created use rotation in the SpriteBuilder does act oddly, as I described here:
Rotation animation issue on iPhone5S with cocos2d 2.0
Finally I used type casting to solve it as following in CCBAnimationManager.m
#implementation CCBRotateTo
-(void)startWithTarget:(CCNode *)aTarget
{
[super startWithTarget:aTarget];
starAngle_ = [(CCNode *)self.target rotation];
diffAngle_ = dstAngle_ - startAngle_;
}
-(void)update:(ccTime)t
{
[(CCNode *)self.target setRotation: startAngle_ + diffAngle_ * t];
}
With this change, now I can support arm64 too.
update your cocos2dv3 to latest (RC4 for now).
I was using Xcode 5.0 and cocos2dv3 RC1 with no problem.
But updating Xcode to 5.1 I had this problem.
So I updated the cocos2dv3 to RC4 and now it's working fine.
You can find cocos 2d latest versions from here.

GPUImage failing the CVPixelBufferGetPlaneCount check on iOS

I'm currently bringing a legacy project up from iOS 5/6 to iOS 6/7.
Part of this project involves taking a picture using the GPUImage library, processing it with a crop filter, then optionally adding some saturation and blur effects. I am currently using version 0.1.2 installed via cocoa pods.
The problem I am having is that when I try to capture an image from the camera, I hit the following assert in GPUImageStillCamera.m line 254
if (CVPixelBufferGetPlaneCount(cameraFrame) > 0)
{
NSAssert(NO, #"Error: no downsampling for YUV input in the framework yet");
}
where cameraFrame is a CVImageBufferRef
I have reproduced the code where this is called and move it to another project, where it works perfectly.
Once I moved this reproduced class back into the main project, I was hitting the assert every time.
Things I've ruled out with my own debugging
64bit (it’s happening on both)
different lib version
initial object setup / code / usage
This has lead me to believe that perhaps it might be a project setting that I've over looked. Any help or even a pointer in the right direction would be very very welcome. I've spent a good 1-2 days on this now and am still entirely lost!
I've included the stripped down class below which shows the general use.
#import "ViewController.h"
#import "GPUImage.h"
#import "ImageViewController.h"
#interface ViewController ()
#property (nonatomic, strong) IBOutlet GPUImageView *gpuImageView;
#property (nonatomic, strong) GPUImageStillCamera *camera;
#property (nonatomic, strong) GPUImageCropFilter *cropFilter;
#end
#implementation ViewController
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self setupCameraCapture];
}
- (void)setupCameraCapture
{
if (self.camera) {
return;
}
self.cropFilter = [[GPUImageCropFilter alloc] initWithCropRegion:CGRectMake(0, 0, 1, 0.5625)];
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
self.camera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPresetPhoto cameraPosition:AVCaptureDevicePositionBack];
}
else {
self.camera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPresetPhoto cameraPosition:AVCaptureDevicePositionFront];
}
self.camera.outputImageOrientation = UIInterfaceOrientationPortrait;
NSError *error = nil;
[self.camera.inputCamera lockForConfiguration:&error];
[self.camera.inputCamera setExposureMode:AVCaptureExposureModeContinuousAutoExposure];
[self.camera.inputCamera setWhiteBalanceMode:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance];
if ([self.camera.inputCamera respondsToSelector:#selector(isLowLightBoostSupported)]) {
BOOL isSupported = self.camera.inputCamera.isLowLightBoostSupported;
if (isSupported) {
[self.camera.inputCamera setAutomaticallyEnablesLowLightBoostWhenAvailable:YES];
}
}
[self.camera.inputCamera unlockForConfiguration];
[self.camera addTarget:self.cropFilter];
[self.cropFilter addTarget:self.gpuImageView];
[self.camera startCameraCapture];
}
- (IBAction)capturePressed:(id)sender
{
[self.camera capturePhotoAsImageProcessedUpToFilter:self.cropFilter withCompletionHandler:^(UIImage *image, NSError *error) {
// do something with the image here
}];
}
#end
The actual culprit was a swizzled method found by my colleague Marek. Hidden away in the depths of the old codebase. The above code works fine.
Lesson: if you really have to swizzle something, make sure you leave proper documentation for the future devs.

Variable lost after dismissing modalViewController

I'm using Reachability in my iPad app and discovered some issues when using modalViewControllers.
In my mainViewController I have a BOOL variable determining weather I'm online or not. Here's my code:
// mainViewController.h
BOOL online;
// mainViewController.m
- (void)reachabilityChanged:(NSNotification *)note
{
if([[note object] isReachable]) {
online = YES;
}
else {
online = NO;
}
}
- (void)getOnline
{
NSLog(#"%d", online);
}
// modalViewController.m
#import "mainViewController.h"
- (IBAction)dismissMe
{
mainViewController *main = [[mainViewController alloc] init];
[main getOnline];
[self dismissModalViewControllerAnimated:YES];
}
When I'm calling [self getOnline] within the mainViewController, it returns 1 ('cause I am online).
But: when I'm calling [main getOnline] within the modalViewController, it returns 0 in the log.
Does anybody know why?!
I also tried to put the online variable as a #property into the modalViewController to handle the if online stuff within the modal. But when I assign a value to it (from the main), and log it within the modal, it always returns (NULL).
Hope, you can help me! With best regards, Julian
Short answer: because they use different instances of the online variable.
Long answer: you should only declare BOOL online in the header, not define it. Defining should happen in the .m file, like this:
In the mainViewController.h:
extern BOOL online; // Declare the variable
In the mainViewController.m:
BOOL online; // Define the variable
// the rest of your code
The way your code is written, a separate BOOL online is created for each .m file that includes mainViewController.h; I am sure this is not what you intended.

Resources