File indexing completed on 2024-04-21 03:40:35
0001 /************************************************************************************* 0002 * Copyright (C) 2019 Aleix Pol Gonzalez <aleixpol@kde.org> * 0003 * * 0004 * This program is free software; you can redistribute it and/or * 0005 * modify it under the terms of the GNU General Public License * 0006 * as published by the Free Software Foundation; either version 2 * 0007 * of the License, or (at your option) any later version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, * 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0012 * GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License * 0015 * along with this program; if not, write to the Free Software * 0016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 0017 *************************************************************************************/ 0018 0019 #include <stdio.h> 0020 #include <math.h> 0021 #include <stdlib.h> 0022 #include <sndfile.h> 0023 #include <QGuiApplication> 0024 #include <QCommandLineParser> 0025 #include <QColor> 0026 #include <plotsmodel.h> 0027 #include <plotsfactory.h> 0028 #include <planecurve.h> 0029 0030 #define SAMPLE_RATE 44100.0 0031 0032 using namespace Analitza; 0033 0034 void recordPlot(PlaneCurve* curve) 0035 { 0036 curve->setResolution(3 * SAMPLE_RATE); 0037 curve->update(QRect(-10, 10, 20, -20)); 0038 0039 const auto points = curve->points(); 0040 QVector<qreal> buffer; 0041 buffer.reserve(points.size()); 0042 0043 // for(const auto &p: points) { 0044 // buffer.append(p.y()); 0045 // } 0046 const double frequency = 40; 0047 for(const auto &p: points) { 0048 for (int i = 0; i<3; ++i) 0049 buffer.append(sin( (2.0 * M_PI * frequency * p.y()) * (points.count() / SAMPLE_RATE))); 0050 } 0051 0052 { 0053 SF_INFO sndInfo; 0054 0055 sndInfo.samplerate = SAMPLE_RATE; 0056 sndInfo.format = SF_FORMAT_PCM_16 | SF_FORMAT_WAV; 0057 sndInfo.channels = 1; 0058 sndInfo.frames = buffer.size(); 0059 0060 SNDFILE *output = sf_open("plot.wav", SFM_WRITE, &sndInfo); 0061 sf_writef_double(output, buffer.data(), buffer.size()); 0062 sf_close(output); 0063 } 0064 } 0065 0066 int main(int argc, char *argv[]) 0067 { 0068 QGuiApplication app(argc, argv); 0069 QCommandLineParser parser; 0070 parser.setApplicationDescription(QStringLiteral("AudioPlots")); 0071 parser.addHelpOption(); 0072 parser.process(app); 0073 0074 if (parser.positionalArguments().isEmpty()) { 0075 parser.showHelp(1); 0076 return 1; 0077 } 0078 0079 const QString expression = parser.positionalArguments().constLast(); 0080 PlotsFactory* s = PlotsFactory::self(); 0081 QScopedPointer<FunctionGraph> p(s->requestPlot(Analitza::Expression(expression), Dim2D).create(Qt::green, QStringLiteral("curve"))); 0082 0083 PlaneCurve* curve = dynamic_cast<PlaneCurve*>(p.data()); 0084 if (!curve) { 0085 QTextStream(stderr) << "Cannot produce a sound curve from " << expression << '\n'; 0086 return 1; 0087 } 0088 recordPlot(curve); 0089 return 0; 0090 }