调用API让声卡发出声音

1.定义一个枚举,乐器类型
    #region 乐器定义
    public enum MusicNo:uint
    {   /// <summary>
     /// //大钢琴
     /// </summary>
        AcousticGrandPiano = 0,
        /// <summary>
        ///明亮的钢琴
        /// </summary>
        BrightAcousticPiano = 1,
        /// <summary>
        ///电子琴
        /// </summary>
        ElectricGrandPiano = 2,
        /// <summary>
        ///酒吧钢琴
        /// </summary>
        HonkyTonkPiano = 3,
        /// <summary>
        ///柔和钢琴
        /// </summary>
        RhodesPiano = 4,
        /// <summary>
        ///合唱效果钢琴
        /// </summary>
        ChorusedPiano = 5,
        /// <summary>
        ///羽管键琴
        /// </summary>
        Harpsichord = 6,
        /// <summary>
        ///克拉维科特琴
        /// </summary>
        Clavichord = 7,
        /// <summary>
        ///钢片琴
        /// </summary>
        Celesta = 8,
        /// <summary>
        ///钟琴
        /// </summary>
        Glockenspiel = 9,
        /// <summary>
        ///八音盒
        /// </summary>
        Musicbox = 10,
        /// <summary>
        ///颤音琴
        /// </summary>
        Vibraphone = 11,
        /// <summary>
        ///马林巴
        /// </summary>
        Marimba = 12,
        /// <summary>
        ///木琴
        /// </summary>
        Xylophone = 13,
        /// <summary>
        ///管钟
        /// </summary>
        TubularBells = 14,
        /// <summary>
        ///大扬琴
        /// </summary>
        Dulcimer = 15,
        /// <summary>
        /// 击杆风琴
        /// </summary>
        HammondOrgan = 16,
        /// <summary>
        /// 打击式风琴
        /// </summary>
        PercussiveOrgan = 17,
        /// <summary>
        /// 摇滚风琴
        /// </summary>
        RockOrgan = 18,
        /// <summary>
        /// 教堂风琴
        /// </summary>
        ChurchOrgan = 19,
        /// <summary>
        /// 簧管风琴
        /// </summary>
        ReedOrgan = 20,
        /// <summary>
        /// 手风琴
        /// </summary>
        Accordian = 21,
        /// <summary>
        /// 口琴
        /// </summary>
        Harmonica = 22,
        /// <summary>
        ///探戈手风琴
        /// </summary>
        TangoAccordian = 23,
        /// <summary>
        /// 尼龙弦吉他
        /// </summary>
        AcousTicGuitarNylon = 24,
        /// <summary>
        /// 钢弦吉他
        /// </summary>
        AcousTicGuitarSteel = 25,
        /// <summary>
        /// 爵士电吉他
        /// </summary>
        ElectricGuitarJazz = 26,
        /// <summary>
        /// 清音电吉他
        /// </summary>
        ElectricGuitarClean = 27,
        /// <summary>
        /// 闷音电吉他
        /// </summary>
        ElectricGuitarMuted = 28,
        /// <summary>
        /// 加驱动效果的电吉他
        /// </summary>
        OverdrivenGuitar = 29,
        /// <summary>
        /// 加失真效果的电吉他
        /// </summary>
        DistortionGuitar = 30,
        /// <summary>
        /// 吉他和音
        /// </summary>
        GuitarHarmonics = 31,
        /// <summary>
        ///大贝司(声学贝司)
        /// </summary>
        AcousticBass = 32,
        /// <summary>
        /// 电贝司(指弹)
        /// </summary>
        ElectricBassfinger = 33,
        /// <summary>
        /// 电贝司(拨片)
        /// </summary>
        ElectricBassPick = 34,
        /// <summary>
        /// 无品贝司
        /// </summary>
        FretlessBass = 35,
        /// <summary>
        ///  掌击Bass 1
        /// </summary>
        SlapBass1 = 36,
        /// <summary>
        /// 掌击Bass 2
        /// </summary>
        SlapBass2 = 37,
        /// <summary>
        /// 电子合成Bass 1
        /// </summary>
        SynthBass1 = 38,
        /// <summary>
        /// 电子合成Bass 2
        /// </summary>
        SynthBass2 = 39,
        /// <summary>
        ///  小提琴
        /// </summary>
        Violin = 40,
        /// <summary>
        /// 中提琴
        /// </summary>
        Viola = 41,
        /// <summary>
        /// 大提琴
        /// </summary>
        Cello = 42,
        /// <summary>
        /// 低音大提琴
        /// </summary>
        Contrabass = 43,
        /// <summary>
        ///弦乐群颤音音色
        /// </summary>
        TremoloStrings = 44,
        /// <summary>
        ///弦乐群拨弦音色
        /// </summary>
        PizzicatoStrings = 45,
        /// <summary>
        ///竖琴
        /// </summary>
        OrchestralHarp = 46,
        /// <summary>
        ///  定音鼓
        /// </summary>
        Timpani = 47,
        /// <summary>
        ///弦乐合奏音色1
        /// </summary>   
        StringEnsemble1 = 48,
        /// <summary>
        ///弦乐合奏音色2
        /// </summary>
        StringEnsemble2 = 49,
        /// <summary>
        ///合成弦乐合奏音色1
        /// </summary>
        SynthStrings1 = 50,
        /// <summary>
        ///合成弦乐合奏音色2
        /// </summary>
        SynthStrings2 = 51,
        /// <summary>
        ///人声合唱“啊”
        /// </summary>
        ChoirAahs = 52,
        /// <summary>
        ///人声“嘟”
        /// </summary>
        VoiceOohs = 53,
        /// <summary>
        ///合成人声
        /// </summary>
        SynthVoice = 54,
        /// <summary>
        ///管弦敲击齐奏
        /// </summary>
        OrchestraHit = 55,
        /// <summary>
        ///小号
        /// </summary>
        Trumpet = 56,
        /// <summary>
        ///长号
        /// </summary>
        Trombone = 57,
        /// <summary>
        ///大号
        /// </summary>
        Tuba = 58,
        /// <summary>
        ///加弱音器小号
        /// </summary>
        MutedTrumpet = 59,
        /// <summary>
        ///法国号(圆号)
        /// </summary>
        FrenchHorn = 60,
        /// <summary>
        ///铜管组(铜管乐器合奏音色)
        /// </summary>
        BrassSection = 61,
        /// <summary>
        ///合成铜管音色1
        /// </summary>
        SynthBrass1 = 62,
        /// <summary>
        ///合成铜管音色2
        /// </summary>
        SynthBrass2 = 63,
        /// <summary>
        ///高音萨克斯风
        /// </summary>
        SopranoSax = 64,
        /// <summary>
        ///次中音萨克斯风
        /// </summary>
        AltoSax = 65,
        /// <summary>
        ///中音萨克斯风
        /// </summary>
        TenorSax = 66,
        /// <summary>
        ///低音萨克斯风
        /// </summary>
        BaritoneSax = 67,
        /// <summary>
        ///双簧管
        /// </summary>
        Oboe = 68,
        /// <summary>
        ///英国管
        /// </summary>
        EnglishHorn = 69,
        /// <summary>
        ///巴松(大管)
        /// </summary>
        Bassoon = 70,
        /// <summary>
        ///单簧管(黑管)
        /// </summary>
        Clarinet = 71,
        /// <summary>
        ///短笛
        /// </summary>
        Piccolo = 72,
        /// <summary>
        ///长笛
        /// </summary>
        Flute = 73,
        /// <summary>
        ///竖笛
        /// </summary>
        Recorder = 74,
        /// <summary>
        ///排箫
        /// </summary>
        PanFlute = 75,
        /// <summary>
        ///[中文名称暂缺]
        /// </summary>
        BottleBlow = 76,
        /// <summary>
        ///日本尺八
        /// </summary>
        Shakuhachi = 77,
        /// <summary>
        ///口哨声
        /// </summary>
        Whistle = 78,
        /// <summary>
        ///奥卡雷那
        /// </summary>
        Ocarina = 79,
        /// <summary>
        ///合成主音1(方波)
        /// </summary>           
        Lead1Square = 80,
        /// <summary>
        ///合成主音2(锯齿波)
        /// </summary>
        Lead2Sawtooth = 81,
        /// <summary>
        ///合成主音3
        /// </summary>
        Lead3CaliopeLead = 82,
        /// <summary>
        ///合成主音4
        /// </summary>
        Lead4ChiffLead = 83,
        /// <summary>
        ///合成主音5
        /// </summary>
        Lead5Charang = 84,
        /// <summary>
        ///合成主音6(人声)
        /// </summary>
        Lead6Voice = 85,
        /// <summary>
        ///合成主音7(平行五度)
        /// </summary>
        Lead7Fifths = 86,
        /// <summary>
        ///合成主音8(贝司加主音)
        /// </summary>
        Lead8BassLead = 87,
        /// <summary>
        ///合成音色1(新世纪)
        /// </summary>
        Pad1NewAge = 88,
        /// <summary>
        ///合成音色2 (温暖)
        /// </summary>
        Pad2Warm = 89,
        /// <summary>
        ///合成音色3
        /// </summary>
        Pad3Polysynth = 90,
        /// <summary>
        ///合成音色4 (合唱)
        /// </summary>
        Pad4Choir = 91,
        /// <summary>
        ///合成音色5
        /// </summary>
        Pad5Bowed = 92,
        /// <summary>
        ///合成音色6 (金属声)
        /// </summary>
        Pad6Metallic = 93,
        /// <summary>
        ///合成音色7 (光环)
        /// </summary>
        Pad7Halo = 94,
        /// <summary>
        ///合成音色8
        /// </summary>
        Pad8Wweep = 5,
        /// <summary>
        ///合成效果 1 雨声
        /// </summary>

FX1Rain = 96,
        /// <summary>
        ///合成效果 2 音轨
        /// </summary>
        FX2Soundtrack = 97,
        /// <summary>
        ///合成效果 3 水晶
        /// </summary>
        FX3Crystal = 98,
        /// <summary>
        ///合成效果 4 大气
        /// </summary>
        FX4Atmosphere = 99,
        /// <summary>
        ///合成效果 5 明亮
        /// </summary>
        FX5Brightness = 100,
        /// <summary>
        ///合成效果 6 鬼怪
        /// </summary>
        FX6Goblins = 101,
        /// <summary>
        ///合成效果 7 回声
        /// </summary>
        FX7Echoes = 102,
        /// <summary>
        ///合成效果 8 科幻
        /// </summary>
        FX8Scifi = 103,
        /// <summary>
        ///西塔尔(印度)
        /// </summary>
        Sitar = 104,
        /// <summary>
        ///班卓琴(美洲)
        /// </summary>
        Banjo = 105,
        /// <summary>
        ///三昧线(日本)
        /// </summary>
        Shamisen = 106,
        /// <summary>
        ///十三弦筝(日本)
        /// </summary>
        Koto = 107,
        /// <summary>
        ///卡林巴
        /// </summary>
        Kalimba = 108,
        /// <summary>
        ///风笛
        /// </summary>
        Bagpipe = 109,
        /// <summary>
        ///民族提琴
        /// </summary>
        Fiddle = 110,
        /// <summary>
        ///山奈
        /// </summary>
        Shanai = 111,
        /// <summary>
        /// 叮当铃
        /// </summary>
        TinkleBell = 112,
        /// <summary>
        ///[中文名称暂缺]
        /// </summary>
        Agogo = 113,
        /// <summary>
        ///钢鼓
        /// </summary>
        SteelDrums = 114,
        /// <summary>
        ///木鱼
        /// </summary>
        Woodblock = 115,
        /// <summary>
        ///太鼓
        /// </summary>
        TaikoDrum = 116,
        /// <summary>
        ///通通鼓
        /// </summary>
        MelodicTom = 117,
        /// <summary>
        ///合成鼓
        /// </summary>
        SynthDrum = 118,
        /// <summary>
        ///铜钹
        /// </summary>
        ReverseCymbal = 119,
        /// <summary>
        ///吉他换把杂音
        /// </summary>
        GuitarFretNoise = 120,
        /// <summary>
        ///呼吸声
        /// </summary>
        BreathNoise = 121,
        /// <summary>
        ///海浪声
        /// </summary>
        Seashore = 122,
        /// <summary>
        ///鸟鸣
        /// </summary>
        BirdTweet = 123,
        /// <summary>
        ///电话铃
        /// </summary>
        TelephoneRing = 124,
        /// <summary>
        ///直升机
        /// </summary>
        Helicopter = 125,
        /// <summary>
        ///鼓掌
        /// </summary>
        Applause = 126,
        /// <summary>
        /// Gunshot
        /// </summary>
        Gunshot = 127,
    }

