4
$\begingroup$

I ran this code in 2019 to give my students a simple Fourier filtering exercise. It generates some multi-tone sound data, then plays the tone (allowing the user to pause the playing, since the tone gets annoying). Then we filter out the higher or lower tone and listen to the difference. I've posted below only the data generation and sound playing code. But now, four years later, the module won't pause when you toggle the play/pause icon. I haven't changed the code, so is this a version update problem?

tData = Table[i 0.00012, {i, 0, 1023}];
yData = Table[ 
   0.1 Sin[2 Pi 440 i 0.00012] + Sin[2 Pi 1400 i 0.00012], {i, 0, 
    1023}];
sound = ListPlay[yData, SampleRate -> 8333];
DynamicModule[{state = "pause"}, 
 Grid[{{Toggler[
     Dynamic@state, {"pause" -> Style["\[RightPointer]", 24], 
      "play" -> Style["\[FilledSmallSquare]", 16]}], 
    Dynamic@If[state == "play", 
      If[ScheduledTasks[] == {}, RunScheduledTask[EmitSound[sound]]]; 
      "playing", 
      If[ScheduledTasks[] != {}, 
       RemoveScheduledTask[ScheduledTasks[]]]; "paused"]}}]]
$\endgroup$

1 Answer 1

5
$\begingroup$

As of Version 11.2 ScheduledTasks, RunScheduledTask, and RemoveScheduledTask where superseded by the function Tasks. Currently I'm running Mathematica Verison 13.1 and the following code will toggle whith the dynamic play/pause icon.

sampleRate = 8000;
tData = Table[i /sampleRate, {i, 0, sampleRate - 1}];
yData = Table[0.1 Sin[2 Pi 440 i /sampleRate] + Sin[2 Pi 1400 i /sampleRate],{i,0,sampleRate - 1}];
plotData = Transpose[{tData, yData}];
ListPlot[plotData, Joined -> True, Frame -> True]
ListPlot[plotData, Mesh -> All, Joined -> True, 
MeshStyle -> {Black, PointSize -> Medium}, Frame -> True, 
PlotRange -> {{0, 0.01}, All}]
sound = AudioStream[Audio[ListPlay[yData, SampleRate -> sampleRate]]];
DynamicModule[{state = "pause"},
Grid[{{
 Toggler[
  Dynamic@state,
  {"pause" -> Style["\[RightPointer]", 24], 
   "play" -> Style["\[FilledSmallSquare]", 16]}
  ],
 Dynamic@If[state == "play",
  AudioPlay[sound, Looping -> True]; "Playing",
  AudioStop[]; "Paused"
  ]
}}]]

Note that I've opted to use AudioPlay and AudioStop (added in Version 11.2) leaving the module independent of Tasks.

$\endgroup$

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.