Skip to content

Commit

Permalink
refactor: add some errors handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hubgan committed Jul 19, 2024
1 parent bf1d520 commit 4f31546
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
9 changes: 7 additions & 2 deletions cpp/OscillatorNode/OscillatorNodeHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
2 changes: 1 addition & 1 deletion cpp/OscillatorNode/OscillatorNodeWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace audiocontext {
#ifdef ANDROID
explicit OscillatorNodeWrapper(std::shared_ptr<OscillatorNode> oscillator) : oscillator_(oscillator) {}
#else
explicit OscillatorNodeWrapper() : oscillator_(std::make_shared<IOSOscillator>(440)) {}
explicit OscillatorNodeWrapper() : oscillator_(std::make_shared<IOSOscillator>()) {}
#endif

jsi::Value getFrequency(jsi::Runtime &runtime, const jsi::PropNameID &propNameId);
Expand Down
2 changes: 1 addition & 1 deletion ios/nodes/Oscillator/IOSOscillator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions ios/nodes/Oscillator/IOSOscillator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion ios/nodes/Oscillator/OscillatorNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@property (nonatomic, assign) float frequency;
@property (nonatomic, assign) double sampleRate;

- (instancetype)initWithFrequency:(float)frequency;
- (instancetype)init;

- (void)start;

Expand Down
4 changes: 2 additions & 2 deletions ios/nodes/Oscillator/OscillatorNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
23 changes: 17 additions & 6 deletions ios/nodes/Oscillator/WaveType/WaveType.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 4f31546

Please sign in to comment.