Saltar al contenido

Generador de partituras ASCII-art

La guía o código que encontrarás en este post es la resolución más fácil y válida que encontramos a tu duda o problema.

Solución:

SOGL V0.12, 178175174173172 171 bytes

l9*6«[email protected]*@¶¹┐∑:@┌ŗ4Ο"γ;]∑«;‽ΗmzΖH+īN D‼,ΨU‛y‚_○¤└yΨšI‘7n2∆╬5;" -o-”;l3=?Jζ2%Ƨ#bWGk+;Jz7m««:U+;W7«κArBb3>?Ζo*ŗ}a2?┌@ŗ}ē9*LI+a╬5b1>?4┐∙b8=?"■QD³‘┼}e9*5+a4-8a>?5+;2-;G↕№}╬5

Pruébelo aquí! (θ agregado para facilitar el uso; para ejecutarse como 171 bytes, se espera que la entrada esté en la pila)

Por lo que puedo decir, esto funciona, pero si encuentra algún problema, dímelo.

Explicación:

primera parte: creación de lienzos

l                                get the length of that array
 9*                              multiply by 9
   6«+                           add 12
      @*                         get that many spaces
        @¶                       push a space and a newline
          ¹                      put all the strings on the stack in an array
           ┐∑                    join with vertical bars
             :                   duplicate that string (which is a line with the ending barline but no staff)
              @┌ŗ                replace spaces with dashes (to make it a line with staff)
                 4Ο              encase 4 copies of the space lines in lines with the dashes
                   "...‘         push the G-clef without newlines
                        7n       split into an array of items of length 7
                          2∆╬5   at 1-indexed coordinates [2; -1] place the G-clef in the staff lines, extending the arrays size 
                              ;  get the input split on spaces back on top of the stack

segunda parte: bucle, colocación de la cabeza de nota

                        loop over the input split on spaces
" -o-”                    push a template for a note head and leger lines
      ;                   get the input optop
       l3=?              if the length of the input is 3, then
           J                pop the last letter off from the input
            ζ               get its unicode point
             2%             modulo 2
               Ƨ#bW         get its index in "#b"
                   G        get the template ontop
                    k       remove its 1st letter
                     +      join the replaced input and the template
                      ;     get the input back ontop to be consisntent with how the if started

sidequest: parse the rest of the inputs
J                  pop the last letter off of the remaining input string (the note), leaving the note length as string on the stack below
 z                 push the lowercase alphabet
  7m               get its first 7 letters
    ««             put the first 2 at the end
      :            duplicate it
       U+          append it uppercased to the original
         ;W        get the notes letter ontop and get its 1-indexed index in that just created string
           7«κ     subtract it from 14
              A    save on variable A
               r   convert the note length to a number
                B  save on variable B

b3>?    }          if b>3 (aka if note length is either 4 or 8)
    Ζo*ŗ             replace "o" with "*"
         a2?   }  if a divides by 2 (aka there isn't staff nor leger lines required)
             ┌@ŗ     replace "-" with " "

ē          push the value of variable E and after that increase it (default is user input number, which errors and defaults to 0)
 9*        multiply by 9
   LI+     increase by 11
      a    push variable a
       ╬5  at those positions (e*9+11, a) insert the note head template in the canvas

tercera parte: banderas y tallos

b1>?                      if b (note length)>1 (aka if the stem is needed at all)
    4┐∙                   get an array of 4 vertical bars
       b8=?       }       if b==8 (aka if the flag is needed)
           "■QD³‘           push "    |"
                 ┼          add verically-down-then-horizontally-right

e9*                       push e*9 (now e starts with 1 as it's been increased) (the X coordinate for the flag)
   5+                     add 5 to it
     a4-                  push a-4 (the Y coordinate, 4 less than the note head as arrays get inserted from the top-left corner)
        8a>?         }    if 8>a (aka if the flag needs to be rotated)
            5+              add 5 to the Y coordinate
              ;2-;          subtract 2 from the X coordinate
                  G         get the stem&flag or stem ontop
                   ↕№       reverse it vertically and mirror characters
                      ╬5  insert the array of the stem and maybe flag at those coordinates

JavaScript (ES6), 616 527 octetos

Gracias @shaggy por eliminar casi 90 bytes

No tenía ni idea de notas … hasta ahora, espero haberlo hecho bien.

