flutter_piano_audio_detection 1.0.5 flutter_piano_audio_detection: ^1.0.5 copied to clipboard
Flutter plug-in for Piano Audio Detection. Can recognize the piano sound in real time using Google Magenta tflite model.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_piano_audio_detection/flutter_piano_audio_detection.dart';
import 'dart:async';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final isRecording = ValueNotifier<bool>(false);
FlutterPianoAudioDetection fpad = new FlutterPianoAudioDetection();
Stream<List<dynamic>>? result;
List<String> notes = [];
String printText = "";
@override
void initState() {
super.initState();
fpad.prepare();
}
void start() {
fpad.start();
getResult();
}
void stop() {
fpad.stop();
}
void getResult() {
result = fpad.startAudioRecognition();
result!.listen((event) {
printText = "";
setState(() {
notes = fpad.getNotes(event);
});
notes.map((e) => {printText += e});
print(notes);
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Piano Audio Detection'),
),
body: Center(
child: Text(
'${notes}',
style: TextStyle(fontSize: 72, fontWeight: FontWeight.bold),
),
),
floatingActionButton: Container(
child: ValueListenableBuilder(
valueListenable: isRecording,
builder: (context, value, widget) {
if (value == false) {
return FloatingActionButton(
onPressed: () {
isRecording.value = true;
start();
},
backgroundColor: Colors.blue,
child: const Icon(Icons.mic),
);
} else {
return FloatingActionButton(
onPressed: () {
isRecording.value = false;
stop();
},
backgroundColor: Colors.red,
child: const Icon(Icons.adjust),
);
}
},
),
),
),
);
}
}