C#中的哔哔声听力测试 [英] Beep beep hearing test in C#

查看:107
本文介绍了C#中的哔哔声听力测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发.Net compact framework 3.5。我想为我的设备生成一个蜂鸣声,我指的是用VB.Net编写的代码。但问题是我不能在C#.Net中使用相同的波形格式。我指的是这个帖子产生一个哔哔声



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



我的尝试:



 使用 System.Collections.Generic; 
使用 System.Text;
使用 System.Media;

命名空间 RFID_IndicateRSSI
{
class beep
{
public void Beep( int 幅度, int 频率, int 持续时间)
{
double A =((幅度* 2 ^ 15 )/ 1000 ) - 1 ;
double DeltaFT = 2 * Math.PI *频率/ 44100 ;

int Samples = 441 *持续时间;
int 字节=样本* 4 ;
int [] hdr = {& H46464952, 36 +字节,& H45564157, & H20746D66, 16 ,& H20001, 44100 176400 ,& H100004,& H61746164,Bytes};
使用(MemoryStream MS = new MemoryStream( 44 +字节))
{

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

}
}





我试过这个代码,但'hdr'中的值得到错误。错误是'波形头已损坏'即使我尝试编写另一个数据流,它也不会接受。我需要帮助。



P.S.我不能使用Console.beep()。 Net Compact框架不支持它。

解决方案

这是VB.NET代码的更正翻译,在完整框架上适用于我:

 使用系统; 
使用 System.IO;
使用 System.Media;

静态 class beep
{
public static void 哔( int 幅度, int 频率, int 持续时间)
{
double A =((幅度* Math.Pow( 2 15 ))/ 1000 ) - 1 ;
double DeltaFT = 2 * Math.PI *频率/ 44100 ;

int Samples = 441 *持续时间/ 10 ;
int 字节=样本* 4 ;
int [] hdr = {0x46464952, 36 +字节,0x45564157,0x20746D66, 16 ,0x20001, 44100 176400 ,0x100004, 0x61746164,字节};

使用(MemoryStream MS = new MemoryStream( 44 + Bytes))
使用(BinaryWriter BW = new BinaryWriter(MS))
{
for int i = 0 ; i < hdr.Length; i ++)
{
BW.Write( HDR [I]);
}
for int i = 0 ; i < 样本; i ++)
{
short 样本;
sample = Convert.ToInt16(A * Math.Sin(DeltaFT * i));
BW.Write(样本);
BW.Write(样本);
}

BW.Flush();
MS.Seek( 0 ,SeekOrigin.Begin);

使用(SoundPlayer SP = new SoundPlayer(MS))
{
SP.Stream = null ;
SP.Stream = MS;
SP.PlaySync();
}
}
}
}



希望这也适用于CF. :)


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:

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.

解决方案

Here's a corrected translation of the VB.NET code, which works for me on full framework:

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. :)


这篇关于C#中的哔哔声听力测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