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$
  • 1
    $\begingroup$ Replace Number_Of_Nones_Following[i,U] with Count[U[[i ;;]], None] $\endgroup$ – Alexey Golyshev Mar 28 '16 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 Mar 28 '16 at 6:53
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

| improve this answer | |
$\endgroup$
  • $\begingroup$ LengthWhile +1 $\endgroup$ – garej Mar 28 '16 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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