ASCII Scores ­­­­­

27

8

Digging around in the depths of your temp folder, you find some compositions for the piano. Unfortunately, these compositions were written with note names and durations only, and you only have access to a text terminal. Therefore, your task is to write a program to display the compositions as ASCII art.

Input

Your program should accept two strings as input. The first string will represent the notes of the top staff (with the treble clef), while the second string will represent the notes of the bottom staff.

The notes will be passed in scientific pitch notation. The top staff's notes will always be between C4 and C6 inclusive. The bottom staff's notes will always be between C2 and C4 inclusive.

Each note will come with a duration, which will be one of: 1, 2, 4, 8. These represent a whole note (semibreve), a half note (minim), a quarter note (crotchet), and an eighth note (quaver) respectively.

Notes of any other duration will never appear in the input.

How the note and duration is separated, and how each note is separated from other notes in the input is up to your discretion. The following is a sample input for the top staff:

E4/4 A4/8 C#5/8 E5/2

Here, the notes are separated by a space, and the duration is separated from the note with a forward slash. These delimeters are not fixed, and you may choose to change them or omit them altogether.

You may assume there is at least one note in each staff. There are no rests in the input.

Output

Your program is to output the score as ASCII art, conforming to the following descriptions.

The clefs should be the first thing at the left of your output (the distance between the two staves should not be changed):

       ^    
      | |   
------|/----
      /     
-----/|-----
    / |     
---/--__----
   | /  \   
---\-\|-|---
    \ | /   
------|-----
      |     
    \_/     





----___-----
   /   \ |  
---\---|----
       | |  
-------/----
      /     
-----/------

------------

A note's stem (the vertical line next to the circle) should point upwards if the note is below the middle line of a staff. It should point downwards if the note is above the middle line of a staff. If the note is on the middle line, then the stem may point in either direction. (The only exception to this is for the bonus, and occurs when connecting eighth notes, described later). The stem should begin on the line above/below the circle, and be 6 lines tall.

All types of notes except whole notes have stems. The flag of an eighth note is represented by two forward slashes on different lines (see example notes below).

A filled in note head (for quarter and eighth notes) is represented by (@). An empty note head (for half and whole notes) is represented by ( ).

Accidentals (sharps, flats, naturals) should be placed as shown in the example notes, with exactly one character between the right side of the accidental and the left side of the note head.

Ledger lines should be used when necessary, and should be 7 characters in length, centered around the note head.

Each note should be 12 characters wide.

Example notes:

                                         |_                    
                                         |_| ( )                
                      |                    |------              
                      |                                         
------------ ---------|-- ------------ ------------ ------------
                      |                                         
------------ ---------|-- ---|-------- ------------ ------------
      (@)    _|_|_    |      |_                                 
-----|------ _|_|_-( )--- ---|/-(@)--- ------------ ---------|\-
     |        | |              |                             | \
-----|------ ------------ -----|------ ------------ ---------|--
     |                         |                             |  
-----|------ ------------ -----|------ ------------ ---------|--
     |                         | /                           | 
                               |/                       --(@)--

quarter note half note    eighth note  whole note   eighth note
              sharped      flatted      natural

After the 12-character note, leave 2 * 12 - 12 = 12 characters blank (either or - depending on the line) if the note is a quarter note. If the note is a half note, leave 4 * 12 - 12 = 36 characters blank. If the note is a whole note, leave 8 * 12 - 12 = 84 characters blank. Do not add extra characters for eighth notes.

At the end of each measure (96 characters after either the clef or bar line), output a bar line. This is done by going down every character between the uppermost and bottom-most lines (inclusive), and replacing with | and - with +. (See example output at bottom of question).

At the end of the piece, output the music end by outputting 3 bar lines in a row, but with a space between the first and second. That is:

+-++
| ||
+-++
| ||
. .. 
. .. 

Note that sharps, flats, and naturals last until the end of the measure. The natural sign should only be used to cancel out a sharp or flat used earlier in the same measure.

For the purpose of this task, sharps, flats, and naturals only affect a note in one octave, and in one clef (a sharp on A5 does not cause A4 to be sharped, and a flat on C4 in the top staff does not cause C4 in the bottom staff to be flatted).

Information for the bonus only

The bonus involves properly connecting eighth notes.

When there are two consecutive eighth notes, aligned to the quarter note beat (in other words, the number of characters before the first eighth note is a multiple of 24), the two eighth notes are to be connected.

Let note A be the note farthest from the middle of the staff. If both notes are the same distance from the middle, either note may be note A. Let the other note be note B.

The direction of both stems should be the direction of the stem of note A.

The stem of one of the notes should be 6 lines tall (as is the case for stems in general), and the stem of the other note should be extended to the end of the stem of the other note.

The stems should be connected with _.

Example connected eighth notes:

                                   ___________                            
                                  |           |                           
                                  |           |                           
                                  |           |                           
                                  |           |       --(@)--             
                            |     |           |        |                  
------------------------ ---|_----|-----------|-- -----|------------------
                            |/ (@)            |        |        |_        
------------------------ ---------------------|-- -----|--------|_|-(@)---
      (@)   _|_|_                             |        |          ||      
-----|------_|_|_-(@)--- ---------------------|-- -----|-----------|------
     |       | | |                            |        |           |      
-----|-----------|------ ---------------------|-- -----|-----------|------
     |           |                            |        |           |      
-----|-----------|------ ------------------(@)--- -----|___________|------
     |           |                                                        
     |___________|                                                        

Example input and output

Input:

A#4/4 G#4/4 F#4/2 A#4/4 G#4/4 F#4/2 F#4/8 F#4/8 F#4/8 F#4/8 G#4/8 G#4/8 G#4/8 G#4/8 A#4/4 G#4/4 F#4/2
A#3/4 G#3/4 F#3/2 A#3/4 G#3/4 F#3/2 F#3/2 G#3/2 F#3/4 E#3/4 F#3/2

Output:

       ^                                                                                                                                                                                                                                                                                                                                                                                                                      
      | |            |                                                                                                |                                                                                                                                                                                                 |                                                                                          
------|/-------------|-----------------------|--------------------------------------------------------------+---------|-----------------------|--------------------------------------------------------------+----------___________-------------___________------------|\----------|\----------|\----------|\-+---------|-----------------------|--------------------------------------------------------------+-++
      /              |                       |                       |                                      |         |                       |                       |                                      |         |           |           |           |           | \         | \         | \         | \|         |                       |                       |                                      | ||
-----/|--------------|-----------------------|-----------------------|--------------------------------------+---------|-----------------------|-----------------------|--------------------------------------+---------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|--+---------|-----------------------|-----------------------|--------------------------------------+-++
    / |              |                       |                       |                                      |         |                       |                       |                                      |         |           |           |           |           |           |           |           |  |         |                       |                       |                                      | ||
---/--__----_|_|_----|-----------------------|-----------------------|--------------------------------------+_|_|_----|-----------------------|-----------------------|--------------------------------------+---------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|--+_|_|_----|-----------------------|-----------------------|--------------------------------------+-++
   | /  \   _|_|_ (@)               _|_|_    |                       |                                      |_|_|_ (@)               _|_|_    |                       |                                      |         |           |           |           |  _|_|_    |           |           |           |  |_|_|_ (@)               _|_|_    |                       |                                      | ||
---\-\|-|----|-|--------------------_|_|_-(@)---------------_|_|_----|--------------------------------------+-|-|--------------------_|_|_-(@)---------------_|_|_----|--------------------------------------+_|_|_----|-----------|-----------|-----------|--_|_|_-(@)---------(@)---------(@)---------(@)---+-|-|--------------------_|_|_-(@)---------------_|_|_----|--------------------------------------+-++
    \ | /                            | |                    _|_|_ ( )                                       |                         | |                    _|_|_ ( )                                       |_|_|_ (@)         (@)         (@)         (@)    | |                                            |                         | |                    _|_|_ ( )                                       | ||
------|------------------------------------------------------|-|--------------------------------------------+-------------------------------------------------|-|--------------------------------------------+-|-|--------------------------------------------------------------------------------------------+-------------------------------------------------|-|--------------------------------------------+-++
      |                                                                                                     |                                                                                                |                                                                                                |                                                                                                | ||
    \_/                                                                                                     |                                                                                                |                                                                                                |                                                                                                | ||
                                                                                                            |                                                                                                |                                                                                                |                                                                                                | ||
                                                                                                            |                                                                                                |                                                                                                |                                                                                                | ||
                                                                                                            |                                                                                                |                                                                                                |                                                                                                | ||
                                                                                                            |                                                                                                |                                                                                                |                                                                                                | ||
            _|_|_                                                                                           |_|_|_                                                                                           |                                                                                                |                                                                                                | ||
----___-----_|_|_-(@)---------------_|_|_-------------------------------------------------------------------+_|_|_-(@)---------------_|_|_-------------------------------------------------------------------+------------------------------------------------_|_|_-------------------------------------------+------------------------------------------------------------------------------------------------+-++
   /   \ |   | | |                  _|_|_ (@)               _|_|_                                           | | | |                  _|_|_ (@)               _|_|_                                           |_|_|_                                           _|_|_ ( )                                       |_|_|_                                                                                           | ||
---\---|---------|-------------------|-|-|------------------_|_|_-( )---------------------------------------+-----|-------------------|-|-|------------------_|_|_-( )---------------------------------------+_|_|_-( )----------------------------------------|-|-|------------------------------------------+_|_|_ (@)---------------_|_|_-------------------------( )---------------------------------------+-++
       | |       |                       |                   | | |                                          |     |                       |                   | | |                                          | | | |                                               |                                          | | | |                  _|_|_ (@)                    |                                          | ||
-------/---------|-----------------------|-----------------------|------------------------------------------+-----|-----------------------|-----------------------|------------------------------------------+-----|-----------------------------------------------|------------------------------------------+-----|-------------------|-|-|-----------------------|------------------------------------------+-++
      /          |                       |                       |                                          |     |                       |                       |                                          |     |                                               |                                          |     |                       |                       |                                          | ||
-----/-----------|-----------------------|-----------------------|------------------------------------------+-----|-----------------------|-----------------------|------------------------------------------+-----|-----------------------------------------------|------------------------------------------+-----|-----------------------|-----------------------|------------------------------------------+-++
                                         |                       |                                          |                             |                       |                                          |     |                                               |                                          |     |                       |                       |                                          | ||
-----------------------------------------------------------------|------------------------------------------+-----------------------------------------------------|------------------------------------------+-----|------------------------------------------------------------------------------------------+-----|-----------------------|-----------------------|------------------------------------------+-++
                                                                                                                                                                                                                                                                                                                                            |                                                                      

For the sake of demonstration, in the third measure, the F# eighth notes are connected, whereas the G# eighth notes are not. Your program should either connect the eighth notes whenever applicable (for the bonus), or leave them all disconnected.

Other information

  • Any amount of trailing whitespace/lines is acceptable.
  • There should be no extra whitespace before the clefs, nor whitespace between notes. Any number of blank lines may be emitted before/after the output.
  • The bonus for connecting eighth notes is 0.75 * number of bytes of source code.
  • This is , so shortest code wins.

es1024

Posted 2015-07-24T21:47:26.347

Reputation: 8 953

You've not filled in the quaver on middle C (assuming treble cleff) in one of your examples. – Muzer – 2015-07-24T21:50:55.147

@Muzer Thanks, fixed. – es1024 – 2015-07-24T21:53:05.667

Also, I have to say, what a wonderful challenge. I don't have time myself to write an entry at the moment but I'm very interested to see what people come up with. (PS: Is it bad that I instantly recognised the tune? ;)) – Muzer – 2015-07-24T21:55:57.780

@steveverrill You are correct, fixed. – es1024 – 2015-07-25T02:15:54.867

7This is awesome! Hard, but awesome. – kirbyfan64sos – 2015-07-25T02:16:55.823

Will we be required to implement both staves? – MayorMonty – 2015-07-25T18:07:41.790

@SpeedyNinja yes, both staves are required – es1024 – 2015-07-25T22:22:02.637

Great challenge! One thing I noticed is that in the example notes some of the stems are facing the wrong way – MCMastery – 2015-08-02T21:20:37.150

I might just code this without making it short lol – Premier Bromanov – 2015-08-07T02:40:05.157

The final note of the bass clef line in the data is a quarter note, when it should be a half note (as shown in your demo)

(i.e. the final F#3/4 should be F#3/2) – Taylor Lopez – 2015-09-16T22:20:04.827

@iAmMortos Thanks, fixed. – es1024 – 2015-09-16T22:47:25.300

Answers

15

Python - 8.85... KB (9369 9066 B)

HaHA! Fastest gun in the... West?

This is FAR from being properly golfed, but it at least functions. At the moment of posting this, it's the shortest entry, so... yay?

Let me start by saying that I've never golfed anything this big, and I don't even know where to START.

This entry does not contain the bonus points yet, but I'd still like to add that before golfing.

I basically wrote a kind of ASCII rendering class that stores the characters that make up each "block" in a 2D format so that any number of different "symbols" could be drawn over the 2D grid. For note blocks, the staff was drawn first, then any staff extending bars as necessary, then the note head, followed by the stem and tail (where applicable) and last the accidental.

When given the following two inputs for trebble and bass clefs:

A#4/4 G#4/4 F#4/2 A#4/4 G#4/4 F#4/2 F#4/8 F#4/8 F#4/8 F#4/8 G#4/8 G#4/8 G#4/8 G#4/8 A#4/4 G#4/4 F#4/2
A#3/4 G#3/4 F#3/2 A#3/4 G#3/4 F#3/2 F#3/2 G#3/2 F#3/4 E#3/4 F#3/2

The following output results:

       ^                                                                                                                                                                                                                                                                                                                                                                                                           
      | |            |                                                                                                |                                                                                                                                                                                                 |                                                                                          
------|/-------------|-----------------------|--------------------------------------------------------------+---------|-----------------------|--------------------------------------------------------------+---------------------------------------------------------|\----------|\----------|\----------|\-+---------|-----------------------|--------------------------------------------------------------+-++
      /              |                       |                       |                                      |         |                       |                       |                                      |         |\          |\          |\          |\          | \         | \         | \         | \|         |                       |                       |                                      | ||
-----/|--------------|-----------------------|-----------------------|--------------------------------------+---------|-----------------------|-----------------------|--------------------------------------+---------|-\---------|-\---------|-\---------|-\---------|-----------|-----------|-----------|--+---------|-----------------------|-----------------------|--------------------------------------+-++
    / |              |                       |                       |                                      |         |                       |                       |                                      |         |           |           |           |           |           |           |           |  |         |                       |                       |                                      | ||
---/--__----_|_|_----|-----------------------|-----------------------|--------------------------------------+_|_|_----|-----------------------|-----------------------|--------------------------------------+---------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|--+_|_|_----|-----------------------|-----------------------|--------------------------------------+-++
   | /  \   _|_|_ (@)               _|_|_    |                       |                                      |_|_|_ (@)               _|_|_    |                       |                                      |         |           |           |           |  _|_|_    |           |           |           |  |_|_|_ (@)               _|_|_    |                       |                                      | ||
---\-\|-|----|-|--------------------_|_|_-(@)---------------_|_|_----|--------------------------------------+-|-|--------------------_|_|_-(@)---------------_|_|_----|--------------------------------------+_|_|_----|-----------|-----------|-----------|--_|_|_-(@)---------(@)---------(@)---------(@)---+-|-|--------------------_|_|_-(@)---------------_|_|_----|--------------------------------------+-++
    \ | /                            | |                    _|_|_ ( )                                       |                         | |                    _|_|_ ( )                                       |_|_|_ (@)         (@)         (@)         (@)    | |                                            |                         | |                    _|_|_ ( )                                       | ||
------|------------------------------------------------------|-|--------------------------------------------+-------------------------------------------------|-|--------------------------------------------+-|-|--------------------------------------------------------------------------------------------+-------------------------------------------------|-|--------------------------------------------+-++
      |                                                                                                     |                                                                                                |                                                                                                |                                                                                                | ||
    \_/                                                                                                     |                                                                                                |                                                                                                |                                                                                                | ||
                                                                                                            |                                                                                                |                                                                                                |                                                                                                | ||
                                                                                                            |                                                                                                |                                                                                                |                                                                                                | ||
                                                                                                            |                                                                                                |                                                                                                |                                                                                                | ||
                                                                                                            |                                                                                                |                                                                                                |                                                                                                | ||
            _|_|_                                                                                           |_|_|_                                                                                           |                                                                                                |                                                                                                | ||
----___-----_|_|_-(@)---------------_|_|_-------------------------------------------------------------------+_|_|_-(@)---------------_|_|_-------------------------------------------------------------------+------------------------------------------------_|_|_-------------------------------------------+------------------------------------------------------------------------------------------------+-++
   /   \ |   | | |                  _|_|_ (@)               _|_|_                                           | | | |                  _|_|_ (@)               _|_|_                                           |_|_|_                                           _|_|_ ( )                                       |_|_|_                                                                                           | ||
---\---|---------|-------------------|-|-|------------------_|_|_-( )---------------------------------------+-----|-------------------|-|-|------------------_|_|_-( )---------------------------------------+_|_|_-( )----------------------------------------|-|-|------------------------------------------+_|_|_-(@)---------------_|_|_-------------------------( )---------------------------------------+-++
       | |       |                       |                   | | |                                          |     |                       |                   | | |                                          | | | |                                               |                                          | | | |                  _|_|_ (@)                    |                                          | ||
-------/---------|-----------------------|-----------------------|------------------------------------------+-----|-----------------------|-----------------------|------------------------------------------+-----|-----------------------------------------------|------------------------------------------+-----|-------------------|-|-|-----------------------|------------------------------------------+-++
      /          |                       |                       |                                          |     |                       |                       |                                          |     |                                               |                                          |     |                       |                       |                                          | ||
-----/-----------|-----------------------|-----------------------|------------------------------------------+-----|-----------------------|-----------------------|------------------------------------------+-----|-----------------------------------------------|------------------------------------------+-----|-----------------------|-----------------------|------------------------------------------+-++
                                         |                       |                                          |                             |                       |                                          |     |                                               |                                          |     |                       |                       |                                          | ||
-----------------------------------------------------------------|------------------------------------------+-----------------------------------------------------|------------------------------------------+-----|------------------------------------------------------------------------------------------+-----|-----------------------|-----------------------|------------------------------------------+-++

Here's a working version of the code with some faux input code since this particular python web IDE didn't allow for simulated input or separate files.

grandstaff.py

Contains both trebble and bass clef staffs, and handles drawing the barlines between the two.

class GrandStaff:
  def __init__(self, trebbleStr, bassStr):
    self.trebbleStr = trebbleStr
    self.bassStr = bassStr
    self.trebbleStaff = Staff("trebble", self.trebbleStr)
    self.bassStaff = Staff("bass", self.bassStr)
    self.bassOffset = 16

    self.lines = {}
    maxmin = self.trebbleStaff.getYExtremes()
    for y in range(maxmin[0], maxmin[1] + 1):
      self.lines[y] = self.trebbleStaff.lines[y]
    maxmin = self.bassStaff.getYExtremes()
    for y in range(maxmin[0], maxmin[1] + 1):
      self.lines[y + self.bassOffset] = self.bassStaff.lines[y]

    for y in range(5, 12):
      self.lines.setdefault(y, [" " for x in range(len(self.trebbleStaff.getLineStr(0)))])
      xpos = 0
      for block in self.trebbleStaff.getBlocks():
        if block.type == "barline":
          self.lines[y][xpos] = '|'
        elif block.type == "finalDoubleBarline":
          for x in [0, 2, 3]:
            self.lines[y][xpos + x] = '|'
        xpos += block.width

  def __str__(self):
    outstr = ""
    maxmin = sorted(self.lines.keys())
    for y in range(maxmin[0], maxmin[-1]):
      self.lines.setdefault(y, [" " for x in range(len(self.trebbleStaff.getLineStr(0)))])
      outstr += ''.join(self.lines[y]) + '\n'

        return outstr

staff.py

A single staff. Contains an array of "Block" objects that represent notes, barlines, clefs, etc. This class handles the spacing of the notes and insertion of the barlines at the appropriate places.

class Staff:
  def __init__(self, clef, inputStr):
    self.clef = clef
    self.inStr = inputStr
    self.lines = {}
    self.__blocks = []
    if clef == "trebble":
      self.__blocks.append(Block("trebbleClef"))
    elif clef == "bass":
      self.__blocks.append(Block("bassClef"))

    notes = inputStr.split(" ");
    measureLength = 0
    for note in notes:
      if measureLength >= 1:
        self.__blocks.append(Block("barline"))
        measureLength -= 1
      parts = note.split("/")
      noteLength = 0
      if len(parts) == 2:
          noteLength += 1 / float(parts[1])
          measureLength += noteLength
      self.__blocks.append(Block("note", note, self.clef))
      for n in range(int(noteLength * 8) - 1):
          self.__blocks.append(Block("staff"))
    self.__blocks.append(Block("finalDoubleBarline"))

    sharps = []
    flats = []
    naturals = []
    for block in self.__blocks:
      if block.type == "note":
        val = block.value

        if block.isSharp:
          if val not in sharps:
            sharps.append(val)
          else:
            block.clearAccidental()
        elif block.isFlat:
          if val not in flats:
            flats.append(val)
          else:
            block.clearAccidental()
        else:
          if val in sharps:
            sharps.remove(val)
            block.setNatural()
          if val in flats:
            flats.remove(val)
            block.setNatural()

      elif block.type == "barline" or block.type == "finalDoubleBarline":
        sharps = []
        flats = []

    maxmin = self.getYExtremes()
    for y in range(maxmin[0], maxmin[1] + 1):
      self.lines[y] = list(self.getLineStr(y))

  def getLineStr(self, lineNum):
    outstr = ""
    for block in self.__blocks:
      outstr += block.getLineStr(lineNum)
    return outstr

  def getYExtremes(self):
    maxmin = [0, 0]
    for block in self.__blocks:
      mm = block.getYExtremes()
      maxmin[0] = mm[0] if mm[0] < maxmin[0] else maxmin[0]
      maxmin[1] = mm[1] if mm[1] > maxmin[1] else maxmin[1]
    return maxmin

  def getBlocks(self):
    return self.__blocks

  def __str__(self):
    maxmin = self.getYExtremes()
    outstr = ""
    for line in range(maxmin[0], maxmin[1] + 1):
      outstr += self.getLineStr(line) + "\n"
    return outstr

block.py

This class handles the "rendering" (or perhaps composition) of the ASCII text. Inside are the hard-coded symbols such as the trebble and bass clefs, a blank staff, barlines, accidentals, and the necessary pieces to build notes of different durations. These are "drawn" onto a 2D grid of character "pixels" in sequential order to produce the final ASCII block.

class Block:

  __staff = [[range(-4, 5, 2),"w",'-']]
  __trebble = [[-6, 7, '^'], [range(-5, 6), 6, '|'], [-5, 8, '|'], [-4, 7, '/'], [-3, 6, '/'], [-2, 5, '/'], [-1, 4, '/'], [0, 3, '/'], [0, range(6,8), '_'], [1, 3, '|'], [1, 5, '/'], [1, 6, ' '], [1, 8, '\\'], [2, 3, '\\'], [2, 5, '\\'], [2, 8, '|'], [3, 4, '\\'], [3, 8, '/'], [6, 4, '\\'], [6, 5, '_'], [6, 6, '/']]
  __bass = [[-4, range(4, 7), '_'], [-3, 3, '/'], [-3, 7, '\\'], [range(-3, 0, 2), 9, '|'], [-2, 3, '\\'], [range(-2, 0), 7, '|'], [0, 7, '/'], [1, 6, '/'], [2, 5, '/']]
  __sharp = [[range(-1, 1), range(0, 5), '_'], [range(-1, 2), range(1, 4, 2), '|']]
  __flat = [[range(-2, 1), 3, '|'], [-1, 4, '_'], [0, 4, '/']]
  __natural = [[range(-1, 1), 2, '|'], [range(-1, 1), 3, '_'], [range(0, 2), 4, '|']]
  __noteOpen = [[0, 6, '('], [0, 7, ' '], [0, 8, ')']]
  __noteClosed = [[0, 6, '('], [0, 7, '@'], [0, 8, ')']]
  __stemUp = [[range(-6, 0), 9, '|']]
  __stemDown = [[range(1, 7), 5, '|']]
  __eighthTailUp = [[-6, 10, '\\'], [-5, 11, '\\']]
  __eighthTailDown = [[6, 6, '/'], [5, 7, '/']]
  __barline = [[range(-4, 5), 0, '+'], [range(-3, 4, 2), 0, '|']]
  __finalBarline = [[range(-4, 5, 2), [0, 2, 3], '+'], [range(-3, 4, 2), [0, 2, 3], '|'], [range(-4, 5, 2), 1, '-']]
  __staffExtender = [[0, range(4, 11), '-']]

  def __init__(self, type, notestr = "", clef = "trebble"):
    if type == "note":
      self.clef = clef
      self.notestr = notestr.upper().split('/')[0]
      self.duration = int(notestr.upper().split('/')[1])
      self.isSharp = True if notestr[1] == '#' else False
      self.isFlat = True if notestr[1] == 'b' else False
      self.isNatural = False;
      self.__clefCallibration = {"trebble":{"A":1,"B":0,"C":6,"D":5,"E":4,"F":3,"G":2,"octave":4}, "bass":{"A":-4,"B":-5,"C":1,"D":0,"E":-1,"F":-2,"G":-3,"octave":3}}
      self.value = self.__clefCallibration[self.clef][self.notestr[0]] + ((self.__clefCallibration[self.clef]["octave"] - int(self.notestr[-1])) * 7)

    self.lines = {}
    self.type = type

    self.__drawBlock()

  def __drawBlock(self):
    self.lines = {}
    if self.type == "note":
      self.width = 12
      self.__draw(self.__staff)

      for y in range(6, self.value + 1, 2):
        self.__draw(self.__staffExtender, y)
      for y in range(-6, self.value - 1, -2):
        self.__draw(self.__staffExtender, y)

      if self.duration in [1, 2]:
        self.__draw(self.__noteOpen, self.value)
      else:
        self.__draw(self.__noteClosed, self.value)

      if self.duration != 1:
        if self.value < 0:
          self.__draw(self.__stemDown, self.value)
          if self.duration == 8:
            self.__draw(self.__eighthTailDown, self.value)
        else:
          self.__draw(self.__stemUp, self.value)
          if self.duration == 8:
            self.__draw(self.__eighthTailUp, self.value)

      if self.isSharp:
        self.__draw(self.__sharp, self.value)
      elif self.isFlat:
        self.__draw(self.__flat, self.value)
      elif self.isNatural:
        self.__draw(self.__natural, self.value)

      # self.__draw(__staffExtender, self.value)
    elif self.type == "staff":
      self.width = 12
      self.__draw(self.__staff)
    elif self.type == "barline":
      self.width = 1
      self.__draw(self.__barline)
    elif self.type == "finalDoubleBarline":
      self.width = 4
      self.__draw(self.__finalBarline)
    elif self.type == "trebbleClef":
      self.width = 12
      self.__draw(self.__staff)
      self.__draw(self.__trebble)
    elif self.type == "bassClef":
      self.width = 12
      self.__draw(self.__staff)
      self.__draw(self.__bass)

  def __draw(self, data, yOffset = 0):
    for char in data:
        self.__drawChunk(char, yOffset)

  def __drawChunk(self, char, yOffset = 0):
      xrng = []
      yrng = []
      drawChr = char[2]

      if type(char[0]) == int:
        yrng = [char[0]]
      elif type(char[0]) == list:
        yrng = char[0]
      else:
        print("ERROR: invalid y range input")

      if type(char[1]) == str:
        if char[1] == 'w':
          xrng = range(self.width)
      elif type(char[1]) == int:
        xrng = [char[1]]
      elif type(char[1]) == list:
        xrng = char[1]
      else:
        print("ERROR: invalid x range input")

      yrng = [y + yOffset for y in yrng]

      for y in yrng:
        self.lines.setdefault(y,[" " for i in range(self.width)])
        for x in xrng:
          self.lines[y][x] = drawChr

  def getLineStr(self, lineNum):
    return "".join(self.lines.setdefault(lineNum, [" " for i in range(self.width)]))

  def getYExtremes(self):
    k = sorted(self.lines.keys())
    return [k[0], k[-1]]

  def setSharp(self):
    self.isSharp = True
    self.isFlat = False
    self.isNatural = False
    self.__drawBlock()

  def setFlat(self):
    self.isSharp = False
    self.isFlat = True
    self.isNatural = False
    self.__drawBlock()

  def setNatural(self):
    self.isSharp = False
    self.isFlat = False
    self.isNatural = True
    self.__drawBlock()

  def clearAccidental(self):
    self.isSharp = False
    self.isFlat = False
    self.isNatural = False
    self.__drawBlock()

  def __str__(self):
    strOut = ""
    first = sorted(self.lines.keys())[0]
    last = sorted(self.lines.keys())[-1]
    for line in range(first, last + 1):
        strOut += self.getLineStr(line) + "\n"
    return strOut

main.py

This class simply "runs" the program by creating an instance of GrandStaff, passing it the given string inputs, and printing it's string value.

import sys
from block import *
from grandstaff import *
from staff import *

print (GrandStaff(sys.argv[1],sys.argv[2]))

Taylor Lopez

Posted 2015-07-24T21:47:26.347

Reputation: 411