/// <summary> /// Writes an audio buffer to the specified instance of a cExternalAudioSource component. /// The data must match the specified data format for the component (sample size, number of channels, etc.). /// </summary> /// <param name="componentName">Instance name of the cExternalAudioSource component to write to.</param> /// <param name="data">Audio frames to write.</param> /// <returns> /// Returns true if the data was written successfully, otherwise returns false /// (e.g. if the internal buffer of the component is full). /// </returns> /// <exception cref="ObjectDisposedException"> /// Thrown if the object has already been disposed. /// </exception> /// <exception cref="InvalidOperationException"> /// Thrown if openSMILE has not been initialized yet. /// </exception> /// <exception cref="OpenSmileException"> /// Thrown for internal openSMILE errors. /// </exception> public bool ExternalAudioSourceWriteData(string componentName, float[] data) { if (disposed) { throw new ObjectDisposedException(typeof(OpenSMILE).FullName); } if (smileObj == IntPtr.Zero) { throw new InvalidOperationException("openSMILE must be initialized first."); } OpenSmileResult result = OpenSmileApi.Smile_extaudiosource_write_data(smileObj, componentName, data, data.Length * sizeof(float)); if (result == OpenSmileResult.SMILE_SUCCESS) { return(true); } else if (result == OpenSmileResult.SMILE_NOT_WRITTEN) { return(false); } else { CheckSmileResult(result); throw new Exception(); // unreachable as CheckSmileResult will throw, just to make compiler happy } }
/// <summary> /// Initializes OpenSMILE with the provided config file and command-line options. /// </summary> /// <param name="configFile">Path to the config file to load.</param> /// <param name="options">Mapping of option names to values.</param> /// <param name="logLevel">Log level to set for the logger.</param> /// <param name="debug">Whether to enable debug mode (outputs debug-level log messages). SMILEapi must be compiled in debug mode for this to take effect.</param> /// <param name="consoleOutput">Whether log messages are printed to the console (useful for debugging).</param> /// <exception cref="ObjectDisposedException"> /// Thrown if the object has already been disposed. /// </exception> /// <exception cref="InvalidOperationException"> /// Thrown if openSMILE has already been initialized before. /// </exception> /// <exception cref="OpenSmileException"> /// Thrown for internal openSMILE errors. /// </exception> public void Initialize(string configFile, Dictionary <string, string> options, int logLevel = 2, bool debug = false, bool consoleOutput = false) { if (disposed) { throw new ObjectDisposedException(typeof(OpenSMILE).FullName); } if (smileObj != IntPtr.Zero) { throw new InvalidOperationException("openSMILE has already been initialized before."); } smileObj = OpenSmileApi.Smile_new(); if (smileObj == IntPtr.Zero) { throw new OpenSmileException(OpenSmileResult.SMILE_FAIL, "Could not create new SMILEapi object"); } logCallback = OnLogMessage; stateCallback = OnStateChanged; OpenSmileApi.Smile_set_log_callback(smileObj, logCallback, IntPtr.Zero); OpenSmileApi.Smile_set_state_callback(smileObj, stateCallback, IntPtr.Zero); smileopt_t[] opts = options?.Select(opt => new smileopt_t(opt.Key, opt.Value)).ToArray(); CheckSmileResult(OpenSmileApi.Smile_initialize(smileObj, configFile, opts.Length, opts, logLevel, debug ? 1 : 0, consoleOutput ? 1 : 0, null)); }
private void CheckSmileResult(OpenSmileResult result) { if (result != OpenSmileResult.SMILE_SUCCESS) { string message = OpenSmileApi.Smile_error_msg(smileObj); throw new OpenSmileException(result, message); } }
/// <summary> /// Resets the internal state of openSMILE after a run has finished or was aborted. /// After resetting, you may call <see cref="Run"/> again without the need to call <see cref="Initialize"/> first. /// You must re-register any cExternalSink/cExternalMessageInterface callbacks, though. /// </summary> /// <exception cref="ObjectDisposedException"> /// Thrown if the object has already been disposed. /// </exception> /// <exception cref="InvalidOperationException"> /// Thrown if openSMILE has not been initialized yet. /// </exception> /// <exception cref="OpenSmileException"> /// Thrown for internal openSMILE errors. /// </exception> public void Reset() { if (disposed) { throw new ObjectDisposedException(typeof(OpenSMILE).FullName); } if (smileObj == IntPtr.Zero) { throw new InvalidOperationException("openSMILE must be initialized first."); } CheckSmileResult(OpenSmileApi.Smile_reset(smileObj)); }
/// <summary> /// Sets the callback function for the specified cExternalMessageInterface component instance. /// The function will get called whenever the component receives a message. /// </summary> /// <param name="componentName">Instance name of the cExternalMessageInterface component for which to set the callback.</param> /// <exception cref="ObjectDisposedException"> /// Thrown if the object has already been disposed. /// </exception> /// <exception cref="InvalidOperationException"> /// Thrown if openSMILE has not been initialized yet. /// </exception> /// <exception cref="OpenSmileException"> /// Thrown for internal openSMILE errors. /// </exception> public void ExternalMessageInterfaceSetJsonCallback(string componentName, ExternalMessageInterfaceJsonCallback callback) { if (disposed) { throw new ObjectDisposedException(typeof(OpenSMILE).FullName); } if (smileObj == IntPtr.Zero) { throw new InvalidOperationException("openSMILE must be initialized first."); } CheckSmileResult(OpenSmileApi.Smile_extmsginterface_set_json_msg_callback(smileObj, componentName, callback, IntPtr.Zero)); }
/// <summary> /// Sets the callback function for the specified cExternalSink component instance. /// The function will get called whenever another openSMILE component writes data to /// the cExternalSink component. /// </summary> /// <param name="componentName">Instance name of the cExternalSink component for which to set the callback.</param> /// <exception cref="ObjectDisposedException"> /// Thrown if the object has already been disposed. /// </exception> /// <exception cref="InvalidOperationException"> /// Thrown if openSMILE has not been initialized yet. /// </exception> /// <exception cref="OpenSmileException"> /// Thrown for internal openSMILE errors. /// </exception> public void ExternalSinkSetCallback(string componentName, ExternalSinkCallback callback) { if (disposed) { throw new ObjectDisposedException(typeof(OpenSMILE).FullName); } if (smileObj == IntPtr.Zero) { throw new InvalidOperationException("openSMILE must be initialized first."); } CheckSmileResult(OpenSmileApi.Smile_extsink_set_data_callback(smileObj, componentName, callback, IntPtr.Zero)); }
/// <summary> /// Signals the end of the input for the specified cExternalAudioSource component instance. /// Attempts to write more data to the component after calling this method will fail. /// </summary> /// <param name="componentName">Instance name of the cExternalAudioSource component for which to signal the EOI state.</param> /// <exception cref="ObjectDisposedException"> /// Thrown if the object has already been disposed. /// </exception> /// <exception cref="InvalidOperationException"> /// Thrown if openSMILE has not been initialized yet. /// </exception> /// <exception cref="OpenSmileException"> /// Thrown for internal openSMILE errors. /// </exception> public void ExternalAudioSourceSetEoi(string componentName) { if (disposed) { throw new ObjectDisposedException(typeof(OpenSMILE).FullName); } if (smileObj == IntPtr.Zero) { throw new InvalidOperationException("openSMILE must be initialized first."); } CheckSmileResult(OpenSmileApi.Smile_extaudiosource_set_external_eoi(smileObj, componentName)); }
protected virtual void Dispose(bool disposing) { if (disposed) { return; } if (disposing) { // Free any other managed objects here later if structure changed. } OpenSmileApi.Smile_free(smileObj); smileObj = IntPtr.Zero; disposed = true; }