Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on .Net compact framework 3.5. I want to generate a beep for my device and I am referring a code which is written in VB.Net. But the problem is I can not use the same wave format in C#.Net. I am referring this thread to generate a beep sound

https://social.msdn.microsoft.com/Forums/vstudio/en-US/c2b953b6-3c85-4eda-a478-080bae781319/beep-beep?forum=vbgeneral[^]

What I have tried:

C#
using System.Collections.Generic;
using System.Text;
using System.Media;

namespace RFID_IndicateRSSI
{
    class beep
    {
        public void Beep(int Amplitude, int Frequency, int Duration)
        {
            double A = ((Amplitude * 2 ^ 15) / 1000) - 1;
            double DeltaFT = 2 * Math.PI * Frequency / 44100;
            
            int Samples = 441 * Duration;
            int Bytes = Samples * 4;
            int[] hdr = {&H46464952, 36 + Bytes, &H45564157,&H20746D66, 16, &H20001, 44100,176400, &H100004, &H61746164, Bytes};
            using (MemoryStream MS = new MemoryStream(44 + Bytes))
            {

                using (BinaryWriter BW = new BinaryWriter(MS))
                {
                    for (int i = 0; i < hdr.Length-1; i++)
                    {
                        BW.Write(hdr[i]);
                    }
                    for (int i = 0; i < Samples -1; i++)
                    {
                        short sample;
                        sample = Convert.ToInt16(A * Math.Sin(DeltaFT * i));
                        BW.Write(sample);
                        BW.Write(sample);
                    }
                    BW.Flush();
                    MS.Seek(0, SeekOrigin.Begin);
                    using (SoundPlayer SP = new SoundPlayer(MS))
                    {
                        SP.Stream = null;
                        SP.Stream = MS;
                        SP.PlaySync();                        
                    }
                }
            }
        }

    }
}


I tried with this code but value in 'hdr' getting error. The error is 'The wave header is corrupt' Even if I try to write some another stream of data, its not accepting as well. I need help with this one.

P.S. I can not use Console.beep(). It's not supported by Net Compact framework.
Posted
Updated 1-Mar-18 8:57am
v5
Comments
Richard Deeming 1-Mar-18 13:58pm    
If you want someone to help you fix an error, then you need to tell us what the error is.

Click the green "Improve question" link, and add the full details of the error you're getting.
webmail123 1-Mar-18 14:24pm    
@Richard Sorry for the confusion. I updated the question.
Richard Deeming 1-Mar-18 14:32pm    
I notice you've changed the calculation for Samples - are you sure the value is still valid?

Based on the MSDN thread, it should be:
int Samples = 441 * Duration / 10;
webmail123 1-Mar-18 14:41pm    
@Richard Thanks for the reply. I think that is not a problem at all. The problem is in hdr[]. That particular format of wave file is not recognizable. So that's why I am getting error.
Richard Deeming 1-Mar-18 14:46pm    
I assume you've replaced the VB hex prefix (&H) with the C# one (0x)? Otherwise, you'd get a compiler error.

1 solution

Here's a corrected translation of the VB.NET code, which works for me on full framework:
C#
using System;
using System.IO;
using System.Media;

static class beep
{
    public static void Beep(int Amplitude, int Frequency, int Duration)
    {
        double A = ((Amplitude * Math.Pow(2, 15)) / 1000) - 1;
        double DeltaFT = 2 * Math.PI * Frequency / 44100;
        
        int Samples = 441 * Duration / 10;
        int Bytes = Samples * 4;
        int[] hdr = { 0x46464952, 36 + Bytes, 0x45564157, 0x20746D66, 16, 0x20001, 44100, 176400, 0x100004, 0x61746164, Bytes };
        
        using (MemoryStream MS = new MemoryStream(44 + Bytes))
        using (BinaryWriter BW = new BinaryWriter(MS))
        {
            for (int i = 0; i < hdr.Length; i++)
            {
                BW.Write(hdr[i]);
            }
            for (int i = 0; i < Samples; i++)
            {
                short sample;
                sample = Convert.ToInt16(A * Math.Sin(DeltaFT * i));
                BW.Write(sample);
                BW.Write(sample);
            }
            
            BW.Flush();
            MS.Seek(0, SeekOrigin.Begin);
            
            using (SoundPlayer SP = new SoundPlayer(MS))
            {
                SP.Stream = null;
                SP.Stream = MS;
                SP.PlaySync();
            }
        }
    }
}

Hopefully that should also work on CF. :)
 
Share this answer
 
Comments
webmail123 2-Mar-18 9:36am    
Thank you very much. It worked finally!!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900