Question How to create multiple audio recording decks

DjGrego

Member
Joined
Nov 21, 2021
Messages
23
Programming Experience
Beginner
Hello.

I am planning a app with multiple audio recording decks. The amount of simultaneous recording decks would be chosen by the user. Is this the best direction to go with creating the decks or is there a simpler way?

C#:
           if (numberOfDecks >= 1)
           {
               WaveInEvent waveSource1 = new WaveInEvent();
               waveSource1.DeviceNumber = 0;
               waveSource1.WaveFormat = new WaveFormat(44100, 2);
               string tempFile1 = (@"C:\TEMP\test1.wav");
               waveFile = new WaveFileWriter(tempFile1, waveSource1.WaveFormat);
               waveSource1.StartRecording();
           }

           if (numberOfDecks >= 2)
           {
               WaveInEvent waveSource2 = new WaveInEvent();
               waveSource2.DeviceNumber = 0;
               waveSource2.WaveFormat = new WaveFormat(44100, 2);
               string tempFile2 = (@"C:\TEMP\test2.wav");
               waveFile = new WaveFileWriter(tempFile2, waveSource2.WaveFormat);
               waveSource2.StartRecording();
           }

           if (numberOfDecks >= 2)
           {
               WaveInEvent waveSource3 = new WaveInEvent();
               //
           }

Thank you for any suggestions.

Greg
 
Solution
You don't need the variable names to be different for each iteration of the loop. Re-read whatever tutorial you were using to learn C# and pay attention to the topic of "scope". You didn't need them to be different variable names for your lines 3, 6, 13, and 16 in your original post. They were local variables only within the scope of the body of the original if statement bodies.

As for your lines 8, 18, that is what arrays or lists are for. I recommend using lists.
If you see yourself duplicating code, that is usually a code smell that the duplicate code should be in a helper function.
If you see yourself duplicating code that only varies by some incremental value, then that is a code smell that the duplicate code should be encapsulated by a loop. The helper function may help in creating self-documenting code.

So that means you would end up with something that looks like this pseudo code:
C#:
for(int i = 0; i < numberOfDecks; i++)
    RecordDeck(i + 1)

:

void RecordDeck(int deckNumber)
{
     fileName = $"test{deckNumber}.wav"
     record into fileName
}
 
Last edited:
Awesome. That makes sense. But it doesn't seem to be possible to do something like:

C#:
void RecordDeck(int deckNumber)
{
    WaveInEvent waveSource{deckNumber} = new WaveInEvent();
}
 
You don't need the variable names to be different for each iteration of the loop. Re-read whatever tutorial you were using to learn C# and pay attention to the topic of "scope". You didn't need them to be different variable names for your lines 3, 6, 13, and 16 in your original post. They were local variables only within the scope of the body of the original if statement bodies.

As for your lines 8, 18, that is what arrays or lists are for. I recommend using lists.
 
Solution
Thank you so much!! Scope was very usefull. Sometimes it only takes one word to Google, then you can connect all of your tutorials to a project.
 
Well, I searched about it on Google and I found lots of ways to do this.
Can you try this I hope you can get what you looking for.

C#:
using System;
using System.Collections.Generic;
using NAudio.Wave;

namespace AudioRecordingApp
{
    class Program
    {
        static List<WaveInEvent> waveSources = new List<WaveInEvent>();
        static List<WaveFileWriter> waveFiles = new List<WaveFileWriter>();

        static void Main(string[] args)
        {
            Console.Write("Enter the number of recording decks: ");
            if (int.TryParse(Console.ReadLine(), out int numberOfDecks) && numberOfDecks > 0)
            {
                InitializeRecordingDecks(numberOfDecks);

                Console.WriteLine("Recording. Press Enter to stop.");
                Console.ReadLine();

                StopRecording();

                Console.WriteLine("Recording stopped.");
            }
            else
            {
                Console.WriteLine("Invalid input. Please enter a positive number.");
            }
        }

        static void InitializeRecordingDecks(int numberOfDecks)
        {
            for (int i = 0; i < numberOfDecks; i++)
            {
                WaveInEvent waveSource = new WaveInEvent();
                waveSource.DeviceNumber = i; // Adjust this based on your audio devices
                waveSource.WaveFormat = new WaveFormat(44100, 2);
                string tempFile = $@"C:\TEMP\test{i}.wav";
                waveFiles.Add(new WaveFileWriter(tempFile, waveSource.WaveFormat));
                waveSources.Add(waveSource);
                waveSource.DataAvailable += (s, e) =>
                {
                    waveFiles[I].Write(e.Buffer, 0, e.BytesRecorded);
                };
                waveSource.StartRecording();
            }
        }

        static void StopRecording()
        {
            foreach (var waveSource in waveSources)
            {
                waveSource.StopRecording();
                waveSource.Dispose();
            }

            foreach (var waveFile in waveFiles)
            {
                waveFile.Close();
                waveFile.Dispose();
            }

            waveSources.Clear();
            waveFiles.Clear();
        }
    }
}


Thanks
 
Last edited by a moderator:
Back
Top Bottom