Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Auto measure label handling #302

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 6 additions & 96 deletions add/data/xql/getMeasures.xql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import module namespace functx = "http://www.functx.com";

import module namespace eutil = "http://www.edirom.de/xquery/util" at "/db/apps/Edirom-Online/data/xqm/util.xqm";

import module namespace measure = "http://www.edirom.de/xquery/measure" at "/db/apps/Edirom-Online/data/xqm/measure.xqm";

(: NAMESPACE DECLARATIONS ================================================== :)

declare namespace mei = "http://www.music-encoding.org/ns/mei";
Expand All @@ -22,107 +24,15 @@ declare namespace xmldb = "http://exist-db.org/xquery/xmldb";
declare option output:media-type "text/plain";
declare option output:method "text";

(: FUNCTION DECLARATIONS =================================================== :)

declare function local:getMeasures($mei as node(), $mdivID as xs:string) as xs:string* {

if ($mei//mei:parts) then (
let $mdiv := $mei/id($mdivID)
let $measureNs :=
if ($mdiv//mei:measure/@label) then (
let $labels := $mdiv//mei:measure/@label/string()
for $label in $labels
let $labelsAnalyzed :=
if (contains($label, '–')) then (
(:substring-before($label, '–'):)
let $first := substring-before($label, '–')
let $last := substring-after($label, '–')
let $steps := xs:integer(number($last) - number($first) + number(1))
for $i in 1 to $steps
return
string(number($first) + $i - 1)
) else
($label)
return
$labelsAnalyzed
) else
($mdiv//mei:measure/@n)

let $measureNsDistinct := distinct-values(eutil:sort-as-numeric-alpha($measureNs))

return
for $measureN in $measureNsDistinct
let $measureNNumber := number($measureN)
let $measures :=
if ($mdiv//mei:measure/@label) then
($mdiv//mei:measure[.//mei:multiRest][number(substring-before(@label, '–')) <= $measureNNumber][.//mei:multiRest/number(@num) gt ($measureNNumber - number(substring-before(@label, '–')))])
else
($mdiv//mei:measure[.//mei:multiRest][number(@n) lt $measureNNumber][.//mei:multiRest/number(@num) gt ($measureNNumber - number(@n))])
let $measures :=
for $part in $mdiv//mei:part
let $partMeasures :=
if ($part//mei:measure/@label) then
($part//mei:measure[@label = $measureN][1])
else
($part//mei:measure[@n = $measureN][1])
for $measure in $partMeasures | $measures[ancestor::mei:part = $part]
let $voiceRef := $part//mei:staffDef/@decls
return
concat(
'{id:"', $measure/@xml:id, '",
voice: "', $voiceRef,
'", partLabel: "', eutil:getPartLabel($measure, 'measure'),
'"}'
)
return
concat(
'{',
'id: "measure_', $mdiv/@xml:id, '_', $measureN, '", ',
'measures: [', string-join($measures, ','), '], ',
'mdivs: ["', $mdiv/@xml:id, '"], ', (: TODO :)
'name: "', $measureN, '"',
'}'
)
) else (
if ($mei/id($mdivID)//mei:measure[@label]) then (
for $measureN in $mei/id($mdivID)//mei:measure/@label
let $measures := $mei/id($mdivID)//mei:measure[@label = $measureN]
let $measure := $measures[1]
(:let $measureLabel := if(exists($measure/@label) and not(contains($measure/@label,'/'))) then($measure/@label) else($measure/@n):)
return
concat(
'{',
'id: "', $measure/@xml:id, '", ',
'measures: [{id:"', $measure/@xml:id, '", voice: "score"}], ',
'mdivs: ["', $measure/ancestor::mei:mdiv[1]/@xml:id, '"], ', (: TODO :)
'name: "', $measureN, '"', (: Hier Unterscheiden wg. Auftakt. :)
'}'
)
) else (
for $measureN in $mei/id($mdivID)//mei:measure/data(@n)
let $measures := $mei/id($mdivID)//mei:measure[@n = $measureN]
let $measure := $measures[1]
(:let $measureLabel := if(exists($measure/@label) and not(contains($measure/@label,'/'))) then($measure/@label) else($measure/@n):)
return
concat(
'{',
'id: "', $measure/@xml:id, '", ',
'measures: [{id:"', $measure/@xml:id, '", voice: "score"}], ',
'mdivs: ["', $measure/ancestor::mei:mdiv[1]/@xml:id, '"], ', (: TODO :)
'name: "', $measureN, '"', (: Hier Unterscheiden wg. Auftakt. :)
'}'
)
)
)
};

(: QUERY BODY ============================================================== :)

let $uri := request:get-parameter('uri', '')
let $mdivID := request:get-parameter('mdiv', '')
let $mei := doc($uri)/root()

let $ret := local:getMeasures($mei, $mdivID)
let $ret := measure:getMeasures($mei, $mdivID)

return
concat('[', string-join($ret, ','), ']')

'[' || string-join($ret, ',') || ']'

69 changes: 10 additions & 59 deletions add/data/xql/getMeasuresOnPage.xql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ xquery version "3.1";
@author <a href="mailto:roewenstrunk@edirom.de">Daniel Röwenstrunk</a>
:)

(: IMPORTS ================================================================= :)

import module namespace eutil="http://www.edirom.de/xquery/util" at "/db/apps/Edirom-Online/data/xqm/util.xqm";

import module namespace measure = "http://www.edirom.de/xquery/measure" at "/db/apps/Edirom-Online/data/xqm/measure.xqm";

(: NAMESPACE DECLARATIONS ================================================== :)

declare namespace mei = "http://www.music-encoding.org/ns/mei";
Expand All @@ -22,60 +28,6 @@ declare namespace xmldb = "http://exist-db.org/xquery/xmldb";
declare option output:method "json";
declare option output:media-type "application/json";

(: FUNCTION DECLARATIONS =================================================== :)

(:~
Finds all measures on a page.

@param $mei The sourcefile
@param $surface The surface to look at
@returns A list of json objects with measure information
:)
declare function local:getMeasures($mei as node(), $surface as node()) as map(*)* {

for $zone in $surface/mei:zone[@type = 'measure']
let $zoneRef := concat('#', $zone/@xml:id)
(:
The first predicate with `contains` is just a rough estimate to narrow down the result set.
It uses the index and is fast while the second (exact) predicate is generally too slow
:)
let $measures := $mei//mei:measure[contains(@facs, $zoneRef)][$zoneRef = tokenize(@facs, '\s+')]
return
for $measure in $measures
let $measureLabel :=
if ($measure/@label) then
($measure/string(@label))
else
($measure/string(@n))
let $measureLabel :=
if ($measure//mei:multiRest) then
($measureLabel || '–' || number($measureLabel) + number($measure//mei:multiRest/@num) - 1)
else
($measureLabel)
return
map {
'zoneId': $zone/string(@xml:id),
'ulx': $zone/string(@ulx),
'uly': $zone/string(@uly),
'lrx': $zone/string(@lrx),
'lry': $zone/string(@lry),
'id': $measure/string(@xml:id),
'name': $measureLabel,
'type': $measure/string(@type),(: where is measure type being used :)
'rest': local:getMRest($measure)
}
};

declare function local:getMRest($measure) {

if ($measure//mei:mRest) then
(string('1'))
else if ($measure//mei:multiRest) then
($measure//mei:multiRest/string(@num))
else
(string('0'))
};

(: QUERY BODY ============================================================== :)

let $uri := request:get-parameter('uri', '')
Expand All @@ -86,8 +38,7 @@ let $mei := doc($uri)/root()

let $surface := $mei/id($surfaceId)

return
array {
(: TODO: überlegen, wie die Staff-spezifischen Ausschnitte angezeigt werden sollen :)
local:getMeasures($mei, $surface)
}
return (
(: TODO: überlegen, wie die Staff-spezifischen Ausschnitte angezeigt werden sollen :)
measure:getMeasuresOnPage($mei, $surface)
)
Loading