Skip to content

Commit

Permalink
Merge pull request #615 from WildernessLabs/MicroAudio
Browse files Browse the repository at this point in the history
Add MicroAudio library
  • Loading branch information
adrianstevens authored Apr 17, 2023
2 parents 97628b6 + 5a8a8f9 commit d57b974
Show file tree
Hide file tree
Showing 14 changed files with 1,217 additions and 1 deletion.
22 changes: 21 additions & 1 deletion Source/Meadow.Foundation.Core/Speakers/PiezoSpeaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ namespace Meadow.Foundation.Audio
/// </summary>
public class PiezoSpeaker : IToneGenerator
{
/// <summary>
/// The volume from 0-1
/// Defined by the PWM port duty cycle from 0 to 0.5
/// </summary>
public float Volume { get; protected set; } = 1.0f;

/// <summary>
/// Gets the port that is driving the Piezo Speaker
/// </summary>
Expand Down Expand Up @@ -72,7 +78,7 @@ public async Task PlayTone(Frequency frequency, TimeSpan duration)
isPlaying = true;

Port.Frequency = frequency;
Port.DutyCycle = 0.5f;
Port.DutyCycle = Volume / 2f;

if (duration.TotalMilliseconds > 0)
{
Expand All @@ -91,5 +97,19 @@ public void StopTone()
{
Port.DutyCycle = 0f;
}

/// <summary>
/// Set the playback volume
/// </summary>
/// <param name="volume">The volume from 0 (off) to 1 (max volume)</param>
public void SetVolume(float volume)
{
Volume = Math.Clamp(volume, 0, 1);

if(isPlaying)
{
Port.DutyCycle = Volume / 2f;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Authors>Wilderness Labs, Inc</Authors>
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Library</OutputType>
<AssemblyName>MicroAudio</AssemblyName>
<Company>Wilderness Labs, Inc</Company>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.Foundation/</PackageProjectUrl>
<PackageId>Meadow.Foundation.Audio.MicroAudio</PackageId>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation</RepositoryUrl>
<PackageTags>Meadow,Meadow.Foundation,Audio,Songs,Tone,Tones,Music,Sound,Effects</PackageTags>
<Version>0.1.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Lightweight single-voice sound effect and music player designed for embedded applications</Description>
</PropertyGroup>
<ItemGroup>
<None Include="..\..\..\icon.png" Pack="true" PackagePath="" />
<ProjectReference Include="..\..\..\Meadow.Foundation.Core\Meadow.Foundation.Core.csproj" />
</ItemGroup>
</Project>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
namespace Meadow.Foundation.Audio
{
/// <summary>
/// Represents a musical note
/// </summary>
public enum Pitch : int
{
/// <summary>
/// The C note
/// </summary>
C = 0,
/// <summary>
/// The C sharp note
/// </summary>
CSharp = 1,
/// <summary>
/// The enharmonic equivalent of C sharp
/// </summary>
DFlat = 1,
/// <summary>
/// The D note
/// </summary>
D = 2,
/// <summary>
/// The D sharp note
/// </summary>
DSharp = 3,
/// <summary>
/// The enharmonic equivalent of D sharp
/// </summary>
EFlat = 3,
/// <summary>
/// The E note
/// </summary>
E = 4,
/// <summary>
/// The F note
/// </summary>
F = 5,
/// <summary>
/// The F sharp note
/// </summary>
FSharp = 6,
/// <summary>
/// The enharmonic equivalent of F sharp
/// </summary>
GFlat = 6,
/// <summary>
/// The G note
/// </summary>
G = 7,
/// <summary>
/// The G sharp note
/// </summary>
GSharp = 8,
/// <summary>
/// The enharmonic equivalent of G sharp
/// </summary>
AFlat = 8,
/// <summary>
/// The A note
/// </summary>
A = 9,
/// <summary>
/// The A sharp note
/// </summary>
ASharp = 10,
/// <summary>
/// The enharmonic equivalent of A sharp
/// </summary>
BFlat = 10,
/// <summary>
/// The B note
/// </summary>
B = 11,
/// <summary>
/// Represents a rest note
/// </summary>
Rest
}

/// <summary>
/// Represents the duration of a musical note
/// </summary>
public enum NoteDuration
{
/// <summary>
/// A whole note
/// </summary>
Whole = 4000,
/// <summary>
/// A half note
/// </summary>
Half = 2000,
/// <summary>
/// A quarter note
/// </summary>
Quarter = 1000,
/// <summary>
/// An eighth note
/// </summary>
Eighth = 500,
/// <summary>
/// A sixteenth note
/// </summary>
Sixteenth = 250,
/// <summary>
/// A thirty-second note
/// </summary>
ThirtySecond = 125,
/// <summary>
/// A whole note triplet
/// </summary>
WholeTriplet = 6000,
/// <summary>
/// A dotted half note
/// </summary>
DottedHalf = 3000
}

/// <summary>
/// Represents the different sound effects that can be played by the <see cref="SystemSounds"/> class
/// </summary>
public enum SystemSoundEffect
{
/// <summary>
/// An alarm or emergency sound effect
/// </summary>
Alarm,
/// <summary>
/// An alert or notification sound effect
/// </summary>
Alert,
/// <summary>
/// A simple beep sound effect
/// </summary>
Beep,
/// <summary>
/// A buzzing or vibrating sound effect
/// </summary>
Buzz,
/// <summary>
/// A chime or bell sound effect
/// </summary>
Chime,
/// <summary>
/// A short click sound effect
/// </summary>
Click,
/// <summary>
/// A failure or error sound effect
/// </summary>
Failure,
/// <summary>
/// A fanfare or celebratory sound effect
/// </summary>
Fanfare,
/// <summary>
/// A notification sound effect
/// </summary>
Notification,
/// <summary>
/// A popping sound effect
/// </summary>
Pop,
/// <summary>
/// A power-up sound effect
/// </summary>
PowerUp,
/// <summary>
/// A power-down sound effect
/// </summary>
PowerDown,
/// <summary>
/// A success or positive feedback sound effect
/// </summary>
Success,
/// <summary>
/// A short tick or click sound effect
/// </summary>
Tick,
/// <summary>
/// A warning or caution sound effect
/// </summary>
Warning,
}

/// <summary>
/// Represents the different sound effects that can be played by the <see cref="GameSoundPlayer"/> class
/// </summary>
public enum GameSoundEffect
{
/// <summary>
/// A sound effect indicating the activation or use of an item or power-up
/// </summary>
Activation,
/// <summary>
/// A simple blip sound effect
/// </summary>
Blip,
/// <summary>
/// A sound effect indicating a boss battle or end challenge
/// </summary>
BossBattle,
/// <summary>
/// A button press or selection sound effect
/// </summary>
ButtonPress,
/// <summary>
/// A coin or currency collection sound effect
/// </summary>
Coin,
/// <summary>
/// A sound effect indicating the collection of an item or bonus
/// </summary>
Collectible,
/// <summary>
/// A countdown or timer sound effect
/// </summary>
Countdown,
/// <summary>
/// A sound effect indicating the death or defeat of an enemy
/// </summary>
EnemyDeath,
/// <summary>
/// An explosion or destruction sound effect
/// </summary>
Explosion,
/// <summary>
/// A sound effect indicating a footstep or movement
/// </summary>
Footstep,
/// <summary>
/// A game over or failure sound effect
/// </summary>
GameOver,
/// <summary>
/// A sound effect indicating a health pickup or healing
/// </summary>
Health,
/// <summary>
/// A hit or damage sound effect
/// </summary>
Hit,
/// <summary>
/// A jump or hop sound effect
/// </summary>
Jump,
/// <summary>
/// A laser or projectile firing sound effect
/// </summary>
Laser,
/// <summary>
/// A sound effect indicating the completion of a level or challenge
/// </summary>
LevelComplete,
/// <summary>
/// A menu navigation or selection sound effect
/// </summary>
MenuNavigate,
/// <summary>
/// A power-up or item pick-up sound effect
/// </summary>
PowerUp,
/// <summary>
/// A power-down or failure sound effect
/// </summary>
PowerDown,
/// <summary>
/// A sound effect indicating the discovery of a secret or hidden item
/// </summary>
SecretFound,
/// <summary>
/// A sound effect indicating a spash in water
/// </summary>
Splash,
/// <summary>
/// A sound effect indicating a teleport or warp
/// </summary>
Teleport,
/// <summary>
/// A victory or success sound effect
/// </summary>
Victory,
/// <summary>
/// A warning or alarm sound effect
/// </summary>
Warning,
/// <summary>
/// A sound effect indicating a weapon or tool switch
/// </summary>
WeaponSwitch,
}
}
Loading

0 comments on commit d57b974

Please sign in to comment.