2.callback
    public enum OpenFlag:uint
    {
        CALLBACK_NULL = 0x00000000,//{当 dwCallback 是 nil 时指定}
        CALLBACK_WINDOW = 0x00010000,//{当 dwCallback 是窗口句柄时指定}
        CALLBACK_FUNCTION = 0x0003000,// {当 dwCallback 是函数指针时指定}
    }

3.打开设备返回值
    public enum OpenMsg:uint
    {
        MMSYSERR_BADDEVICEID = 2,//{设备ID超界}
        MMSYSERR_ALLOCATED = 4, //{指定的资源已被分配}
        MMSYSERR_NOMEM = 7, //{不能分配或锁定内存}
        MIDIERR_NOMAP = 66,//{当前没有 MIDI 映射, 只有打开映射程序才可能发生}
        MIDIERR_NODEVICE = 68,//{MIDI 映射中的端口不存在, 只有打开映射程序才可能发生}
    }
    #endregion

4.midi帮助类
    public class MIDIHelper
    {
        #region API
        [DllImport("winmm.dll")]
        static extern uint midiOutOpen(out IntPtr lphMidiOut, uint uDeviceID, IntPtr dwCallback, IntPtr dwInstance, uint dwFlags);//打开数字乐器接口输出
        [DllImport("winmm.dll")]
        static extern uint midiOutClose(IntPtr hMidiOut);//关闭数字乐器接口输出
        [DllImport("winmm.dll")]
        static extern uint midiOutShortMsg(IntPtr hMidiOut, uint dwMsg);//发送数字乐器接口消息

#endregion

5.定义变量
        #region 自定义变量
        bool isOpen = false;
        IntPtr DeviceHandle = IntPtr.Zero;
        public bool Opend => isOpen;

#endregion
        #region 方法

6.打开设备。
        public void Open()
        {
            IntPtr device;//此处后续要用
            uint hand = midiOutOpen(out device, 0, IntPtr.Zero, IntPtr.Zero, 0);
            isOpen = hand == 0;
            DeviceHandle = device;
        }
        /// <summary>
        /// 播放声音返回0表示成功!621表示设备未打开
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
     
        public uint Play(uint msg,int time)//播放,尝试了很多写法,发现msg * 256 + 0x00403B90;基本正常,百度百科的方法声音沙哑(msg*256+144+volume*655536+channel)
        {
            if (isOpen && DeviceHandle != IntPtr.Zero)
            {
                uint data = msg * 256 + 0x00403B90;
                Console.WriteLine(data);
                uint ms = midiOutShortMsg(DeviceHandle,data);
                System.Threading.Thread.Sleep(time);
                return msg;
            }
            else
                return 621;
        }
        public uint ChangeInstruments(MusicNo musicNo)//改变乐器
        {
            return midiOutShortMsg(DeviceHandle, Convert.ToUInt32(192 + (uint)musicNo * 256));
        }
        public void Close()//关闭设备
        {
            uint msg = midiOutClose(DeviceHandle);
        }
        #endregion
    }

