7

If I define:

aNote[x_] := Sin[440 * 2 * Pi * x];

then this works (i.e. it produces a widget with a Play button, and clicking on this button produces an A note):

Play[aNote[x], {x, 0, 3.5}]

but this doesn't:

env[x_] := HeavisidePi[x - 1] + HeavisidePi[x - 2.5];
Play[env[x] aNote[x], {x, 0, 3.5}]

The latter just produces something like:

Sound[SampledSoundFunction[
  Function[{Play`Time24}, 
   Block[{x = 
      0. + 0.000125 Play`Time24}, ((HeavisidePi[x - 1] + 
          HeavisidePi[x - 2.5]) aNote[x] + 0.00024672) 1.00025]], 
  28000, 8000]]

...but no widget, and no sound.

The difference between the two is that in the second one, the sinusoidal sound wave is multiplied by an "envelope" consisting of two rectangular unit bumps of width 1, and centered at 1 and 2.5, respectively.

kjo
  • 11,717
  • 1
  • 30
  • 89

1 Answers1

5

You can produce an identical signal by using UnitBox instead of HeavisidePi:

aNote[x_] := Sin[440*2*Pi*x];
unv[x_] := UnitBox[x - 1] + UnitBox[x - 2.5];
Play[unv[x] aNote[x], {x, 0, 3.5}]

enter image description here

Use UnitStep and Piecewise for more sophisticated functions.

Not sure, but maybe the reason for your trouble is that HeavisidePi does not evaluate at 1/2:

HeavisidePi[.5]

HeavisidePi[0.5]

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Vitaliy Kaurov
  • 73,078
  • 9
  • 204
  • 355