-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.dart
153 lines (128 loc) · 3.96 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// ignore_for_file: avoid_print
import 'dart:typed_data'; // for Uint8List
import 'package:dart_melty_soundfont/preset.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter/material.dart';
import 'package:flutter_pcm_sound/flutter_pcm_sound.dart';
import 'package:dart_melty_soundfont/synthesizer.dart';
import 'package:dart_melty_soundfont/synthesizer_settings.dart';
import 'package:dart_melty_soundfont/audio_renderer_ex.dart';
import 'package:dart_melty_soundfont/array_int16.dart';
String asset = 'assets/TimGM6mbEdit.sf2';
int sampleRate = 44100;
void main() => runApp(const MeltyApp());
class MeltyApp extends StatefulWidget {
const MeltyApp({Key? key}) : super(key: key);
@override
State<MeltyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MeltyApp> {
Synthesizer? _synth;
bool _isPlaying = false;
bool _pcmSoundLoaded = false;
bool _soundFontLoaded = false;
int _remainingFrames = 0;
int _fedCount = 0;
int _prevNote = 0;
@override
void initState() {
super.initState();
// DartMeltySoundfont
_loadSoundfont().then((_) {
_soundFontLoaded = true;
setState(() {});
});
// FlutterPcmSound
_loadPcmSound().then((_) {
_pcmSoundLoaded = true;
setState(() {});
});
}
Future<void> _loadPcmSound() async {
FlutterPcmSound.setFeedCallback(onFeed);
await FlutterPcmSound.setLogLevel(LogLevel.standard);
await FlutterPcmSound.setFeedThreshold(8000);
await FlutterPcmSound.setup(sampleRate: sampleRate, channelCount: 1);
}
Future<void> _loadSoundfont() async {
ByteData bytes = await rootBundle.load(asset);
_synth = Synthesizer.loadByteData(bytes, SynthesizerSettings());
// print available instruments
List<Preset> p = _synth!.soundFont.presets;
for (int i = 0; i < p.length; i++) {
String instrumentName = p[i].regions.isNotEmpty ? p[i].regions[0].instrument.name : "N/A";
print('[preset $i] name: ${p[i].name} instrument: $instrumentName');
}
return Future<void>.value(null);
}
@override
void dispose() {
FlutterPcmSound.release();
super.dispose();
}
void onFeed(int remainingFrames) async {
setState(() {
_remainingFrames = remainingFrames;
});
// c major scale
List<int> notes = [60, 62, 64, 65, 67, 69, 71, 72];
int step = (_fedCount ~/ 16) % notes.length;
int curNote = notes[step];
if (curNote != _prevNote) {
_synth!.noteOff(channel: 0, key: _prevNote);
_synth!.noteOn(channel: 0, key: curNote, velocity: 120);
}
ArrayInt16 buf16 = ArrayInt16.zeros(numShorts: 1000);
_synth!.renderMonoInt16(buf16);
await FlutterPcmSound.feed(PcmArrayInt16(bytes: buf16.bytes));
_fedCount++;
_prevNote = curNote;
}
Future<void> _play() async {
// start playing audio
await FlutterPcmSound.play();
setState(() {
_isPlaying = true;
});
// turnOff all notes
_synth!.noteOffAll();
// select preset (i.e. instrument)
_synth!.selectPreset(channel: 0, preset: 0);
}
Future<void> _pause() async {
await FlutterPcmSound.pause();
setState(() {
_isPlaying = false;
});
}
@override
Widget build(BuildContext context) {
Widget child;
if (!_pcmSoundLoaded || !_soundFontLoaded) {
child = const Text("initializing...");
} else {
child = Center(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(20.0),
child: ElevatedButton(
child: Text(_isPlaying ? "Pause" : "Play"),
onPressed: () => _isPlaying ? _pause() : _play(),
),
),
Padding(
padding: const EdgeInsets.all(20.0),
child: Text("Remaining Frames $_remainingFrames"),
)
],
),
);
}
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Soundfont')),
body: child,
));
}
}