Skip to content

Commit

Permalink
feat: updated interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Makowski committed Nov 14, 2024
1 parent d104c5a commit 7389e47
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
18 changes: 17 additions & 1 deletion packages/react-native-audio-api/src/core/BaseAudioContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IBaseAudioContext } from '../interfaces';
import { ContextState } from './types';
import { ContextState, PeriodicWaveConstraints } from './types';
import AudioDestinationNode from './AudioDestinationNode';
import OscillatorNode from './OscillatorNode';
import GainNode from './GainNode';
Expand Down Expand Up @@ -75,4 +75,20 @@ export default class BaseAudioContext {
this.context.createBuffer(numOfChannels, length, sampleRate)
);
}

createPeriodicWave(
real: number[],
imag: number[],
constraints?: PeriodicWaveConstraints
): PeriodicWave {
if (real.length !== imag.length) {
throw new RangeError(
`The lengths of the real (${real.length}) and imaginary (${imag.length}) arrays are different`
);
}

const disableNormalization = constraints?.disableNormalization ?? false;

return this.context.createPeriodicWave(real, imag, disableNormalization);
}
}
12 changes: 12 additions & 0 deletions packages/react-native-audio-api/src/core/OscillatorNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { OscillatorType } from './types';
import AudioScheduledSourceNode from './AudioScheduledSourceNode';
import AudioParam from './AudioParam';
import BaseAudioContext from './BaseAudioContext';
import PeriodicWave from './PeriodicWave';
import { InvalidStateError } from '../errors';

export default class OscillatorNode extends AudioScheduledSourceNode {
readonly frequency: AudioParam;
Expand All @@ -20,6 +22,16 @@ export default class OscillatorNode extends AudioScheduledSourceNode {
}

public set type(value: OscillatorType) {
if (value === 'custom') {
throw new InvalidStateError(
"The type can't be set to custom. You need to call setPeriodicWave() instead in order to define a custom waveform."
);
}

(this.node as IOscillatorNode).type = value;
}

public setPeriodicWave(wave: PeriodicWave): void {
(this.node as IOscillatorNode).setPeriodicWave(wave.periodicWave);
}
}
10 changes: 10 additions & 0 deletions packages/react-native-audio-api/src/core/PeriodicWave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IPeriodicWave } from '../interfaces';

Check warning

Code scanning / check-spelling

Candidate Pattern Warning

Line matches candidate pattern "\\b(?:I|isA)(?=(?:[A-Z][a-z]{2,})+\\b)" (candidate-pattern)

Check failure

Code scanning / check-spelling

Unrecognized Spelling Error

IPeriodic is not a recognized word. (unrecognized-spelling)

export default class PeriodicWave {
/** @internal */
public readonly periodicWave: IPeriodicWave;

Check failure

Code scanning / check-spelling

Unrecognized Spelling Error

IPeriodic is not a recognized word. (unrecognized-spelling)

constructor(periodicWave: IPeriodicWave) {

Check failure

Code scanning / check-spelling

Unrecognized Spelling Error

IPeriodic is not a recognized word. (unrecognized-spelling)
this.periodicWave = periodicWave;
}
}
11 changes: 10 additions & 1 deletion packages/react-native-audio-api/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ export type BiquadFilterType =

export type ContextState = 'running' | 'closed';

export type OscillatorType = 'sine' | 'square' | 'sawtooth' | 'triangle';
export type OscillatorType =
| 'sine'
| 'square'
| 'sawtooth'
| 'triangle'
| 'custom';

export interface PeriodicWaveConstraints {
disableNormalization: boolean;
}
14 changes: 14 additions & 0 deletions packages/react-native-audio-api/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface IBaseAudioContext {
readonly state: ContextState;
readonly sampleRate: number;
readonly currentTime: number;

createOscillator(): IOscillatorNode;
createGain(): IGainNode;
createStereoPanner(): IStereoPannerNode;
Expand All @@ -21,6 +22,11 @@ export interface IBaseAudioContext {
length: number,
sampleRate: number
) => IAudioBuffer;
createPeriodicWave: (
real: number[],
imag: number[],
disableNormalization: boolean
) => IPeriodicWave;

Check warning

Code scanning / check-spelling

Candidate Pattern Warning

Line matches candidate pattern "\\b(?:I|isA)(?=(?:[A-Z][a-z]{2,})+\\b)" (candidate-pattern)

Check failure

Code scanning / check-spelling

Unrecognized Spelling Error

IPeriodic is not a recognized word. (unrecognized-spelling)
}

export interface IAudioContext extends IBaseAudioContext {
Expand All @@ -34,6 +40,7 @@ export interface IAudioNode {
readonly channelCount: number;
readonly channelCountMode: ChannelCountMode;
readonly channelInterpretation: ChannelInterpretation;

connect: (node: IAudioNode) => void;
disconnect: (node: IAudioNode) => void;
}
Expand All @@ -52,6 +59,7 @@ export interface IBiquadFilterNode extends IAudioNode {
readonly Q: AudioParam;
readonly gain: AudioParam;
type: BiquadFilterType;

getFrequencyResponse(
frequencyArray: number[],
magResponseOutput: number[],
Expand All @@ -70,6 +78,8 @@ export interface IOscillatorNode extends IAudioScheduledSourceNode {
readonly frequency: IAudioParam;
readonly detune: IAudioParam;
type: OscillatorType;

setPeriodicWave(periodicWave: IPeriodicWave): void;

Check failure

Code scanning / check-spelling

Unrecognized Spelling Error

IPeriodic is not a recognized word. (unrecognized-spelling)
}

export interface IAudioBufferSourceNode extends IAudioScheduledSourceNode {
Expand All @@ -82,6 +92,7 @@ export interface IAudioBuffer {
readonly duration: number;
readonly sampleRate: number;
readonly numberOfChannels: number;

getChannelData(channel: number): number[];
copyFromChannel(
destination: number[],
Expand All @@ -100,7 +111,10 @@ export interface IAudioParam {
defaultValue: number;
minValue: number;
maxValue: number;

setValueAtTime: (value: number, startTime: number) => void;
linearRampToValueAtTime: (value: number, endTime: number) => void;
exponentialRampToValueAtTime: (value: number, endTime: number) => void;
}

export interface IPeriodicWave {}

1 comment on commit 7389e47

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log or 📝 job summary for details.

Unrecognized words (1)

IPeriodic

To accept these unrecognized words as correct, you could run the following commands

... in a clone of the [email protected]:software-mansion-labs/react-native-audio-api.git repository
on the feat/periodic-wave branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/main/apply.pl' |
perl - 'https://github.com/software-mansion-labs/react-native-audio-api/actions/runs/11843118839/attempts/1'
Pattern suggestions ✂️ (1)

You could add these patterns to .github/actions/spelling/patterns.txt:

# Automatically suggested patterns

# hit-count: 6 file-count: 2
# IServiceProvider / isAThing
\b(?:I|isA)(?=(?:[A-Z][a-z]{2,})+\b)

Notices (1)

See the 📜action log or 📝 job summary for details.

ℹ️ Notices Count
ℹ️ candidate-pattern 2

See ℹ️ Event descriptions for more information.

If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

🚂 If you're seeing this message and your PR is from a branch that doesn't have check-spelling,
please merge to your PR's base branch to get the version configured for your repository.

Please sign in to comment.