sense function

Future<String> sense (dynamic filename dynamic apiKey dynamic fileFormat dynamic taskInput)

Analyzes audio file and Returns it to JSON format.

First, make sure the audio file format is one of the listOfFileformats.

Implementation

Future<String> sense(filename, apiKey, fileFormat, taskInput) async {
  // Exception Handling
  fileError(filename);
  formatError(fileFormat);
  taskError(taskInput);

  final channel = ClientChannel(
    hostAddress,
    port: 50051,
    options: const ChannelOptions(credentials: ChannelCredentials.insecure()),
  );
  final stub = SenseClient(channel);

  const CHUNK = 1024 * 1024;

  Stream<Request> getFileChunks(filename) async* {
    num i = 0;
    var audioByte = await new File(filename).readAsBytes();
    num st = 0;
    num end = 0;
    if (audioByte.length < CHUNK) {
      end = audioByte.length;
    } else {
      end = CHUNK;
    }
    while (true) {
      if (i != 0) {
        st += CHUNK;
        end += CHUNK;
      }
      if (end > audioByte.length && st < audioByte.length) {
        end = audioByte.length;
      } else if (end > audioByte.length && st > audioByte.length) {
        return;
      }
      List<int> temp = audioByte.sublist(st, end);
      i++;
      final requestData = Request()
        ..data = temp
        ..apikey = apiKey
        ..format = fileFormat
        ..task = taskInput;
      yield requestData;
    }
  }

  try {
    var chunksGenerator = getFileChunks(filename);
    var response = await stub.sense(chunksGenerator);
    return (response.outputs);
  } catch (e) {
    throw ArgumentError('Invalid API key');
  }
}