From 4f31546fadfef505b0bbff360f4cfec9ec718245 Mon Sep 17 00:00:00 2001 From: Hubert Gancarczyk Date: Fri, 19 Jul 2024 14:31:47 +0200 Subject: [PATCH] refactor: add some errors handling --- .../OscillatorNodeHostObject.cpp | 9 ++++++-- cpp/OscillatorNode/OscillatorNodeWrapper.h | 2 +- ios/nodes/Oscillator/IOSOscillator.h | 2 +- ios/nodes/Oscillator/IOSOscillator.mm | 4 ++-- ios/nodes/Oscillator/OscillatorNode.h | 2 +- ios/nodes/Oscillator/OscillatorNode.m | 4 ++-- ios/nodes/Oscillator/WaveType/WaveType.m | 23 ++++++++++++++----- 7 files changed, 31 insertions(+), 15 deletions(-) diff --git a/cpp/OscillatorNode/OscillatorNodeHostObject.cpp b/cpp/OscillatorNode/OscillatorNodeHostObject.cpp index 7ca9e71e..3e5b6b81 100644 --- a/cpp/OscillatorNode/OscillatorNodeHostObject.cpp +++ b/cpp/OscillatorNode/OscillatorNodeHostObject.cpp @@ -71,8 +71,13 @@ namespace audiocontext if (propName == "type") { - wrapper_->setType(runtime, propNameId, value); - return; + std::string waveType = value.getString(runtime).utf8(runtime); + if (waveType == "sine" || waveType == "triangle" || waveType == "sawtooth" || waveType == "square") { + wrapper_->setType(runtime, propNameId, value); + return; + } + + throw std::runtime_error("You have to pick correct wave type: ['sine', 'square', 'sawtooth', 'trinagle']"); } throw std::runtime_error("Not yet implemented!"); diff --git a/cpp/OscillatorNode/OscillatorNodeWrapper.h b/cpp/OscillatorNode/OscillatorNodeWrapper.h index 4611aba1..9fcc6a61 100644 --- a/cpp/OscillatorNode/OscillatorNodeWrapper.h +++ b/cpp/OscillatorNode/OscillatorNodeWrapper.h @@ -28,7 +28,7 @@ namespace audiocontext { #ifdef ANDROID explicit OscillatorNodeWrapper(std::shared_ptr oscillator) : oscillator_(oscillator) {} #else - explicit OscillatorNodeWrapper() : oscillator_(std::make_shared(440)) {} + explicit OscillatorNodeWrapper() : oscillator_(std::make_shared()) {} #endif jsi::Value getFrequency(jsi::Runtime &runtime, const jsi::PropNameID &propNameId); diff --git a/ios/nodes/Oscillator/IOSOscillator.h b/ios/nodes/Oscillator/IOSOscillator.h index d72a4d13..0edaa4e5 100644 --- a/ios/nodes/Oscillator/IOSOscillator.h +++ b/ios/nodes/Oscillator/IOSOscillator.h @@ -11,7 +11,7 @@ typedef struct objc_object OscillatorNode; namespace audiocontext { class IOSOscillator { public: - explicit IOSOscillator(const float frequency); + explicit IOSOscillator(); void start() const; void stop() const; void changeFrequency(const float frequency) const; diff --git a/ios/nodes/Oscillator/IOSOscillator.mm b/ios/nodes/Oscillator/IOSOscillator.mm index beaab376..977ff30f 100644 --- a/ios/nodes/Oscillator/IOSOscillator.mm +++ b/ios/nodes/Oscillator/IOSOscillator.mm @@ -2,8 +2,8 @@ namespace audiocontext { - IOSOscillator::IOSOscillator(const float frequency) { - OscillatorNode_ = [[OscillatorNode alloc] initWithFrequency:frequency]; + IOSOscillator::IOSOscillator() { + OscillatorNode_ = [[OscillatorNode alloc] init]; } void IOSOscillator::start() const { diff --git a/ios/nodes/Oscillator/OscillatorNode.h b/ios/nodes/Oscillator/OscillatorNode.h index 2a85f838..d2fe8716 100644 --- a/ios/nodes/Oscillator/OscillatorNode.h +++ b/ios/nodes/Oscillator/OscillatorNode.h @@ -12,7 +12,7 @@ @property (nonatomic, assign) float frequency; @property (nonatomic, assign) double sampleRate; -- (instancetype)initWithFrequency:(float)frequency; +- (instancetype)init; - (void)start; diff --git a/ios/nodes/Oscillator/OscillatorNode.m b/ios/nodes/Oscillator/OscillatorNode.m index df13e2de..72da417d 100644 --- a/ios/nodes/Oscillator/OscillatorNode.m +++ b/ios/nodes/Oscillator/OscillatorNode.m @@ -2,9 +2,9 @@ @implementation OscillatorNode {} -- (instancetype)initWithFrequency:(float)frequency { +- (instancetype)init { if (self = [super init]) { - self.frequency = frequency; + self.frequency = 440; self.sampleRate = 44100; self.waveType = WaveTypeSine; diff --git a/ios/nodes/Oscillator/WaveType/WaveType.m b/ios/nodes/Oscillator/WaveType/WaveType.m index 6e1ce788..2be34b1e 100644 --- a/ios/nodes/Oscillator/WaveType/WaveType.m +++ b/ios/nodes/Oscillator/WaveType/WaveType.m @@ -6,16 +6,21 @@ @implementation WaveType + (WaveTypeEnum)waveTypeFromString:(NSString *)type { if ([type isEqualToString:@"sine"]) { return WaveTypeSine; - } else if ([type isEqualToString:@"square"]) { + } + + if ([type isEqualToString:@"square"]) { return WaveTypeSquare; - } else if ([type isEqualToString:@"sawtooth"]) { + } + + if ([type isEqualToString:@"sawtooth"]) { return WaveTypeSawtooth; - } else if ([type isEqualToString:@"triangle"]) { + } + + if ([type isEqualToString:@"triangle"]) { return WaveTypeTriangle; - } else { - NSLog(@"Unknown wave type: %@", type); - return WaveTypeSine; // Default value } + + @throw [NSException exceptionWithName:@"Invalid wave type" reason:@"You have to pick correct wave type: ['sine', 'square', 'sawtooth', 'trinagle']" userInfo:nil]; } + (NSString *)stringFromWaveType:(WaveTypeEnum)waveType { @@ -28,7 +33,10 @@ + (NSString *)stringFromWaveType:(WaveTypeEnum)waveType { return @"sawtooth"; case WaveTypeTriangle: return @"triangle"; + default: + @throw [NSException exceptionWithName:@"Invalid wave type" reason:@"You have to pick correct wave type: ['sine', 'square', 'sawtooth', 'trinagle']" userInfo:nil]; } + return nil; } @@ -42,7 +50,10 @@ + (float)valueForWaveType:(WaveTypeEnum)waveType atPhase:(double)phase { return (float)(2 * (phase / FULL_CIRCLE_RADIANS - floor(phase / FULL_CIRCLE_RADIANS + 0.5))); case WaveTypeTriangle: return (float)(2 * fabs(2 * (phase / FULL_CIRCLE_RADIANS - floor(phase / FULL_CIRCLE_RADIANS + 0.5))) - 1); + default: + @throw [NSException exceptionWithName:@"Invalid wave type" reason:@"You have to pick correct wave type: ['sine', 'square', 'sawtooth', 'trinagle']" userInfo:nil]; } + return 0; }