Just Intonation  Version 1.3.1 (19)
Explore key-independent dynamically adapting tuning in just intonation
audiogenerator.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2016-2017 Karolin Stange, Christoph Wick, and Haye Hinrichsen
3  *
4  * This file is part of JustIntonation.
5  *
6  * JustIntonation is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * JustIntonation is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with JustIntonation. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 //=============================================================================
21 // Virtual base class for a sound-generating module
22 //=============================================================================
23 
24 #include "audiogenerator.h"
25 
26 #include <QDebug>
27 #include <cmath>
28 
29 //#include "system/log.h"
30 
31 
32 //-----------------------------------------------------------------------------
33 // Constructor
34 //-----------------------------------------------------------------------------
35 
37  : mParameters()
38  , mMaxNumberOfFrames(4096)
39 {}
40 
41 
42 //-----------------------------------------------------------------------------
43 // Set parameters
44 //-----------------------------------------------------------------------------
45 
49 
51 {
52  if (mParameters==parameters) return;
53  mParameters = parameters;
54  const int minimal = 1;
55  const int maximal = 1000000;
56  if (mParameters.sampleRate < minimal)
57  {
58  LOGWARNING << "Sample rate is too small" << mParameters.sampleRate;
59  }
60  else if (mParameters.sampleRate > maximal)
61  {
62  LOGWARNING << "Sample rate is too large" << mParameters.sampleRate;
63  }
64  else
65  {
66  LOGMESSAGE << "Setting sample rate " << mParameters.sampleRate
67  << "buffersize = " << mParameters.bufferSize;
68  }
69 }
70 
71 
72 //-----------------------------------------------------------------------------
73 // Virtual function for sound generation
74 //-----------------------------------------------------------------------------
75 
101 
102 size_t AudioGenerator::generateSound (char *data, size_t maxSize)
103 {
104  static int t=0;
105  const double pi = 3.141592655;
106  quint16* buffer = (quint16*)data;
107 
108  if (mParameters.sampleSize==16)
109  {
110  if (maxSize<4) return 0;
111  const size_t N=std::min(maxSize/4,mMaxNumberOfFrames);
112  const double amplitude = 1024; // max 32768
113  for (size_t i=0; i<N; i++)
114  {
115  buffer[2*i]=amplitude*sin(440.0*2*pi*t/mParameters.sampleRate);
116  buffer[2*i+1]=amplitude*sin(220.0*2*pi*t/mParameters.sampleRate);
117  t++;
118  }
119  return N*4; // return number of written PCMType values
120  }
121  else if (mParameters.sampleSize==24)
122  {
123 
124  if (maxSize<6) return 0;
125  const size_t N=std::min(maxSize/6,mMaxNumberOfFrames);
126  const double amplitude = 1024*256; // max 32768
127  for (size_t i=0; i<N; i++)
128  {
129  qint32 left=amplitude*sin(440.0*2*pi*t/mParameters.sampleRate);
130  qint32 right=amplitude*sin(220.0*2*pi*t/mParameters.sampleRate);
131  buffer[3*i]=static_cast<quint16>(left&0xFFFFU);
132  buffer[3*i+1]=static_cast<quint16>(((left>>16)|(right<<16))&0xFFFFU);
133  buffer[3*i+2]=static_cast<quint16>((right>>8)&0xFFFFU);
134  t++;
135  }
136  return N*6; // return number of written PCMType values
137  }
138  else qDebug() << "AudioGenerator::generateSound: Sample size "
139  << mParameters.sampleSize << " is not yet implemented.";
140  return 0;
141 }
AudioGenerator()
Constructor.
int sampleSize
Sample size (16 or 24)
AudioParameters mParameters
Local copy of the audio parameters.
#define LOGMESSAGE
Definition: log.h:43
Structure holding the parameters and status of an audio device.
virtual size_t generateSound(char *data, size_t maxSize)
Virtual function for sound generation. This function generates an A440 on the right and an A220 on th...
int sampleRate
Actual sample rate.
void setParameters(const AudioParameters &parameters)
Set audio device parameters.
int bufferSize
Buffer size of the device if applicable.
size_t mMaxNumberOfFrames
Maximal number of frames per buffer.
#define LOGWARNING
Definition: log.h:44