接下来看看调用例子:

其中play方法中的数据12,50,...............摘自网上

MIDIHelper helper = new Gaofajin.Shell.MIDIHelper();
            helper.Open();
            if (helper.Opend)
            {
               helper.ChangeInstruments(Gaofajin.Shell.MusicNo.Piccolo);

helper.Play(12, 250);

helper.Play(12, 250);

helper.Play(15, 250);

helper.Play(17, 250);

helper.Play(19, 1000);

helper.Play(24, 700);

helper.Play(19, 250);

helper.Play(22, 200);

helper.Play(00, 250);

helper.Play(22, 900);

helper.Play(19, 400);

helper.Play(17, 400);

helper.Play(19, 400);

helper.Play(22, 400);

helper.Play(24, 250);

helper.Play(22, 250);

helper.Play(19, 1500);

helper.Play(00, 300);

helper.Play(19, 1000);

helper.Play(24, 700);

helper.Play(19, 250);

helper.Play(22, 200);

helper.Play(00, 250);

helper.Play(22, 900);

helper.Play(19, 400);

helper.Play(17, 400);

helper.Play(19, 400);

helper.Play(22, 400);

helper.Play(24, 250);

helper.Play(22, 250);

helper.Play(19, 1000);

helper.Play(00, 300);

helper.Play(19, 400);

helper.Play(17, 250);

helper.Play(15, 600);

helper.Play(00, 100);

helper.Play(15, 250);

helper.Play(17, 400);

helper.Play(19, 400);

helper.Play(17, 100);

helper.Play(00, 250);

helper.Play(17, 500);

helper.Play(00, 100);

helper.Play(17, 400);

helper.Play(15, 250);

helper.Play(12, 250);

helper.Play(00, 100);

helper.Play(12, 250);

helper.Play(15, 250);

helper.Play(17, 400);

helper.Play(19, 400);

helper.Play(22, 600);

helper.Play(24, 250);

helper.Play(22, 400);

helper.Play(19, 250);

helper.Play(17, 250);

helper.Play(15, 600);

helper.Play(17, 600);

helper.Play(19, 400);

helper.Play(17, 150);

helper.Play(00, 250);

helper.Play(17, 600);

helper.Play(15, 600);

helper.Play(12, 300);

helper.Play(00, 100);

helper.Play(12, 250);

helper.Play(15, 250);

helper.Play(17, 250);

helper.Play(19, 400);

helper.Play(22, 250);

helper.Play(24, 700);

helper.Play(00, 100);

helper.Play(24, 500);

}
            helper.Close();

原文地址:https://www.cnblogs.com/gfjin/p/8179078.html

时间: 2024-10-16 08:59:49

调用API让声卡发出声音的相关文章

第8章 让开发板发出声音,蜂鸣器驱动 心得体会

第8章让开发板发出声音,蜂鸣器驱动 心得体会 通过学习Android深度探索(卷1)HAL与驱动开发的第8章 让开发板发出声音,蜂鸣器驱动,我加深对驱动的认识.以下主要是我对本节实验和参考在Linux驱动开发中使用PWM控制蜂鸣器中的一些见解. 先编写一个简单的蜂鸣器驱动程序,根据开发板上的硬件IO接口和Android驱动规范,在kernel中的driver目录中新建一个Buzzer文件,新建一个buzzer.c的源文 在目录下创建一个Makefile文件 先运行make clean命令清楚到编

go语言让windows发出声音,或者播放音乐

go语言让windows发出声音,或者播放音乐的例子:会发出alert警告的声音 ( 这是我应群员的求助写的, 如果你需要了解其中的调用原理或过程 或更多go语言调用win32api的资料,加群: 218160862 , 答案我告诉你) package main func main(){ winSound() } // golang 让windows发出警告的声音  todo 需要完善播放mp3之类 func winSound(  )  { funInDllFile, err := syscal

VB调用API函数

API函数快速入门--怎样在VB中声明和使用API函数-- 一.在VB中声明API函数有两种方法:如果我们只在某个窗体中使用API函数,我们可以在窗体代码的 General部分声明它: 声明的语法是: Private Declare Function ... Private Declare Sub..... 这里必须采用Private声明,因为这个API函数只能被一个窗体内的程序所调用. 如果我们的程序有多个窗体构成,而且我们需要在多个窗体中使用同一个API函数,就需要在模块中 声明了. 先添加

第8章 让开发板发出声音,蜂鸣器驱动

第8章让开发板发出声音,蜂鸣器驱动 心得体会 在目录下创建一个Makefile文件 先运行make clean命令清楚到编译生成的一些工程文件,执行make命令,重新编译驱动程序.将编译生成的buzzer.ko内核镜像文件拷贝出来,adb push buzzer.ko /fpga/moudles 然后插入内核镜像文件到系统中,insmod buzzer.ko,并且装载设备驱动程序,mknod  /dev/buzzer c 248 0 现在就可以在库层直接编写C/C++文件来调用这个驱动程序,这个

第八章 让开发板发出声音:蜂鸣器驱动

这一章的主要内容是让开发板发出声音——蜂鸣器驱动,在Linux驱动开发中重点也学习了这一部分内容,同时最后我们小组的考核内容是通过中断控制蜂鸣器发七个音符的音. 这一部分中,要中断控制蜂鸣器,在中断和蜂鸣器代码的基础上进行增加.中断与蜂鸣器的代码不变,在中断的测试代码中,首先要定义七个音符,之后将开发板上的七个键与七个音符相连接,再将七个音符的转换与蜂鸣器相连接,来达到所需的效果. 核心代码: int main() { int fd,fd1; int div; int keyval; fd1 =

OAuth2.0学习(5-4)新浪开放平台-微博API-使用OAuth2.0调用API

使用OAuth2.0调用API 使用OAuth2.0调用API接口有两种方式: 1. 直接使用参数,传递参数名为 access_token URL 1 https://api.weibo.com/2/statuses/public_timeline.json?access_token=abcd 2.在header里传递,形式为在header里添加 Authorization:OAuth2空格abcd,这里的abcd假定为Access Token的值,其它接口参数正常传递即可. 注:所有的微博开放

调用API接口下载腾讯CDN访问日志

公司使用腾讯cdn为网站静态内容加速,由于业务需求,需要每天下载昨天的日志(因为腾讯方面给出回复,访问日志会有2个小时或以上时间的延迟,所以不建议下载当天日志,所以每天统计前一天的日志以做分析).因为cdn是由运维来管理,但是这个需求是业务的,如果每天都由运维进行下载,再通过邮件或其他工具发送,可能就显得麻烦.所幸腾讯CDN提供了API接口,因此采用shell脚本调用API进行下载的方式,定期下载日志,这样只要业务人员运行这个脚本就能自行下载日志,解放了运维的工作. #!/bin/bash ##

openstack 调用API 实现云主机的IO 控制,CGroup 策略

# vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright (c) 2011 X.commerce, a business unit of eBay Inc. # Copyright 2010 United States Government as represented by the # Administrator of the National Aeronautics and Space Administration. # All Righ

32位汇编第四讲,干货分享,汇编注入的实现,以及快速定位调用API的数量(OD查看)

32位汇编第四讲,干货分享,汇编注入的实现,以及快速定位调用API的数量(OD查看) 昨天,大家可能都看了代码了,不知道昨天有没有在汇编代码的基础上,实现注入计算器. 如果没有,今天则会讲解,不过建议把昨天代码熟悉一遍(课程是紧跟着来的,请不要拉下任何一天,因为今天的知识, 可能就和昨天的知识挂钩,昨天的知识,和前天的挂钩.....,当然你如你懂汇编,不是新手,那么则可以直接往下看) 一丶远程线程注入,和汇编远程注入的区别 昨天的代码,大家可能看了(没看也没有关系,就是远程线程注入的代码,开发角