analyzeAudioNoise method

  1. @override
Future<NoiseDetectionResult> analyzeAudioNoise({
  1. required String inputPath,
  2. double sensitivityLevel = 0.5,
  3. ProgressCallback? onProgress,
})
override

Analyzes an audio file for noise detection and quality assessment

Performs comprehensive audio analysis to detect background noise, audio quality issues, and provides recommendations for improvement.

inputPath - Path to the input audio file sensitivityLevel - Noise detection sensitivity (0.0 to 1.0, default: 0.5) onProgress - Optional callback for analysis progress (0.0 to 1.0)

Returns NoiseDetectionResult with analysis results and recommendations. Throws NoiseAnalysisException if analysis fails.

Implementation

@override
Future<NoiseDetectionResult> analyzeAudioNoise({
  required String inputPath,
  double sensitivityLevel = 0.5,
  ProgressCallback? onProgress,
}) async {
  _validateFilePath(inputPath, 'inputPath');
  _validateSensitivityLevel(sensitivityLevel);

  // Set up progress listening
  StreamSubscription<dynamic>? progressSub;
  if (onProgress != null) {
    progressSub = _progressChannel.receiveBroadcastStream().listen((data) {
      if (data is Map && data['operation'] == 'noise_analysis') {
        final progress = data['progress'] as double?;
        if (progress != null) {
          onProgress(progress);
        }
      }
    });
  }

  try {
    final Map<String, dynamic> arguments = {
      'inputPath': inputPath,
      'sensitivityLevel': sensitivityLevel,
    };

    final result = await methodChannel
        .invokeMethod('analyzeAudioNoise', arguments)
        .timeout(const Duration(minutes: 3));

    if (result == null) {
      throw NoiseAnalysisException(
        'Noise analysis failed: No result returned from platform',
        inputPath: inputPath,
        sensitivityLevel: sensitivityLevel,
      );
    }

    // Safely convert the result to Map<String, dynamic>
    final Map<String, dynamic> convertedResult;
    if (result is Map) {
      convertedResult = Map<String, dynamic>.from(result);
    } else {
      throw NoiseAnalysisException(
        'Noise analysis failed: Invalid result type from platform',
        inputPath: inputPath,
        sensitivityLevel: sensitivityLevel,
      );
    }

    return NoiseDetectionResult.fromMap(convertedResult);
  } on PlatformException catch (e) {
    if (e.code == 'PLATFORM_NOT_SUPPORTED') {
      throw PlatformNotSupportedException(
        'Noise analysis is not supported on this platform',
        platform: Platform.operatingSystem,
        operation: 'analyzeAudioNoise',
      );
    }
    throw _convertPlatformException(e, NoiseAnalysisException.new, {
      'inputPath': inputPath,
      'sensitivityLevel': sensitivityLevel,
    });
  } catch (e) {
    throw NoiseAnalysisException(
      'Noise analysis failed: $e',
      originalError: e,
      inputPath: inputPath,
      sensitivityLevel: sensitivityLevel,
    );
  } finally {
    await progressSub?.cancel();
  }
}