0
$\begingroup$

So, here's my code to return a play object after passing in frequency and duration as parameters. When I run it, it says

"Sound::ssnm: A good PlayRange could not be found since most of the samples are not evaluating to machine-sized real numbers."

And I really couldn't understand why after thinking for a really long time... Am I missing something? When I don't use module and directly generate play objects, the body of codes works fine...

 ADSR[t_, Ta_, Td_, Ts_, Tr_, Aa_, Ad_] := Module[{ma, md, mr},
   ma = Aa/Ta;
   md = (Aa - Ad)/(Ta - Td);
   mr = Ad/(Ts - Tr);
   If[t <= Ta, Return[Aa ( 1 - Sqrt[1 - (t/Ta)^2])], Null];
   If [t <= Td, Return[Aa + md (t - Ta)], Null];
   If [t <= Ts, Return[Ad], Null];
   If [t <= Tr, Return[Ad (1 - Sqrt[1 - ((t - Tr)/(Tr - Ts))^2])], 
    Return[0.]];
   ]; (*This is the new adsr envelope used in AM*)


MySound[k_, dur_] := 
 Module[{fundamental, carrier, chromaticOvertones, amplitudes, 
   mA, s, sound},
  fundamental = 440*2^(k/12);
  carrier = fundamental;
  chromaticOvertones = Table[2^(i/12), {i, 0, 12}];
  amplitudes = Table[1/E^i, {i, 1, Length[chromaticOvertones]}];
  mA[t_] = 
   Sum[amplitudes[[j]]*
     Sin[2*t*Pi*fundamental*chromaticOvertones[[j]]], {j, 1, 
     Length[chromaticOvertones]}];
  (*the amplitude of modulating signal in FM*)

  s[t_] := ADSR[t, .05, .1, .2, 1.5, 1.0, 0.7]*
    Sin[2*Pi*carrier*t + 
      mA[t, fundamental]*Cos[2*Pi*t*1.5 fundamental]*1/3];
  sound = Play[s[t, 440], {t, 0, dur}, SampleRate -> 8000];
  Return[sound];
  ]
p = MySound[1, .125]
EmitSound[p]

Here's the output from the kernel. And I don't understand it.

{; Return[
   Sound[SampledSoundFunction[
     Function[{Play`Time17}, 
      Block[{t = 
         0. + 0.000125 Play`Time17}, (s$181163[t, 440] + 0.) 1.]], 
     1000, 8000]]], (2^(1/12)) ; 
  Return[Sound[
    SampledSoundFunction[
     Function[{Play`Time17}, 
      Block[{t = 
         0. + 0.000125 Play`Time17}, (s$181163[t, 440] + 0.) 1.]], 
     1000, 8000]]], (2^(1/6)) ; 
  Return[Sound[
    SampledSoundFunction[
     Function[{Play`Time17}, 
      Block[{t = 
         0. + 0.000125 Play`Time17}, (s$181163[t, 440] + 0.) 1.]], 
     1000, 8000]]], (2^(1/4)) ; 
  Return[Sound[
    SampledSoundFunction[
     Function[{Play`Time17}, 
      Block[{t = 
         0. + 0.000125 Play`Time17}, (s$181163[t, 440] + 0.) 1.]], 
     1000, 8000]]], (2^(1/3)) ; 
  Return[Sound[
    SampledSoundFunction[
     Function[{Play`Time17}, 
      Block[{t = 
         0. + 0.000125 Play`Time17}, (s$181163[t, 440] + 0.) 1.]], 
     1000, 8000]]], (2^(5/12)) ; 
  Return[Sound[
    SampledSoundFunction[
     Function[{Play`Time17}, 
      Block[{t = 
         0. + 0.000125 Play`Time17}, (s$181163[t, 440] + 0.) 1.]], 
     1000, 8000]]], Sqrt[2] ; 
  Return[Sound[
    SampledSoundFunction[
     Function[{Play`Time17}, 
      Block[{t = 
         0. + 0.000125 Play`Time17}, (s$181163[t, 440] + 0.) 1.]], 
     1000, 8000]]], (2^(7/12)) ; 
  Return[Sound[
    SampledSoundFunction[
     Function[{Play`Time17}, 
      Block[{t = 
         0. + 0.000125 Play`Time17}, (s$181163[t, 440] + 0.) 1.]], 
     1000, 8000]]], (2^(2/3)) ; 
  Return[Sound[
    SampledSoundFunction[
     Function[{Play`Time17}, 
      Block[{t = 
         0. + 0.000125 Play`Time17}, (s$181163[t, 440] + 0.) 1.]], 
     1000, 8000]]], (2^(3/4)) ; 
  Return[Sound[
    SampledSoundFunction[
     Function[{Play`Time17}, 
      Block[{t = 
         0. + 0.000125 Play`Time17}, (s$181163[t, 440] + 0.) 1.]], 
     1000, 8000]]], (2^(5/6)) ; 
  Return[Sound[
    SampledSoundFunction[
     Function[{Play`Time17}, 
      Block[{t = 
         0. + 0.000125 Play`Time17}, (s$181163[t, 440] + 0.) 1.]], 
     1000, 8000]]], (2^(11/12)) ; 
  Return[Sound[
    SampledSoundFunction[
     Function[{Play`Time17}, 
      Block[{t = 
         0. + 0.000125 Play`Time17}, (s$181163[t, 440] + 0.) 1.]], 
     1000, 8000]]], 2 ; 
  Return[Sound[
    SampledSoundFunction[
     Function[{Play`Time17}, 
      Block[{t = 
         0. + 0.000125 Play`Time17}, (s$181163[t, 440] + 0.) 1.]], 
     1000, 8000]]]}
$\endgroup$
3
$\begingroup$

The following changes eliminate the error messages. In the MySound function definition:

  • Change mA[t_] = ... to ma[t_] := ... (I think you intended SetDelayed instead of Set).

  • There's a strange semicolon-like character at the end of chromaticOvertones = Table[2^(i/12), {i, 0, 12}];. Replace it with a semicolon.

The definitions of mA[t_] and s[t_] declare a single argument t_, but they are called with ma[t,fundamental] and s[t,440].

  • In ADSR[t,.05,.1,.2,1.5,1.0,0.7]*Sin[2*Pi*carrier*t + mA[t,fundamental] * Cos[2*Pi*t*1.5 fundamental]*1/3];, change mA[t,fundamental] to mA[t].

  • In Play[s[t, 400], {t, 0, dur}, SampleRate -> 8000], change s[t, 400] to s[t].

$\endgroup$
  • $\begingroup$ Thanks!! I didn't even notice that strange semicolon... It’s from the Chinese character set and looks similar to ;..... $\endgroup$ – Macrophage Jul 12 '18 at 23:20

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.