Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pomodoro] Make sound from generated code #77

Open
DamianSuess opened this issue Apr 21, 2019 · 1 comment
Open

[pomodoro] Make sound from generated code #77

DamianSuess opened this issue Apr 21, 2019 · 1 comment
Assignees
Labels
request New feature or request

Comments

@DamianSuess
Copy link
Collaborator

As a user, I want the Pomodoro timer to make sounds OnStart, OnComplete, and OnPause. However, I don't want to carry along heavy WAV or MP3 files; let's generate the sounds!

Reference:

@DamianSuess DamianSuess added the request New feature or request label Apr 21, 2019
@DamianSuess
Copy link
Collaborator Author

DamianSuess commented Apr 22, 2019

Did a quick test with NAudio, however, the v1.8.5 DLL from the NuGet is 450K. We don't need that much weight just to generate tones. NAudio is very nice, however, it may be overkill for this small project's needs.

I whipped up a small WinForms app with 7 signal type radio buttons in a GroupBox (hence the switch method).

    private void BtnSin_Click(object sender, EventArgs e)
    {
      var signal = new SignalGenerator()
      {
        Gain = 0.2,
        Frequency = 500,
        Type = SignalGeneratorType.Sin
      }
      .Take(TimeSpan.FromSeconds(2));

      using (var wo = new WaveOutEvent())
      {
        wo.Init(signal);
        wo.Play();

        while (wo.PlaybackState == PlaybackState.Playing)
        {
          Thread.Sleep(500);
        }
      }
    }

    private void BtnExploreTypes_Click(object sender, EventArgs e)
    {
      int duration = 2;
      SignalGeneratorType type = GetSignalType();

      if (type == SignalGeneratorType.Sweep)
        duration = 3;

      var signal = MakeSignal(type, duration);

      using (var wo = new WaveOutEvent())
      {
        wo.Init(signal);
        wo.Play();

        while (wo.PlaybackState == PlaybackState.Playing)
        {
          Thread.Sleep(500);
        }
      }
    }

    private ISampleProvider MakeSignal(SignalGeneratorType signalType, int duration)
    {
      var signal = new SignalGenerator()
      {
        Gain = 0.2,
        Frequency = 500,
        FrequencyEnd = 2000,
        SweepLengthSecs = 2,
        Type = signalType
      };
      // }.Take(TimeSpan.FromSeconds(duration));

      if (signalType == SignalGeneratorType.Sweep)
      {
        signal.FrequencyEnd = 2000;
        signal.SweepLengthSecs = 2;
      }

      var signalTake = signal.Take(TimeSpan.FromSeconds(duration));

      return signalTake;
    }

    private SignalGeneratorType GetSignalType(SignalGeneratorType def = SignalGeneratorType.Sin)
    {
      SignalGeneratorType type = SignalGeneratorType.Sin;
      RadioButton radioBtn = this.groupBox1.Controls.OfType<RadioButton>()
                                 .Where(x => x.Checked).FirstOrDefault();

      if (radioBtn != null)
      {
        switch (radioBtn.Name)
        {
          case "radioButton1":
            type = SignalGeneratorType.Triangle;
            break;

          case "radioButton2":
            type = SignalGeneratorType.Square;
            break;

          case "radioButton3":
            type = SignalGeneratorType.SawTooth;
            break;

          case "radioButton4":
            type = SignalGeneratorType.Pink;
            break;

          case "radioButton5":
            type = SignalGeneratorType.White;
            break;

          case "radioButton6":
            type = SignalGeneratorType.Sweep;
            break;

          case "radioButton7":
            type = SignalGeneratorType.Sin;
            break;

          default:
            type = def;
            break;
        }
      }
      else
      {
        type = def;
      }

      return type;
    }

@DamianSuess DamianSuess self-assigned this Apr 26, 2019
@DamianSuess DamianSuess added this to the 0.4 milestone Apr 26, 2019
@DamianSuess DamianSuess removed this from the 0.4 milestone Apr 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant