aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Effect/ladspa/effectladspafactory.cpp
diff options
context:
space:
mode:
authortrialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38>2012-12-01 19:51:33 +0000
committertrialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38>2012-12-01 19:51:33 +0000
commit30ec69e4baa2307bddd3beac96b3efef37558d27 (patch)
tree04421f96611e2bf58cfd0dacfda579277da8fb15 /src/plugins/Effect/ladspa/effectladspafactory.cpp
parent6aa98e65dc08d3f8b8e107cf3e5285201a1a3487 (diff)
downloadqmmp-30ec69e4baa2307bddd3beac96b3efef37558d27.tar.gz
qmmp-30ec69e4baa2307bddd3beac96b3efef37558d27.tar.bz2
qmmp-30ec69e4baa2307bddd3beac96b3efef37558d27.zip
added Hebrew translation (author: Genghis Khan)
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@3006 90c681e8-e032-0410-971d-27865f9a5e38
Diffstat (limited to 'src/plugins/Effect/ladspa/effectladspafactory.cpp')
0 files changed, 0 insertions, 0 deletions
+0000 WASAPI: implemented function latency(), fixed drain() implementation' href='/qmmp/commit/src/plugins/Output/wasapi/outputwasapi.cpp?id=a236d38020b49c1a329f699fefecf02e359b47e8'>a236d3802
afefc873f
8ecf3a1af



a49047c93

472fac2ad
afefc873f













a49047c93




afefc873f
4abfc3230

3caee8cc0
afefc873f



a49047c93
afefc873f





afefc873f


a49047c93
afefc873f


4abfc3230
afefc873f
4abfc3230






a49047c93
4abfc3230









a49047c93
4abfc3230

afefc873f




a49047c93
afefc873f


8e0b73ced


































472fac2ad
8e0b73ced
472fac2ad
8e0b73ced
472fac2ad

8e0b73ced






3caee8cc0
8ecf3a1af

a49047c93
8ecf3a1af




3caee8cc0
8e0b73ced




472fac2ad
8e0b73ced




472fac2ad
afefc873f
472fac2ad
afefc873f


472fac2ad
afefc873f
472fac2ad
afefc873f


afefc873f
472fac2ad
afefc873f
472fac2ad
afefc873f


afefc873f
472fac2ad


afefc873f


afefc873f

a236d3802


afefc873f



8e0b73ced
a49047c93
8e0b73ced
472fac2ad
8e0b73ced

472fac2ad
8e0b73ced
472fac2ad
8e0b73ced
472fac2ad

afefc873f
85473783d
472fac2ad


afefc873f
afefc873f
5d154b50f







472fac2ad
afefc873f
472fac2ad
afefc873f

472fac2ad


afefc873f



472fac2ad
a236d3802

afefc873f



472fac2ad
afefc873f



472fac2ad
afefc873f



472fac2ad


afefc873f

472fac2ad
afefc873f
472fac2ad

afefc873f


472fac2ad
afefc873f
472fac2ad

a49047c93
afefc873f
472fac2ad
afefc873f
472fac2ad
a49047c93
afefc873f
472fac2ad
afefc873f
472fac2ad
a49047c93
472fac2ad



a49047c93
472fac2ad

afefc873f
472fac2ad
a49047c93
afefc873f
afefc873f


472fac2ad
afefc873f


472fac2ad

afefc873f

472fac2ad
afefc873f


472fac2ad

a49047c93
afefc873f

472fac2ad
afefc873f
472fac2ad
afefc873f
a49047c93
afefc873f



472fac2ad
afefc873f

472fac2ad
afefc873f
472fac2ad



afefc873f




472fac2ad
afefc873f

472fac2ad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
                                                                             
                                                                             
                                                                             





















                                                                              





                                           
                  
                         

                          
                                       
 



                                                      

                                                     
                                                                  













                                                    




                                    
                     

                                                                  
                                                                           



                              
                        





                                                                                                                                    


                                                                                       
                                 


                      
                                             
      






                                                                                                         
                                 









                                                                                                                       
                                 

                          




                                                                                                            
                                  


                      


































                                                                                            
                                                  
      
                                              
          

                                               






                                               
                                                                                                    

                                                                                   
                                                                                        




                                                                          
                                                                                                                         




                                                                                               
                                                                          




                                                                                                  
                                                                                                        
      
                                                                                               


                      
                                                                                                             
      
                                                                                               


                      
 
                                                   
      
                                                                                          


                      
                                      


                                             


                 

                               


                                                



                                                                 
                       
                           
                     
                      

                                                
                                                      
 
                                                                             
 

                                    
      
                                                                 


                                                                          
      
 







                                                              
                                                                             
      
                                                                                              

                   


                                                          



                           
                       

                                                                                



                             
                            



                            
                            



                           


                             

  
                                                      
  

                                 


                                  
                       
      

                                   
                                  
      
                      
      
                                  
                                 
      
                  
      
                              
                             



                                    
                                   

                             
      
                                         
                                        
      


                    
                             


                                                                  

                                                                         

  
                              


                                                                  

                                                              
                                           

  
                                                        
  
                                                                              
      
                                                                                                                          



                    
                                            

                        
                                                                              
      



                                                                              




                     
                             

                         
  
/***************************************************************************
 *   Copyright (C) 2016-2019 by Ilya Kotov                                 *
 *   forkotov02@ya.ru                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include <QObject>
#include <QSettings>
#include <string.h>
#include <iostream>
#include <unistd.h>
#include <initguid.h>
#include <audioclient.h>
#include <endpointvolume.h>
#include <mmdeviceapi.h>
#include <mmreg.h>
#include <functiondiscoverykeys_devpkey.h>
#include <math.h>
#include <qmmp/buffer.h>
#include "outputwasapi.h"

#define WASAPI_BUFSIZE 10000000LL //1s

#ifndef AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM
#define AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM 0x80000000
#endif

OutputWASAPI *OutputWASAPI::instance = nullptr;
VolumeWASAPI *OutputWASAPI::volumeControl = nullptr;
OutputWASAPI::DWASAPIChannels OutputWASAPI::m_wasapi_pos[10]  = {
   {Qmmp::CHAN_FRONT_LEFT, SPEAKER_FRONT_LEFT},
   {Qmmp::CHAN_FRONT_RIGHT, SPEAKER_FRONT_RIGHT},
   {Qmmp::CHAN_FRONT_CENTER, SPEAKER_FRONT_CENTER},
   {Qmmp::CHAN_LFE, SPEAKER_LOW_FREQUENCY},
   {Qmmp::CHAN_REAR_LEFT, SPEAKER_BACK_LEFT},
   {Qmmp::CHAN_REAR_RIGHT, SPEAKER_BACK_RIGHT},
   {Qmmp::CHAN_REAR_CENTER, SPEAKER_BACK_CENTER},
   {Qmmp::CHAN_SIDE_LEFT, SPEAKER_SIDE_LEFT},
   {Qmmp::CHAN_SIDE_RIGHT, SPEAKER_BACK_RIGHT},
   {Qmmp::CHAN_NULL, 0}
};

OutputWASAPI::OutputWASAPI() : Output()
{
    m_pEnumerator = nullptr;
    m_pDevice = nullptr;
    m_pAudioClient = nullptr;
    m_pRenderClient = nullptr;
    m_pSimpleAudioVolume = nullptr;
    instance = this;
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    m_id = settings.value("WASAPI/device", "default").toString();
    m_exclusive = settings.value("WASAPI/exclusive_mode", false).toBool();
}

OutputWASAPI::~OutputWASAPI()
{
    instance = nullptr;
    uninitialize();
}

bool OutputWASAPI::initialize(quint32 freq, ChannelMap map, Qmmp::AudioFormat format)
{
    HRESULT result = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&m_pEnumerator);
    if(result != S_OK)
    {
        qWarning("OutputWASAPI: CoCreateInstance failed, error code = 0x%lx", result);
        m_pEnumerator = nullptr;
        return false;
    }

    if(m_id != "default" && !m_id.isEmpty())
    {
        WCHAR id[m_id.length() + 1];
        m_id.toWCharArray(id);
        id[m_id.length()] = 0;

        if((result = m_pEnumerator->GetDevice(id, &m_pDevice)) != S_OK)
        {
            qWarning("OutputWASAPI: IMMDeviceEnumerator::GetDevice failed, error code = 0x%lx", result);
            m_pDevice = nullptr;
        }
        else
            qDebug("OutputWASAPI: using device id: %s", qPrintable(m_id));
    }

    if(!m_pDevice)
    {
        if((result = m_pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &m_pDevice)) != S_OK)
        {
            qWarning("OutputWASAPI: IMMDeviceEnumerator::GetDefaultAudioEndpoint failed, error code = 0x%lx", result);
            m_pDevice = nullptr;
            return false;
        }
    }

    if((result = m_pDevice->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void**)&m_pAudioClient)) != S_OK)
    {
        qWarning("OutputWASAPI: IMMDevice::Activate failed, error code = 0x%lx", result);
        m_pAudioClient = nullptr;
        return false;
    }

    WAVEFORMATEXTENSIBLE wfex;
    wfex.Format.wFormatTag      = WAVE_FORMAT_EXTENSIBLE;
    wfex.Format.nChannels       = map.count();
    wfex.Format.nSamplesPerSec  = freq;
    wfex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE);

    if(format == Qmmp::PCM_S16LE)
    {
        wfex.Format.wBitsPerSample = 16;
        wfex.Samples.wValidBitsPerSample = 16;
    }
    else if(format == Qmmp::PCM_S24LE)
    {
        wfex.Format.wBitsPerSample  = 32;
        wfex.Samples.wValidBitsPerSample = 24;
    }
    else if(format == Qmmp::PCM_S32LE)
    {
        wfex.Format.wBitsPerSample  = 32;
        wfex.Samples.wValidBitsPerSample = 32;
    }
    else
    {
        format = Qmmp::PCM_S16LE;
        wfex.Format.wBitsPerSample  = 16;
        wfex.Samples.wValidBitsPerSample = 16;
    }

    wfex.Format.nBlockAlign     = (wfex.Format.wBitsPerSample / 8) * wfex.Format.nChannels;
    wfex.Format.nAvgBytesPerSec = wfex.Format.nSamplesPerSec * wfex.Format.nBlockAlign;

    //generate channel order
    ChannelMap out_map;
    int i = 0;
    DWORD mask = 0;
    while(m_wasapi_pos[i].pos != Qmmp::CHAN_NULL)
    {
        if(map.contains(m_wasapi_pos[i].pos))
        {
            mask |= m_wasapi_pos[i].chan_mask;
            out_map << m_wasapi_pos[i].pos;
        }
        i++;
    }

    wfex.dwChannelMask = mask;
    wfex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;

    AUDCLNT_SHAREMODE mode = m_exclusive ? AUDCLNT_SHAREMODE_EXCLUSIVE :  AUDCLNT_SHAREMODE_SHARED;
    DWORD streamFlags = 0;
    //enable channel matrixer and a sample rate converter if format is unsupported
    if(m_pAudioClient->IsFormatSupported(mode, (WAVEFORMATEX *)&wfex, nullptr) != S_OK)
    {
        streamFlags |= AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM;
        qDebug("OutputWASAPI: format is not supported, using converter");
    }

    if((result = m_pAudioClient->Initialize(mode, streamFlags, WASAPI_BUFSIZE, 0, (WAVEFORMATEX *)&wfex, NULL)) != S_OK)
    {
        qWarning("OutputWASAPI: IAudioClient::Initialize failed, error code = 0x%lx", result);
        return false;
    }

    if((result = m_pAudioClient->GetBufferSize(&m_bufferFrames)) != S_OK)
    {
        qWarning("OutputWASAPI: IAudioClient::GetBufferSize failed, error code = 0x%lx", result);
        return false;
    }

    if((result = m_pAudioClient->GetService(IID_IAudioRenderClient, (void**)&m_pRenderClient)) != S_OK)
    {
        qWarning("OutputWASAPI: IAudioClient::GetService failed, error code = 0x%lx", result);
        return false;
    }

    if((result = m_pAudioClient->GetService(IID_ISimpleAudioVolume, (void**)&m_pSimpleAudioVolume)) != S_OK)
    {
        qWarning("OutputWASAPI: IAudioClient::GetService failed, error code = 0x%lx", result);
        return false;
    }


    if((result = m_pAudioClient->Start()) != S_OK)
    {
        qWarning("OutputWASAPI: IAudioClient::Start failed, error code = 0x%lx", result);
        return false;
    }

    configure(freq, out_map, format);
    m_frameSize = sampleSize() * channels();
    if(volumeControl)
        volumeControl->restore();
    return true;
}

qint64 OutputWASAPI::latency()
{
    UINT32 frames = 0;
    m_pAudioClient->GetCurrentPadding(&frames);
    return frames * 1000 / sampleRate();
}

qint64 OutputWASAPI::writeAudio(unsigned char *data, qint64 len)
{
    UINT32 frames = 0;
    BYTE *pData = nullptr;
    DWORD flags = 0;
    DWORD result = 0;
    m_pAudioClient->GetCurrentPadding(&frames);

    UINT32 framesAvailable = m_bufferFrames - frames;

    UINT32 framesToWrite = qMin(framesAvailable, (UINT32)len / m_frameSize);

    //wait until buffer is not full
    if(framesToWrite == 0)
    {
        usleep(len * 1000000L / sampleRate() / m_frameSize / 2);
        m_pAudioClient->GetCurrentPadding(&frames);
        framesAvailable = m_bufferFrames - frames;
        framesToWrite = qMin(framesAvailable, (UINT32)len / m_frameSize);
    }

    if(format() == Qmmp::PCM_S24LE)
    {
        for(DWORD i = 0; i < framesToWrite * channels(); ++i)
        {
            ((quint32*) data)[i] <<= 8;
        }
    }

    if((result = m_pRenderClient->GetBuffer(framesToWrite, &pData)) != S_OK)
    {
        qWarning("OutputWASAPI: IAudioClient::GetBuffer failed, error code = 0x%lx", result);
        return -1;
    }
    memcpy(pData, data, framesToWrite * m_frameSize);
    m_pRenderClient->ReleaseBuffer(framesToWrite, flags);
    return framesToWrite * m_frameSize;
}

void OutputWASAPI::drain()
{
    UINT32 frames = 0;
    while((m_pAudioClient->GetCurrentPadding(&frames) == S_OK) && (frames > 0))
        usleep(50);
}

void OutputWASAPI::suspend()
{
    m_pAudioClient->Stop();
}

void OutputWASAPI::resume()
{
   m_pAudioClient->Start();
}

void OutputWASAPI::reset()
{
    m_pAudioClient->Stop();
    m_pAudioClient->Reset();
    m_pAudioClient->Start();
}

ISimpleAudioVolume *OutputWASAPI::simpleAudioVolume()
{
    return m_pSimpleAudioVolume;
}

void OutputWASAPI::uninitialize()
{
    if(m_pAudioClient)
    {
        m_pAudioClient->Stop();
        m_pAudioClient->Release();
        m_pAudioClient = nullptr;
    }
    if(m_pEnumerator)
    {
        m_pEnumerator->Release();
        m_pEnumerator = nullptr;
    }
    if(m_pDevice)
    {
        m_pDevice->Release();
        m_pDevice = nullptr;
    }
    if(m_pRenderClient)
    {
        m_pRenderClient->Release();
        m_pRenderClient = nullptr;
    }
    if(m_pSimpleAudioVolume)
    {
        m_pSimpleAudioVolume->Release();
        m_pSimpleAudioVolume = nullptr;
    }
}

/***** MIXER *****/
VolumeWASAPI::VolumeWASAPI()
{
    OutputWASAPI::volumeControl = this;
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    m_volume.left = settings.value("WASAPI/left_volume", 100).toInt();
    m_volume.right = settings.value("WASAPI/right_volume", 100).toInt();
}

VolumeWASAPI::~VolumeWASAPI()
{
    m_volume = volume();
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    settings.setValue("WASAPI/left_volume", m_volume.left);
    settings.setValue("WASAPI/right_volume", m_volume.right);
    OutputWASAPI::volumeControl = nullptr;
}

void VolumeWASAPI::setVolume(const VolumeSettings &vol)
{
    if(OutputWASAPI::instance && OutputWASAPI::instance->simpleAudioVolume())
    {