Mercurial > hg > dml-open-cliopatria
comparison cpack/dml/components/score.pl @ 0:718306e29690 tip
commiting public release
author | Daniel Wolff |
---|---|
date | Tue, 09 Feb 2016 21:05:06 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:718306e29690 |
---|---|
1 /* Part of DML (Digital Music Laboratory) | |
2 Copyright 2014-2015 Samer Abdallah, University of London | |
3 | |
4 This program is free software; you can redistribute it and/or | |
5 modify it under the terms of the GNU General Public License | |
6 as published by the Free Software Foundation; either version 2 | |
7 of the License, or (at your option) any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public | |
15 License along with this library; if not, write to the Free Software | |
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 */ | |
18 | |
19 :- module(cp_score, | |
20 [ score//2, score//3 | |
21 , sonify_ui//2 | |
22 , score_audio_player//1 | |
23 , score_audio_player//3 | |
24 , pitch_class_histogram//1 | |
25 ]). | |
26 | |
27 :- use_module(library(http/html_write)). | |
28 :- use_module(library(http/http_dispatch)). | |
29 :- use_module(library(dcg_core)). | |
30 :- use_module(library(musiclab)). | |
31 :- use_module(library(humdrum_p2r)). | |
32 :- use_module(library(mlserver)). | |
33 :- use_module(library(real)). | |
34 :- use_module(library(sandbox)). | |
35 :- use_module(components(matlab),[]). | |
36 :- use_module(components(r_fig),[]). | |
37 :- use_module(components(audio),[audio_player//2]). | |
38 :- use_module(api(score)). | |
39 | |
40 :- set_prolog_flag(double_quotes,string). | |
41 | |
42 %% score(+R:uri,+Width:number)// is det. | |
43 % | |
44 % Generates an HTML DIV element containing a rendered score. URI is assumed | |
45 % to refer to a Humdrum file. Width is in mm and is passed to Lilypond as | |
46 % the desired staff width. | |
47 score(URI,Width) --> score(URI,Width,[]). | |
48 score(URI,Width,Opts) --> | |
49 {option(transpose(T),Opts,'P1')}, | |
50 {http_link_to_id(score_render,[uri(URI),width(Width),layout(snip),format(svg),transpose(T)],URL)}, | |
51 html(div([id=score,width='100%',height=auto],[img([src=URL],[])])). | |
52 | |
53 | |
54 %% sonify_ui(+R:uri,+ID:hander_id)// is det | |
55 % | |
56 % Generates an interface for setting parameters and sonifying a Humdrum score. | |
57 sonify_ui(URI,HandlerID) --> | |
58 { http_link_to_id(HandlerID,[],AudioPlayerURL), | |
59 setting_property(score:fluidsynth_rc,type(oneof(RCs))), | |
60 setting(score:fluidsynth_rc,RC0), | |
61 Intervals=['-P5','-d5','-P4','-M3','-m3','-M2','-m2','P1','m2','M2','m3','M3','P4','d5','P5'] | |
62 }, | |
63 html([ form([class=forms,target=player,method=get,action=AudioPlayerURL], | |
64 [ input([type=hidden,name=uri,value=URI]) | |
65 , input([type=hidden,name=autoplay,value=true]) | |
66 %, input([type=hidden,name=format,value=ogg]) | |
67 , table([class=form], | |
68 [ tr([ th(class=label,"Tempo scaling factor"), | |
69 td(input([type=number,min=0,max=4,step=0.1,name=tempo,value=1],[]))]) | |
70 % , tr([ th(class=label,"Transposition in semitones"), | |
71 % td(input([type=text,name=transpose,value='P1'],[]))]) | |
72 , tr([ th(class=label,"Transposition interval"), | |
73 td(\html_select(transpose,Intervals,'P1'))]) | |
74 , tr([ th(class=label,"Fluidsynth initialisation"), | |
75 td(\html_select(fluidrc,RCs,RC0))]) | |
76 ]) | |
77 , input([type=submit,class=btn,value="Sonify"],[]) | |
78 , iframe([name=player,seamless=seamless,style="display:inline-block;height:3.2em"],[]) | |
79 ]) | |
80 ]). | |
81 | |
82 html_select(Name,Values,Initial) --> | |
83 html(select([name=Name], \seqmap(html_option(Initial),Values))). | |
84 | |
85 html_option(X,X) --> !, html(option(selected=selected,X)). | |
86 html_option(_,X) --> html(option(X)). | |
87 | |
88 | |
89 %% score_audio_player(+R:uri,+As:list(html_attrib),+Ps:list(http_param))// is det. | |
90 %% score_audio_player(+R:uri)// is det. | |
91 % | |
92 % Generates an HTML 5 audio player to play a sonified score. Ps is a list | |
93 % of HTTP parameters to be passed ultimately to score_sonify/1. As is a list | |
94 % of HTML element attributes to be added to the HTML AUDIO element. | |
95 score_audio_player(URI) --> score_audio_player(URI,[],[]). | |
96 score_audio_player(URI,Attr,Params) --> | |
97 {maplist(score_audio_link(URI,Params),[ogg],Links)}, | |
98 audio_player(Links, Attr). | |
99 | |
100 score_audio_link(URI,Params,Fmt,URL-just(Fmt)) :- get_link(URI,a(Params)-Fmt,URL). | |
101 | |
102 | |
103 %% pitch_class_histogram(+Lang:oneof([ml,r]),+R:uri)// is det. | |
104 % | |
105 % Generates a graphical rendering of the pitch class histogram computed | |
106 % from the given URI, assumed to refer to a Humdrum file. Figure is | |
107 % generated using Matlab. | |
108 pitch_class_histogram(URI) --> | |
109 cp_r_fig:figure( cp_score:pitch_class_histogram(r,URI), 12, 6, []). | |
110 % cp_matlab:figure( cp_score:pitch_class_histogram(ml,URI), 12, 6, []). | |
111 | |
112 pitch_class_histogram(Lang,URI) :- | |
113 hum_uri_path(URI,Path), | |
114 kern_pc_hist(Path,Hist1), | |
115 findall(PCN-X, (member(PC-X,Hist1), pitch_class_number(PC,PCN)), Hist2), | |
116 numlist(0,11,PCNs), | |
117 maplist(pc_number_name,PCNs,PCNames), | |
118 maplist(pcn_count(Hist2),PCNs,Counts), | |
119 ( Lang=r | |
120 -> r(par(ps=10,mar=[2.1,2.2,1.1,0])), | |
121 r(barplot(Counts,'names.arg'=PCNames,main="Pitch class histogram")) | |
122 ; Lang=ml | |
123 -> ?? ( bar(PCNs,Counts); | |
124 xticks(PCNs,cell(PCNames)); | |
125 title("Pitch class histogram"); | |
126 caxis([0,3])) | |
127 ). | |
128 | |
129 pcn_count(Hist,PCN,Count) :- member(PCN-Count,Hist) -> true; Count=0. | |
130 | |
131 | |
132 sandbox:safe_primitive(cp_score:pitch_class_histogram(_,_)). |