2021-10-13 23:03:14 +00:00
import { isUndef } from "./TypeCheck" ;
2019-08-10 22:07:02 +00:00
/ * *
2019-12-16 20:58:31 +00:00
* Assert that the statement is true , otherwise invoke the error .
* @param statement
* @param error The message which is passed into an Error
2019-08-10 22:07:02 +00:00
* /
2021-10-13 17:10:36 +00:00
export function assert ( statement : boolean , error : string ) : asserts statement {
2019-08-10 22:07:02 +00:00
if ( ! statement ) {
throw new Error ( error ) ;
}
}
2019-10-09 16:38:10 +00:00
/ * *
* Make sure that the given value is within the range
* /
2019-11-17 18:09:19 +00:00
export function assertRange ( value : number , gte : number , lte = Infinity ) : void {
2019-10-09 16:38:10 +00:00
if ( ! ( gte <= value && value <= lte ) ) {
2021-10-13 23:03:14 +00:00
throw new RangeError (
` Value must be within [ ${ gte } , ${ lte } ], got: ${ value } `
) ;
2019-10-09 16:38:10 +00:00
}
}
2019-11-11 16:43:43 +00:00
/ * *
2021-10-13 23:03:14 +00:00
* Warn if the context is not running .
2019-11-11 16:43:43 +00:00
* /
2021-10-13 23:03:14 +00:00
export function assertContextRunning (
context : import ( "../context/BaseContext" ) . BaseContext
) : void {
2019-11-11 16:43:43 +00:00
// add a warning if the context is not started
if ( ! context . isOffline && context . state !== "running" ) {
2021-10-13 23:03:14 +00:00
warn (
"The AudioContext is \"suspended\". Invoke Tone.start() from a user action to start the audio."
) ;
}
}
/ * *
* If it is currently inside a scheduled callback
* /
let isInsideScheduledCallback = false ;
let printedScheduledWarning = false ;
/ * *
* Notify that the following block of code is occurring inside a Transport callback .
* /
export function enterScheduledCallback ( insideCallback : boolean ) : void {
isInsideScheduledCallback = insideCallback ;
}
/ * *
* Make sure that a time was passed into
* /
export function assertUsedScheduleTime (
time? : import ( "../type/Units" ) . Time
) : void {
if ( isUndef ( time ) && isInsideScheduledCallback && ! printedScheduledWarning ) {
printedScheduledWarning = true ;
warn ( "Events scheduled inside of scheduled callbacks should use the passed in scheduling time. See https://github.com/Tonejs/Tone.js/wiki/Accurate-Timing" ) ;
2019-11-11 16:43:43 +00:00
}
}
2019-09-20 04:31:43 +00:00
/ * *
* A basic logging interface
* /
interface Logger {
log : ( args? : any [ ] ) = > void ;
warn : ( args? : any [ ] ) = > void ;
}
/ * *
* The default logger is the console
* /
let defaultLogger : Logger = console ;
/ * *
* Set the logging interface
* /
2019-09-20 21:49:54 +00:00
export function setLogger ( logger : Logger ) : void {
defaultLogger = logger ;
2019-09-20 04:31:43 +00:00
}
/ * *
* Log anything
* /
2019-08-10 22:07:02 +00:00
export function log ( . . . args : any [ ] ) : void {
2019-09-20 04:31:43 +00:00
defaultLogger . log ( . . . args ) ;
}
/ * *
* Warn anything
* /
export function warn ( . . . args : any [ ] ) : void {
defaultLogger . warn ( . . . args ) ;
2019-08-10 22:07:02 +00:00
}