#![forbid(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]
pub mod compiler;
pub mod composer;
pub mod export;
pub mod language;
pub mod player;
pub mod report;
pub mod synth;
pub use pikopiko_runtime as runtime;
pub use pikopiko_runtime::text;
pub(crate) mod text_table {
use crate::text::{Catalog, Seg};
include!(concat!(env!("OUT_DIR"), "/text.rs"));
}
pub mod lang;
use compiler::{include, parser, song};
pub use compiler::ast::Ast;
pub use compiler::diagnostic::{Diagnostic, Severity, Span};
pub use compiler::dialect::Dialect;
pub use compiler::song::{ChordKeys, Event, EventKind, Marker, Meter, Song, SongMeta};
pub use player::voice::{SynthVoice, Voice};
pub use player::{
render_to_vec, render_to_vec_looped, render_to_vec_looped_with, render_to_vec_with, Command,
JumpAt, Notice, NoticeMask, Player, SfxChannels, SfxError, SfxId, TrackActivity,
MAX_REGISTERED_SFX, NOTICE_CAPACITY,
};
pub use report::info::{
describe, describe_with, key_name, secs_to_ticks, ticks_to_secs, KeyInfo, MarkerInfo, SongInfo,
TrackInfo,
};
pub use synth::{Synth, SynthConfig, SynthSpec};
pub const SPEC: &str = include_str!("../spec/ja.md");
pub const SPEC_EN: &str = include_str!("../spec/en.md");
pub fn spec_for(lang: &str) -> &'static str {
if is_japanese(lang) {
SPEC
} else {
SPEC_EN
}
}
pub fn is_japanese(lang: &str) -> bool {
let lang = lang.trim();
lang == "ja" || lang.starts_with("ja-") || lang.starts_with("ja_") || lang.starts_with("ja.")
}
pub const PPQ: u32 = pikopiko_runtime::defaults::PPQ;
pub const WHOLE_TICKS: u32 = 4 * PPQ;
pub const NOTATION_WHOLE: u32 = 1920;
pub const NOTATION_SCALE: u32 = WHOLE_TICKS / NOTATION_WHOLE;
pub const MAX_LENGTH_NUMBER: u32 = NOTATION_WHOLE;
pub const MAX_TICK_LENGTH: u32 = NOTATION_WHOLE * 255;
#[derive(Clone, Copy)]
pub struct CompileOptions<'a> {
pub dialect: Dialect,
pub synths: &'a [&'a SynthSpec],
pub include: Option<&'a dyn include::Include>,
pub prelude: &'a [include::Source<'a>],
}
impl Default for CompileOptions<'_> {
fn default() -> Self {
CompileOptions {
dialect: Dialect::PIKOPIKO,
synths: synth::BUILTIN,
include: None,
prelude: &[],
}
}
}
impl core::fmt::Debug for CompileOptions<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("CompileOptions")
.field("dialect", &self.dialect)
.field("synths", &self.synths)
.field("include", &self.include.map(|_| "…"))
.field("prelude", &self.prelude)
.finish()
}
}
pub fn parse(src: &str) -> (Ast, Vec<Diagnostic>) {
parser::parse(src, &Dialect::PIKOPIKO)
}
pub fn parse_with(src: &str, dialect: &Dialect) -> (Ast, Vec<Diagnostic>) {
parser::parse(src, dialect)
}
pub fn parse_with_options(src: &str, opts: &CompileOptions<'_>) -> (Ast, Vec<Diagnostic>) {
parser::parse_root(src, &opts.dialect, opts.prelude, opts.include, opts.synths)
}
pub fn compile_with_diagnostics(src: &str) -> (Option<Song>, Vec<Diagnostic>) {
compile_with(src, &CompileOptions::default())
}
pub fn compile_with(src: &str, opts: &CompileOptions<'_>) -> (Option<Song>, Vec<Diagnostic>) {
let (_, song, diags) = compile_parts(src, opts);
(song, diags)
}
pub(crate) fn compile_parts(
src: &str,
opts: &CompileOptions<'_>,
) -> (Ast, Option<Song>, Vec<Diagnostic>) {
let (ast, mut diags) = parse_with_options(src, opts);
let (song, exp) = song::expand(&ast, opts.synths);
diags.extend(exp);
parser::fill_files(&mut diags, &ast.sources);
diags.sort_by_key(|d| (d.span.source, d.span.line, d.span.col));
diags.dedup_by(|a, b| a.is_repeat_of(b));
if diags.iter().any(|d| d.is_error()) {
return (ast, None, diags);
}
(ast, song, diags)
}
pub fn compile(src: &str) -> Result<Song, Vec<Diagnostic>> {
match compile_with_diagnostics(src) {
(Some(song), _) => Ok(song),
(None, diags) => Err(diags),
}
}
pub(crate) fn nearest<'a>(name: &str, candidates: &[&'a str]) -> Option<&'a str> {
candidates
.iter()
.map(|c| (edit_distance(c, name), *c))
.filter(|(d, c)| *d <= 2 && *d * 2 <= c.chars().count().max(name.chars().count()))
.min()
.map(|(_, c)| c)
}
fn edit_distance(a: &str, b: &str) -> usize {
let a: Vec<char> = a.chars().collect();
let b: Vec<char> = b.chars().collect();
let mut d = vec![vec![0usize; b.len() + 1]; a.len() + 1];
for (i, row) in d.iter_mut().enumerate() {
row[0] = i;
}
for (j, v) in d[0].iter_mut().enumerate() {
*v = j;
}
for i in 1..=a.len() {
for j in 1..=b.len() {
let cost = usize::from(a[i - 1] != b[j - 1]);
let mut v = (d[i - 1][j] + 1)
.min(d[i][j - 1] + 1)
.min(d[i - 1][j - 1] + cost);
if i > 1 && j > 1 && a[i - 1] == b[j - 2] && a[i - 2] == b[j - 1] {
v = v.min(d[i - 2][j - 2] + 1);
}
d[i][j] = v;
}
}
d[a.len()][b.len()]
}