Skip to content

Instantly share code, notes, and snippets.

@techpotatoes
Last active August 4, 2021 09:41
Show Gist options
  • Save techpotatoes/4873cea449f2cde6eb19f0b08a1f39e0 to your computer and use it in GitHub Desktop.
Save techpotatoes/4873cea449f2cde6eb19f0b08a1f39e0 to your computer and use it in GitHub Desktop.
Extracting pitch from the audio sample
...
class _MyHomePageState extends State<MyHomePage> {
final _audioRecorder = FlutterAudioCapture();
final pitchDetectorDart = PitchDetector(44100, 2000);
var note = "";
var status = "Click on start";
Future<void> _startCapture() async {
await _audioRecorder.start(listener, onError,
sampleRate: 44100, bufferSize: 3000);
setState(() {
note = "";
status = "Play something";
});
}
Future<void> _stopCapture() async {
await _audioRecorder.stop();
setState(() {
note = "";
status = "Click on start";
});
}
void listener(dynamic obj) {
//Gets the audio sample
var buffer = Float64List.fromList(obj.cast<double>());
final List<double> audioSample = buffer.toList();
//Uses pitch_detector_dart library to detect a pitch from the audio sample
final result = pitchDetectorDart.getPitch(audioSample);
//If there is a pitch - evaluate it
if (result.pitched) {
//TODO
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body:
Expanded(
child: Row(
children: [
Expanded(
child: Center(
child: FloatingActionButton(
onPressed: _startCapture,
child: const Text("Start")))),
Expanded(
child: Center(
child: FloatingActionButton(
onPressed: _stopCapture, child: const Text("Stop")))),
],
))
]),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment