csounddart 0.0.1
csounddart: ^0.0.1 copied to clipboard
Dart interface to Csound API for Web and Desktop.
example/lib/main.dart
import 'package:csounddart/csound.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:csounddart/csounddart.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
}
late Csound _cs;
static const String _csd =
"""
<CsoundSynthesizer>
<CsOptions>
--output=dac
--realtime
</CsOptions>
; ==============================================
<CsInstruments>
sr = 44100
ksmps = 16
nchnls = 2
0dbfs = 1
opcode mtofi,i,i
imid xin
iout = 440 * exp(0.0577622645 * (imid - 69))
xout iout
endop
instr 1
inote init p4
ivel init p5
ifq = mtofi(inote)
kfq = k(ifq)
iT = 1000 / ifq
aPulse = linseg:a(1, iT/1000, 0)
aPulse = (aPulse + (noise:a(1,0.5) * aPulse)) / 2
aPulse = aPulse * pow(2, ((1-((ivel-1)/126)) * -4 ))
iff = ivel * 100 + 20
aPulse = butterlp(aPulse, iff)
;resonr allows to clear internal space
alow, ahigh, ao svfilter aPulse, kfq, 500
//ao = butterbp(aPulse, kfq, 1500)
amo = ao * 15
amo -= 3.25
amo = butterlp(amo, 150)
//ao = lowpass2:a(amo, 150, 50)
amo = distort1( amo, 1, 1, 0, 0)
; Two parallel lines
;one raising, removing gain, and DC
a1 = (pow(amo, 2)) * (-0.08)
a1 = dcblock(a1)
a2 = butterhp(amo, 250) * 0.25
as = limit( ((a1 * 0.0001) + a2 + ao) * 0.7 , -1, 1)
as = dcblock(as)
outs as, as
endin
instr 2
ao = oscili(0.3, linseg:k(100, p3, 400))
av = vco2(0.3, linseg:k(1, p3, 10))
chnset(av, "vco")
ao4 = oscili(1.0, linseg:k(1000, p3, 10))
chnset(ao4, "sine")
outs ao,ao
endin
</CsInstruments>
; ==============================================
<CsScore>
i 2 0 10
i 1 0 5 45 50
i 1 5 5 60 50
i 1 10 5 80 40
</CsScore>
</CsoundSynthesizer>
""";
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await Csounddart.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Container(
child: SingleChildScrollView(
child: Column(
children: [
Text('Running on: $_platformVersion\n'),
Container(height: 3,),
ElevatedButton(onPressed: (){
_cs = Csound();
}, child: Text("Init")),
Container(height: 3,),
ElevatedButton(onPressed: (){
_cs.compileCsdText(_csd);
_cs.perform();
}, child: Text("Play")),
Container(height: 3,),
ElevatedButton(onPressed: (){
_cs.stop();
}, child: Text("Stop")),
],
),
),
),
),
),
);
}
}