6
$\begingroup$

I have a 1D Table, something like this, that I want to turn into musical notes:

    U = {10, None, None, 0, 2, 3, 5, None, 2, 0, 3, None, 7, None, None, 3, 2, 3, 7, None, None, 0, 5, None, 5, None, 2, 3, None, None, 7, None}

'None' signifies a rest. I'm currently using this line to do that (dur is the duration of each note, len is the length of U):

Sound[Table[SoundNote[U[[i]], {dur*i, dur*(i + 1)}], {i, 1, len}]]

This works, but I'd like to extend the duration of the notes preceding a rest (one or more 'None's) to get a legato effect. So how can I write a function (pure or defined) that counts the number of consecutive None's immediately following a location, i, in the list, U (if there are any at all)? So it would work something like this:

Sound[Table[SoundNote[U[[i]], {dur*i, dur*(i + 1 + Number_Of_Consecutive_Nones_Following[i,U])}], {i, 1, len}]]

Thank you!!

$\endgroup$
2
  • 1
    $\begingroup$ Replace Number_Of_Nones_Following[i,U] with Count[U[[i ;;]], None] $\endgroup$ Commented Mar 28, 2016 at 6:33
  • $\begingroup$ Added clarification. I need a function that tells me how many consecutive Nones there are immediately after the ith location (if there are any at all), not the total number. $\endgroup$
    – Eriek
    Commented Mar 28, 2016 at 6:53

1 Answer 1

7
$\begingroup$
U = {10, None, None, 0, 2, 3, 5, None, 2, 0, 3, None, 7, None, None, 3, 2, 3, 7, None, None, 0, 5, None, 5, None, 2, 3, None, None, 7, None};

dur = 1; len = Length@U;

Sound[
 Table[
  SoundNote[
   U[[i]], {dur*i, dur*(i + 1 + LengthWhile[U[[i + 1 ;;]], # == None &])}], {i, 1, len}
  ]
 ]

enter image description here

$\endgroup$
1
  • $\begingroup$ LengthWhile +1 $\endgroup$
    – garej
    Commented Mar 28, 2016 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.