f=i=>i.map((v,p)=>(k[e=(w=q+12)*(l="AGFEDCbagfedc".search(v[1]))+p*9+12]="o*"[(s=v[0])>3|0],l<1|l>11&&(k[e-1]=k[e+1]="-"),(t=v[2])&&(k[e-2]="b#"[t>"f"|0]),--s&&[1,2,3,4].map(i=>(k[(b=l<8)?e+w*i-1:e-w*i+1]="|",s>6&&( b?k[e+w*4]="/":k[e-w*4+2]="\",k[b?e+w*3+1:e-w*3+3]='|')))),k=[...`    /\  $s=" ".repeat(q=i.length*9)  
   | |  $s    
---|-|--$-
   |/   $ 
---/|---$l
  / |   $t
-|--|---$l
 | (| \ $t
-|--|--)$l
  \ | / $t
-----|--$l
     |  $s   
   *_/  $s`])&&k.join``

console.log(f(["8g","8Df","4cs","2C","1A"]))
.as-console-wrapper  max-height: 100% !important; top: 0 
.as-console-row:after  display: none !important; 

explicación

f=i=>i.map((v,p)=>( // for each note

  k[e=(w=q+12)*(l="AGFEDCbagfedc".search(v[1]))+p*9+12]= // position in 1D array to set the note to
  "o*"[(s=v[0])>3|0], // note value (either o or *)

  l<1|l>11&&(k[e-1]=k[e+1]="-"), // add leger line

  (t=v[2])&&(k[e-2]="b#"[t>"f"|0]), // add sharp or flat

  --s&&[1,2,3,4].map(i=> // add the 4 stem lines
                     (k[(b=l<8)?e+w*i-1:e-w*i+1]="|", // durration over eigth note => add stem

                      s>6&&( // if to add a flag
                        b?k[e+w*4]="/":k[e-w*4+2]="\", // add flag either on left or the right side

                        k[b?e+w*3+1:e-w*3+3]='|') // add the line after the flag
                     )
                    )
),
// template, extended to the final length with lines
k=[...`    /\  $s=" ".repeat(q=i.length*9)  
   | |  $s   
---|-|--$"
   |/   $"
---/|---$l
  / |   $t
-|--|---$l
 | (| \ $t
-|--|--)$l
  \ | / $t
-----|--$l
     |  $s   
   *_/  $s`])&&k.join``

Carbón, 180171168 163 octetos

F⁵⁺⸿⸿×-⁺²⁷×⁸№θ ↑⁹←↙↓⁹J⁴¦⁹↑⁶↗¹↑²↖¹↓↙¹↓³↙²↓³ M²↑(| ↘¹↙)↙¹↓²↙¹↑←_*F⪪θ «A⌕AGFEDCbagfedc§ι¹λJχλA⁺⁹χχ¿⁼³Lι§b#⁼§ι²s→P׳¬﹪λ²→P§o*›ι4¿›ι2¿›λ⁶«↗↑⁴¿›ι8«↘↘¹↓¹»»«↙↓⁴¿›ι8«↗↗¹↑¹

¡Pruébelo en línea! El enlace corresponde a una versión detallada del código. Explicación:

F⁵⁺⸿⸿×-⁺²⁷×⁸№θ ↑⁹←↙↓⁹

Imprime el pentagrama.

J⁴¦⁹↑⁶↗¹↑²↖¹↓↙¹↓³↙²↓³ M²↑(| ↘¹↙)↙¹↓²↙¹↑←_*

Imprime la clave.

F⪪θ «

Recorre cada nota.

A⌕AGFEDCbagfedc§ι¹λ

Encuentra la coordenada Y de la nota.

JχλA⁺⁹χχ

Esto es realmente engañoso: χ es una variable predefinida en 10, que es exactamente la coordenada X de la accidental de la primera nota, si la tiene. Después de saltar a esa posición, se le agrega 9, que representa la siguiente posición de nota.

¿⁼³Lι§b#⁼§ι²s→

Imprime el accidental, si lo hay.

P׳¬﹪λ²→P§o*›ι4

Imprima las líneas del libro mayor si es necesario y la nota. De hecho, la línea se imprime en cualquier coordenada y uniforme, aunque, por supuesto, imprimirla sobre el pentagrama no tiene ningún efecto.

¿›ι2

Nada más que hacer por las semibreves.

¿›λ⁶«

Para notas por debajo del punto medio,

↗↑⁴

dibuja el tallo hacia arriba,

¿›ι8«↘↘¹↓¹

y la bandera de corcheas.

»»«

Para notas por encima del punto medio,

↙↓⁴

dibuja el tallo hacia abajo,

¿›ι8«↗↗¹↑¹

y la bandera de corcheas.

Si haces scroll puedes encontrar los informes de otros administradores, tú además tienes la libertad de dejar el tuyo si lo deseas.

¡Haz clic para puntuar esta entrada!
(Votos: 3 Promedio: 4.7)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *