OpenAL不能正常播放声音吗?

时间:2019-06-05 22:09:35

标签: c++ openal

我正在以16位单声道@ 44.1Khz设置OpenAL声音渲染,我已经组装了一个声音数据数组,作为一个简短的int达到+ -16,000左右,但是当我加载AL缓冲区时,没有声音。

我已经验证我的代码将数据放入我指定的短代码中,并且无法验证AL是否有效地生成了缓冲区。但是,我已经验证了该组成部分已被写入到短内容中,并且这些行确实在编译器中通过了。启动程序时,AL无法检测到音频设备,并给我一个错误:ALCmmdev播放错误:设备初始化失败。

无论哪种方式,下面是相关代码: main.cpp:

        void trackPlay(){
            //tracking =
            ALCdevice *dev = NULL;
            ALCcontext *ctx = NULL;

            const char *defname = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
            std::cout << "Default device: " << defname << std::endl;

            dev = alcOpenDevice(defname);
            ctx = alcCreateContext(dev, NULL);
            alcMakeContextCurrent(ctx);
            for(int i=0;i<instruments.size();i++){
                instruments[i].currentStep = tracking;
                instruments[i].assembleSongData();
                alBufferData(instruments[i].soundbuffer, AL_FORMAT_MONO16, &instruments[i].samples, sizeof(instruments[i].samples), 44100);
                instruments[i].soundsource = i;
                alGenSources(i, &instruments[i].soundsource);
                alSourcei(instruments[i].soundsource, AL_BUFFER, instruments[i].soundbuffer);
            }
            for(int i=0;i<instruments.size();i++){
                alSourcePlay(instruments[i].soundsource);

            }


        }

entities.h:

        void playVertice(short data[], float srate,  float freq, float amp, float phase,
                                 float decayf, float bpm,float am,float amfreq, float fm, float fmfreq,float step){
            float ampmult = 1024.0f;
            float cycle = 2.0f;
            float minute = 60.0f;
            float i = 0.0f;
            float ampadj =ampmult* amp;
            float M_PI = PI;

            int sizeSS=((44100*(60/tempo))*composition.size()+ (44100*(60/tempo))*16);
            //data=data;
            for(float i = step*(44100*(60/tempo));i<sizeSS;(i)++){
                //cout<<"\nattempting transfer at "<<i;
                data[(short)i] += (ampadj*(((sin(((freq*2.0*M_PI)/44100*i)*phase+((freq*2.0*M_PI)/44100*i)*(fm*sin((fmfreq*2.0*M_PI)/44100*i))))
                                            +(sin(((freq*2.0*M_PI)/44100*i)+((freq*2.0*M_PI)/44100*i)*(fm*sin((fmfreq*2.0*M_PI)/44100*i))))
                                            *(sin((amfreq*2.0*M_PI)/44100*i)*am))));
            }
        }
        short samples;
        void assembleSongData(){
            updateVoices();
            assembleVoices();
            float bpm = tempo;
            //ALfloat *soundstream;
            ALuint srate = 44100;

            alGenBuffers(1, &soundbuffer);

            short *samples=(short*)malloc(4*((44100*(60/tempo))*composition.size()+ (44100*(60/tempo))*16));

            cout<<composition.size();

            for (int it=0;composition.size()>it&&stepvoices.size()>it;it++){
                for(int iu=0; iu < composition[it].size() && iu < stepvoices[it].size();iu++){
                    for (int ii=0;ii<stepvoices[it][iu].size()&&it<composition[it][iu].size();ii++){
                        for (int iy=0;iy<composition[it][iu].size();iy++){
                            for (int i=0;i<voices_spinner;i++){
                                //cout<<it<<"\n";
                                float amp=composition.at(it).at(iu).at(iy)*stepvoices[it][iu][i];
                                float pitch=composition.at(it).at(iu).at(iy)*stepvoices[it][iu][i];
                                float phase=stepvoices[it][iu][i];
                                float decayf=decaystep.at(it);
                                playVertice(samples,srate,pitch,amp,phase,
                                            decayf,tempo,amint_spinner,am_spinner,fmint_spinner,fm_spinner,it);
                            }
                        }
                    }
                }
            }
            cout<<"\nwrote composition\n";


        }

我收到一个AlcDeviceFailed错误,但是当我播放歌曲时,它实际上检测到了我的设备。无论如何,我以前都可以通过音频设备在该软件上播放该软件,但是那时数据被全部写入-32768左右,所以我的扬声器只剪了5分钟。我希望现在能听到一些音频播放,但是我的AL代码有问题,我不知道该怎么办。

0 个答案:

没有答案