/// <summary> Plays a note. The highest supported node is F# in 6th octave. </summary> /// <param name="Note"> The note to play. </param> /// <param name="Octave"> The octave in which to play the node. </param> /// <param name="Duration"> The duration to play (max supported is 65535 milli seconds). </param> /// <returns> If the method completed with success the returning value is /// <see cref="wclErrors.WCL_E_SUCCESS" />. If the method failed the returning value is /// one of the Bluetooth Framework error code. </returns> /// <seealso cref="wclWeDoPiezoNote"/> public Int32 PlayNote(wclWeDoPiezoNote Note, Byte Octave, UInt16 Duration) { if (Octave == 0 || Octave > 6 || (Octave == 6 && Note > wclWeDoPiezoNote.pnFis)) { return(wclErrors.WCL_E_INVALID_ARGUMENT); } if (!Attached) { return(wclBluetoothErrors.WCL_E_BLUETOOTH_DEVICE_NOT_INSTALLED); } // The basic formula for the frequencies of the notes of the equal tempered scale is given by // fn = f0 * (a)n // where // f0 - the frequency of one fixed note which must be defined. // A common choice is setting the A above middle C (A4) at f0 = 440 Hz. // n - the number of half steps away from the fixed note you are. If you are at a higher note, // n is positive. If you are on a lower note, n is negative. // fn - the frequency of the note n half steps away. // a - (2)1/12 = the twelfth root of 2 = the number which when multiplied by itself 12 times // equals 2 = 1.059463094359... Double BaseTone = 440.0; Int32 OctavesAboveMiddle = Octave - 4; float HalfStepsAwayFromBase = (float)Note - (float)wclWeDoPiezoNote.pnA + (OctavesAboveMiddle * 12); Double Frequency = BaseTone * Math.Pow(Math.Pow(2.0, 1.0 / 12), HalfStepsAwayFromBase); return(Hub.Io.PiezoPlayTone((UInt16)Math.Round(Frequency), Duration, ConnectionId)); }
private void BtPlay_Click(Object Sender, EventArgs e) { if (FPiezo == null) { MessageBox.Show("Device is not attached"); } else { wclWeDoPiezoNote[] Notes = new wclWeDoPiezoNote[] { wclWeDoPiezoNote.pnA, wclWeDoPiezoNote.pnAis, wclWeDoPiezoNote.pnB, wclWeDoPiezoNote.pnC, wclWeDoPiezoNote.pnCis, wclWeDoPiezoNote.pnD, wclWeDoPiezoNote.pnDis, wclWeDoPiezoNote.pnE, wclWeDoPiezoNote.pnF, wclWeDoPiezoNote.pnFis, wclWeDoPiezoNote.pnG, wclWeDoPiezoNote.pnGis }; wclWeDoPiezoNote Note = Notes[cbNote.SelectedIndex]; Int32 Res = FPiezo.PlayNote(Note, (Byte)(cbOctave.SelectedIndex + 1), Convert.ToUInt16(edDuration.Text)); if (Res != wclErrors.WCL_E_SUCCESS) { MessageBox.Show("Play failed: 0x" + Res.ToString("X8")); } } }
private void btPlay_Click(object sender, EventArgs e) { wclWeDoHub Hub = GetHub(); wclWeDoPiezo Piezo = FRobot.GetPiezoDevice(Hub); if (Piezo != null) { wclWeDoPiezoNote[] Notes = new wclWeDoPiezoNote[] { wclWeDoPiezoNote.pnA, wclWeDoPiezoNote.pnAis, wclWeDoPiezoNote.pnB, wclWeDoPiezoNote.pnC, wclWeDoPiezoNote.pnCis, wclWeDoPiezoNote.pnD, wclWeDoPiezoNote.pnDis, wclWeDoPiezoNote.pnE, wclWeDoPiezoNote.pnF, wclWeDoPiezoNote.pnFis, wclWeDoPiezoNote.pnG, wclWeDoPiezoNote.pnGis }; wclWeDoPiezoNote Note = Notes[cbNote.SelectedIndex]; Int32 Res = Piezo.PlayNote(Note, (Byte)(cbOctave.SelectedIndex + 1), Convert.ToUInt16(edDuration.Text)); if (Res != wclErrors.WCL_E_SUCCESS) { MessageBox.Show("Play failed: 0x" + Res.ToString("X8")); } } }