I want to build an app that uses DartMeltySoundFont as a synthesizer and raw_sounds to play the sounds.
DartMeltySoundFont gives me a byte buffer in the type ArrayInt16 and raw_sounds expects Uint8List to play the audio.
I already asked the developer of DartMeltySoundFont but he does not have to to look into it.
This is an example app taken from raw_sound examples and added DartMeltySoundFont to it. Look at the line with the comment "// How do I return a Uint8List from buf16 here? "
import 'dart:typed_data'; // for Uint8List
import 'package:flutter/material.dart';
import 'package:raw_sound/raw_sound_player.dart';
import 'package:dart_melty_soundfont/dart_melty_soundfont.dart';
import 'package:flutter/services.dart' show rootBundle;
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// Player instance to play raw PCM (16-bit integer) audio data
final _playerPCMI16 = RawSoundPlayer();
@override
void initState() {
super.initState();
// release any initialized player instances
_playerPCMI16
.initialize(
pcmType: RawSoundPCMType.PCMI16,
)
.then((value) {
setState(() {
// Trigger rebuild to update UI
});
});
}
@override
void dispose() {
// Finally release any initialized player instances
_playerPCMI16.release();
super.dispose();
}
Future<void> _playPCMI16() async {
if (_playerPCMI16.isPlaying) {
return;
}
await _playerPCMI16.play();
setState(() {
// Trigger rebuild to update UI
});
// Continuously feed the player until the playback is paused/stopped
//final dataBlock = _genPCMI16DataBlock(nPeriods: 20);
final dataBlock = await _getSound();
while (_playerPCMI16.isPlaying) {
await _playerPCMI16.feed(dataBlock);
}
}
Future<void> _pausePCMI16() async {
await _playerPCMI16.pause();
setState(() {
// Trigger rebuild to update UI
});
}
_getSound() async {
// Create the synthesizer.
ByteData bytes = await rootBundle.load('assets/akai_steinway.sf2');
Synthesizer synth = Synthesizer.loadByteData(
bytes,
SynthesizerSettings(
sampleRate: 44100,
blockSize: 64,
maximumPolyphony: 64,
enableReverbAndChorus: true,
));
// Turn on some notes
synth.noteOn(channel: 0, key: 72, velocity: 120);
synth.noteOn(channel: 0, key: 76, velocity: 120);
synth.noteOn(channel: 0, key: 79, velocity: 120);
synth.noteOn(channel: 0, key: 82, velocity: 120);
// Render the waveform (3 seconds)
ArrayInt16 buf16 = ArrayInt16.zeros(numShorts: 44100 * 3);
synth.renderMonoInt16(buf16);
// How do I return a Uint8List from buf16 here?
return null;
}
Widget build(BuildContext context) {
debugPrint('PlayerPCMI16 is inited? ${_playerPCMI16.isInited}');
if (!_playerPCMI16.isInited) {
return Container();
}
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.grey,
),
home: Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('Raw Sound Plugin Example App'),
),
body: Column(
children: [
Card(
child: Row(
children: [
IconButton(
icon: Icon(_playerPCMI16.isPlaying ? Icons.stop : Icons.play_arrow),
onPressed: () {
_playerPCMI16.isPlaying ? _pausePCMI16() : _playPCMI16();
},
),
Text('Test PCMI16 (16-bit Integer)'),
],
),
),
],
),
),
);
}
}
CodePudding user response:
hey there just add below line in _getSound function.
var unit8list= buf16.bytes.buffer.asUint8List();
and return unit8list. it will work
Example:
_getSound() async {
// Create the synthesizer.
ByteData bytes = await rootBundle.load('assets/akai_steinway.sf2');
Synthesizer synth = Synthesizer.loadByteData(
bytes,
SynthesizerSettings(
sampleRate: 44100,
blockSize: 64,
maximumPolyphony: 64,
enableReverbAndChorus: true,
));
// Turn on some notes
synth.noteOn(channel: 0, key: 72, velocity: 120);
synth.noteOn(channel: 0, key: 76, velocity: 120);
synth.noteOn(channel: 0, key: 79, velocity: 120);
synth.noteOn(channel: 0, key: 82, velocity: 120);
// Render the waveform (3 seconds)
ArrayInt16 buf16 = ArrayInt16.zeros(numShorts: 44100 * 3);
synth.renderMonoInt16(buf16);
var unit8list= buf16.bytes.buffer.asUint8List();
// How do I return a Uint8List from buf16 here?
return unit8list;
}