Get Position of Notes

• Jan 16, 2017 - 13:21

Hello,

For a project I must be able to get the position of all elements in a score.

E.g. The first note is at position (x1, y1) in the score (in pixels or whatever)

At some point musescore must know at what position it places each element. Is it possible to extract this information somehow?

Thanks for your help


Comments

Same question..

Graphical position within the page : I see that the object note has some information like this, inherited from Element ("bbox", "pos"). It works.. There is "something" in pos.x and pos.y. What's this ? A number ? I have difficulties to understand the online doc in the plugin-editor ( few explanation, .. ). "bbox" covers the head only ? The stem also ?

Page Number : I can't retrieve the pagenumber of the cursor ... I see in the online-doc that an object "Page" exists, inherited from "Element", with a "pagenumber" property. Not clear ( for me ) how to use it ... I've tried a lot of combinations without success.

"notes" , .. : lot of list of objects are not in the online documentation. Difficult to "discover" in "blind" mode.
Perhaps I've missed something. I will be happy to learn more how to progress...

Hereafter a sample fo script to try to discover how ot works.

{syntaxhighlighter }
import QtQuick 2.0
import MuseScore 1.0
MuseScore {
version: "1.0"
description: "plugin scan position"
menuPath: "Plugins.Notes.scan_position"

function scanPosition() {
var cursor = curScore.newCursor();
cursor.rewind(0);
cursor.voice = 0
cursor.staffIdx = curScore.nstaves - 1;
while (cursor.segment) {
if (cursor.element && cursor.element.type == Element.CHORD) {
var notes = cursor.element.notes;
for (var i = 0; i < notes.length; i++) {
var note = notes[i];
console.log("note" + note.pitch + + " pos=" + note.pos.x + "/" + note.pos.y + " page#" + cursor.element.pagenumber );
}
}
cursor.next();
}
}
onRun: {
console.log("hello scan position");
if (typeof curScore === 'undefined')
Qt.quit();
scanPosition()
Qt.quit();
}
}
{/syntaxhighlighter}

In reply to by Frank Revolle

I progress . I can get the page number of a note, modify the layout, ... :-)
I discover a little bit more thanks to generic information in :
https://musescore.org/en/developers-handbook/references/musescore-inter…

{syntaxhighlighter }
import QtQuick 2.0
import MuseScore 1.0
import FileIO 1.0

MuseScore {
version: "1.0"
description: "plugin scan position"
menuPath: "Plugins.Notes.scan_position"

// output file to save the positions of the notes
FileIO {
id: outfile
source: "outfile.txt"
onError: console.log(msg)
}

// input file with layout parameters
FileIO {
id: infile
source: "infile.txt"
onError: console.log(msg)
}

// to inspect an object
function logInspect(o, oname) {
console.log("object " + oname + " <" + o.objectName + ">" );
for(var p in o){
var t=typeof o[p];
console.log(" " + p + " / " + t )
}
}

// to read positions of the chords
function scanPosition() {
// logInspect(curScore,"score");

// read the width and height from the input file
// var params = infile.read();
var params = "width 8 height 8";
var reg_width = /width (\d*)/ ;
var width = Number(params.match(reg_width)[1]);
var reg_height = /height (\d*)/ ;
var height = Number(params.match(reg_height)[1]);
console.log("infile size=<" + width + "/" + height + ">" );

// apply the width and height in the layout
curScore.pageFormat.evenBottomMargin = 0.5 ;
curScore.pageFormat.evenLeftMargin = 0.5 ;
curScore.pageFormat.evenTopMargin = 0.5 ;
curScore.pageFormat.oddBottomMargin = 0.5 ;
curScore.pageFormat.oddLeftMargin = 0.5 ;
curScore.pageFormat.oddTopMargin = 0.5 ;
curScore.pageFormat.size.width = width ;
curScore.pageFormat.size.height = height ;
curScore.pageFormat.printableWidth = curScore.pageFormat.size.width
- 2 * curScore.pageFormat.evenLeftMargin ;
curScore.pageFormat.twosided = false ;
curScore.doLayout();

console.log("pages=" + curScore.npages + " measures=" + curScore.nmeasures)

// scan the ticks of the measures
var cursor = curScore.newCursor();
cursor.rewind(0);
cursor.voice = 0;
cursor.staffIdx = curScore.nstaves - 1;
var tmeasure = [ ] ;
while (cursor.segment) {
tmeasure.push(cursor.tick) ;
cursor.nextMeasure();
}
console.log("measure " + tmeasure );

// scan the graphic positions of the chords
cursor.rewind(0);
cursor.voice = 0;
cursor.staffIdx = curScore.nstaves - 1;
var nrmeasure = 0 ;
var nbmeasure = tmeasure.length ;
var offsett = 0 ;
var toWrite = "" ;
while (cursor.segment) {
var pagenr = cursor.measure.parent.parent.pagenumber;
if (cursor.element && cursor.element.type == Element.CHORD) {
var chord = cursor.element ;
var t = cursor.tick ;
// search the measure for this tick
while ((nrmeasure < nbmeasure ) && (t >= tmeasure[nrmeasure] )) {
nrmeasure ++ ;
offsett = tmeasure[nrmeasure - 1];
}
// add the information in the string to write
toWrite +=
" m " + nrmeasure +
" t " + (t - offsett) +
" p " + pagenr +
" x " + chord.pagePos.x.toPrecision(6) +
" y " + chord.pagePos.y.toPrecision(6) +
" w " + chord.bbox.width.toPrecision(6) +
" h " + chord.bbox.height.toPrecision(6) +
"\n" ;
}
cursor.next();
}

// write the positions in the output file
outfile.write(toWrite);
}

onRun: {
console.log("hello scan position");

if (typeof curScore === 'undefined')
Qt.quit();

scanPosition()

Qt.quit();
}
}
{/syntaxhighlighter}

In reply to by Frank Revolle

Hey,

Thank you very much for your help. I think your plugin is a very good starting point for what I would like to do!

You printed the pagePos.x, pagePos.y and bbox.width, bbox.height. Do you have some more information on what the resulting numbers mean? E.g. what unit they are in?

In the end I would like to find for each printed element the position in the score, do you think that is possible via plugin?

Thanks again and kind regards

In reply to by Frank Revolle

all type of elements are in the online help of musescore plugins editor , rubric "element" :
.ACCIDENTAL, .ACCIDENTAL, .AMBITUS, .ARPEGGIO, .BAGPIPE_EMBELLISHMENT, .BAR_LINE, .BEAM, .BEND, .BRACKET, .BREATH, .CHORD, .CHORDLINE, .CLEF, .COMPOUND, .DYNAMIC, .ELEMENT, .ELEMENT_LIST, .FBOX, .FIGURED_BASS, .FINGERING, .FRET_DIAGRAM, .FSYMBOL, .GLISSANDO, .GLISSANDO_SEGMENT, .HAIRPIN, .HAIRPIN_SEGMENT, .HARMONY, .HBOX, .HOOK, .ICON, .IMAGE, .INSTRUMENT_CHANGE, .INSTRUMENT_NAME, .JUMP, .KEYSIG, .LASSO, .LAYOUT_BREAK, .LEDGER_LINE, .LINE, .LYRICS, .LYRICSLINE, .LYRICSLINE_SEGMENT, .MARKER, .MEASURE, .MEASURE_LIST, .NOTE, .NOTEDOT, .NOTEHEAD, .NOTELINE, .OSSIA, .OTTAVA, .OTTAVA_SEGMENT, .PAGE, .PEDAL, .PEDAL_SEGMENT, .REHEARSAL_MARK, .REPEAT_MEASURE, .REST, .SEGMENT, .SELECTION, .SHADOW_NOTE, .SLUR, .SLUR_SEGMENT, .SPACER, .STAFF_LINES, .STAFF_LIST, .STAFF_STATE, .STAFF_TEXT, .STEM, .STEM_SLASH, .SYMBOL, .SYSTEM, .TAB_DURATION_SYMBOL, .TBOX, .TEMPO_TEXT, .TEXT, .TEXTLINE, .TEXTLINE_SEGMENT, .TIE, .TIMESIG, .TREMOLO, .TREMOLOBAR, .TRILL, .TRILL_SEGMENT, .TUPLET, .VBOX, .VOLTA, .VOLTA_SEGMENT

In reply to by Frank Revolle

Hello and thanks again for your help. I tried to adapt your code for my purposes and many things work fine but I got stuck with this:

I have a piano score with two staffs. When I print measure.pagePos.x, measure.pagePos.y and measure.bbox.width, measure.bbox.height, I get for example (x, y, w, h) = (9.3, 24.2, 33.1546, 16.5). I am trying to get an ltrb (left,top, right, bottom) format in pixels. My results are then ltrb = (194, 504, 884, 847).
The first three values make sense but the bottom value is way off.
measure.bbox.height returns 16.5. Shouldn't that be 4 if the definition of a tenth is a tenth of the distance between staff lines?
Another thing that confuses me, is that the results are the same whether I use cursor.staffIdx = 0 or cursor.staffIdx = 1. Shouldn't there be a difference?
Thanks for all help

In reply to by Marc Sabatella

Running MuseScore with the "-d" command helped a bit to understand the bounding-boxes.
I still do not know how I should imagine the bounding-box of a measure. Intuitevely I would guess that the bounding box of a measure corresponds to the left-barline, top-staffline, right-barline and bottom-staffline that enclose it but again this seems not to be the case. Is there a documentation that says how these bounding-boxes look like?
Is there another way to find the boundaries of measures etc?
I also tried parsing MusicXML but as you have seen in another post that didn't really help me either.

Thanks for your help

In reply to by stusimon

Remember, a measure potentially spans multiple staves, and potentially includes items not physically on the staff. I'm not sure it is good to depend on these details. Can we step back a bit and get a better understanding of the actual problem you are trying to solve?

In reply to by Marc Sabatella

I am working on a machine learning project for optical music recognition. For the training set I need the position of elements like measures, notes etc. in an image of a score.

My idea was that I could generate the scores in MuseScore and since MuseScore places each element on a sheet i.e. when I make a png, it must know the position for each element and can directly generate a list of labels.

What I need in the end is a png of a score with a list of the form:
label - pos x0 - pos y0 - pos x1 - pos y1
label .....

where the label can be a measure, a note head etc. and pos x0, y0 is the coordinates of the top left corner and pos x1 y1 of the bottom right corner.

In reply to by stusimon

That helps. But now, let's think about this: what exactly do you mean by a "measure"? As I said, logically, it includes everything across multiple staves, including the spaces between the staves, notes above and below the staff, etc. So I'm guessing what you really want are the *staff lines*. Those are the *physical* (as opposed to logical) things you might possibly need to get positions of. But probably better to get that from an SVG file than from a plugin within MuseScore.

In reply to by Marc Sabatella

Yes I guess in the end I'm more interested in the *physical* properties.
Parsing the SVG will for sure help me with measures etc. but the whole musical information is missing then.

I.e. for notes I would like to get the position, the pitch and the duration, and maybe even more information in future. Do you think it is possible (without too much of a hassle) to include this information to the svg when exporting from musescore?

Hi,
I am having pretty much the same problem but I just need the position (in Pixel) of every note. Which file are you investigating and in which editor?

Do you still have an unanswered question? Please log in first to post your question.