Python NWC tool to reverse selected notes in a staff

Here’s the python code. Enjoy.

This code needs to import nwctxt.py see https://github.com/nwsw/nwc2ut-python

"""joeSelReverse.py Reverses the Notes that are selected in a NoteWorthy Composer staff

"""

import sys
import nwctxt # see https://github.com/nwsw/nwc2ut-python
import logging
logging.basicConfig(filename='joeNWC_PY.log',level=logging.DEBUG)  # DEBUG => print ALL msgs

clip = nwctxt.NWC2Clip()
PlayContext = nwctxt.NWC2PlayContext()
txtItemL = clip.Items
clipItemL = [nwctxt.NWC2ClipItem(txtItem) for txtItem in txtItemL]
first2 = clipItemL[:2]
reversedClipItemL = first2 + list(reversed(clipItemL[2:]))
reversedTextItemL = [item.ReconstructClipText() for item in reversedClipItemL]

print clip.GetClipHeader()
for textItem in reversedTextItemL[2:]:
    #print "clipItem.GetObjType()= %s, type(clipItem.GetObjType())= %s, "%(clipItem.GetObjType(), type(clipItem.GetObjType()))
    print textItem
print clip.GetClipFooter()


# TO DEBUG uncomment below to report to NWC STDOUT DIALOG & to CAUSE NO STAFF MODIFICATION
# sys.exit(nwctxt.NWC2RC_REPORT) 

Advertisements

Essential Music Links

Music Software

NoteWorthy Composer
A music notation software authoring tool for Windows.
music21
A Python Toolkit for Computer-Aided Musicology and Musical Analysis.
MuseScore
Create, play and print beautiful sheet music.
MusicXML
The standard open format for exchanging digital sheet music.
LilyPond
LilyPond is a music engraving program, devoted to producing the highest-quality sheet music possible.

Ear Training

The Musical Intervals Tutor
A study aid to help you learn the sounds of the basic music intervals from minor second to perfect octave.
Big Ears
The Original Online Ear Trainer!

Musicology

Dolmetsch Online
Music theory & history online.

How 2 Create a Python NWC User Tool

Preamble

Read Introduction to User Tool Development. Summary:

  • NWC Staff Clip input to your tool is from STDIN.
  • Main Output back to NWC Staff is to STDOUT.
  • User Interaction via “PROMPT” command line arguments used when calling your tool.

Create your Python Module

Make a Python script to do something to a NoteWorthy Composer clip. Here is a simple example. It copies the NWC clip into a file.

import sys
instr = sys.stdin.read()
f = open('joesFirstTestOut.txt', 'w'); f.write(instr); f.close()

Hook Up your Python Module with NWC

1. Copy your module to:
<path>\Program Files\NoteWorthy Composer 2\Scripts\
2. Hook up NWC to your module
2.1 In NWC Click Menu >> Tools >> User Tools
2.2 In User Tools dialog click "New" button
2.3 In User Tool Description dialog enter:

Group: joe                               <or whatever>
Name: joesFirst.py                       <or whatever>
Command: python scripts\joesFirst.py     <or python scripts\whatever>
Input Type: clip text
Options: 
    compress input:         [ ]
    returns file text:      [ ]
    long task handling:     [ ]
    prompts for user input: [ ]
2.4 In User Tool Description dialog click "OK" button
2.5 In User Tools dialog click "Close" button

Run your Python Module

2.1 In NWC select the staff for which you want the clip
2.2 In NWC Click Menu >> Tools >> User Tools
2.3 In User Tools dialog select your group & command
2.4 In User Tools dialog click "Run" button

N.B. For the example module, the file named 'joesFirstTestOut.txt' 
will appear:
    NOT IN <path>\Program Files\NoteWorthy Composer 2\Scripts\
    BUT IN <path>\Program Files\NoteWorthy Composer 2\Scripts\

For More Information about NWC2 Scripting

Read also Getting started with NWC2 User Tools

Great Comments about my code from MIT’s Music21 creator, Michael Scott Cuthbert

Big thanks to MIT’s Music21 creator, Dr. Michael Scott Cuthbert, for his encouraging comments about my code. It’s nice to know that I am staying current and being of service.

He left comments regarding the code that I published:

On Stack Overflow he said, “…your solution is so elegant that we’ve decided to include some code based on it in spirit in the next release (v1.1) of music21”.

On this blog he said, “Thanks for the great post and ideas! We’ll be incorporating something like this in a music21 midi.realtime module… Please feel free to post this to the music21list (Google Groups). People will be interested!” You can see the Google Group re-post here.

Lastly, let me again express my gratitude to Dr. Cuthbert for his encouragement.

Love and peace,

Joe