Skip to content

Getting Started with SoundFlow

Getting Started with SoundFlow

This guide will help you get up and running with SoundFlow quickly. You’ll learn how to install the library, set up your development environment, and write your first SoundFlow application.

Prerequisites

Before you begin, make sure you have the following installed:

  • .NET SDK 8.0 or later: SoundFlow is built on .NET 8.0, so you’ll need the corresponding SDK to build and run SoundFlow projects.
  • An IDE or code editor: You can use any IDE or code editor that supports .NET development. Popular choices include:
  • Basic knowledge of C# and .NET: Familiarity with C# programming and .NET concepts will be helpful.

Supported Operating Systems:

  • Windows
  • macOS
  • Linux
  • Android
  • IOS

Installation

You can install SoundFlow in several ways:

This is the easiest and recommended way to add SoundFlow to your .NET projects.

  1. Open the NuGet Package Manager Console: In Visual Studio, go to Tools > NuGet Package Manager > Package Manager Console.
  2. Run the installation command:
Install-Package SoundFlow

This command will download and install the latest version of SoundFlow and its dependencies into your current project.

Basic Usage Example

Let’s create a simple console application that plays an audio file using SoundFlow.

  1. Create a new console application:

    • In Visual Studio, go to File > New > Project. Select Console App and give it a name (e.g., SoundFlowExample).

    • Or, use the .NET CLI:

      dotnet new console -o SoundFlowExample
      cd SoundFlowExample
  2. Install the SoundFlow NuGet package: (If you haven’t already)

    Install-Package SoundFlow

    or

    dotnet add package SoundFlow
  3. Replace the contents of Program.cs with the following code:

    using SoundFlow.Abstracts;
    using SoundFlow.Backends.MiniAudio;
    using SoundFlow.Components;
    using SoundFlow.Enums;
    using SoundFlow.Providers;
    namespace SoundFlowExample;
    internal static class Program
    {
    private static void Main(string[] args)
    {
    // Initialize the audio engine with the MiniAudio backend, 44.1kHz sample rate, and playback capability.
    using var audioEngine = new MiniAudioEngine(44100, Capability.Playback);
    // Create a SoundPlayer and load an audio file.
    // Make sure you replace "path/to/your/audiofile.wav" with the actual path to your audio file.
    var player = new SoundPlayer(new StreamDataProvider(File.OpenRead("path/to/your/audiofile.wav")));
    // Add the player to the master mixer. This connects the player's output to the audio engine's output.
    Mixer.Master.AddComponent(player);
    // Start playback.
    player.Play();
    // Keep the console application running until playback finishes or the user presses a key.
    Console.WriteLine("Playing audio... Press any key to stop.");
    Console.ReadKey();
    // Stop playback.
    player.Stop();
    // Remove the player from the mixer.
    Mixer.Master.RemoveComponent(player);
    }
    }
  4. Replace "path/to/your/audiofile.wav" with the actual path to an audio file on your computer.

  5. Build and run the application:

    • In Visual Studio, press F5 or go to Debug > Start Debugging.

    • Or, use the .NET CLI:

      dotnet run

You should now hear the audio file playing through your default audio output device.

Code Explanation:

  • using SoundFlow...: These lines import the necessary namespaces from the SoundFlow library.
  • using var audioEngine = new MiniAudioEngine(...): This creates an instance of the MiniAudioEngine, which is the default audio backend for SoundFlow. It’s initialized with a sample rate of 44100 Hz and playback capability. The using statement ensures that the engine is properly disposed of when it’s no longer needed.
  • var player = new SoundPlayer(...): This creates a SoundPlayer instance and loads the specified audio file using a StreamDataProvider.
  • Mixer.Master.AddComponent(player): This adds the SoundPlayer to the Master mixer. The Master mixer is the root of the audio graph and represents the final output of the audio engine.
  • player.Play(): This starts the playback of the audio file.
  • Console.WriteLine(...) and Console.ReadKey(): These lines keep the console application running and wait for the user to press a key.
  • player.Stop(): This stops the playback.
  • Mixer.Master.RemoveComponent(player): This removes the SoundPlayer from the Master mixer, disconnecting it from the audio graph.

Troubleshooting

  • “Could not load file or assembly ‘SoundFlow’…”: Make sure you have installed the SoundFlow NuGet package or added a reference to the SoundFlow library if you built it from source.
  • No audio output:
    • Verify that your audio device is properly configured and selected as the default output device in your operating system’s sound settings.
    • Check the volume levels in your operating system and in the SoundFlow application.
    • Ensure that the audio file you are trying to play is in a supported format and is not corrupted.
  • Errors during installation: If you encounter errors while installing the NuGet package, try clearing your NuGet cache (dotnet nuget locals all --clear) and try again.

If you encounter any other issues, please open an issue on the GitHub repository.

Next Steps

Now that you have successfully set up SoundFlow and played your first audio file, you can explore the more advanced features and concepts covered in this Wiki:

  • Core Concepts: Learn more about the fundamental building blocks of SoundFlow.
  • API Reference: Dive into the detailed documentation for each class and interface.
  • Tutorials and Examples: Get hands-on experience with various SoundFlow features through practical examples.

Happy coding!