File: DllTest.cpp

package info (click to toggle)
soundtouch 2.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 800 kB
  • sloc: cpp: 5,189; pascal: 408; makefile: 68; ansic: 65; sh: 43
file content (114 lines) | stat: -rw-r--r-- 3,664 bytes parent folder | download | duplicates (3)
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
////////////////////////////////////////////////////////////////////////////////
///
/// DllTest.cpp : This is small app main routine used for testing sound processing
/// with SoundTouch.dll API
///
/// Author        : Copyright (c) Olli Parviainen
/// Author e-mail : oparviai 'at' iki.fi
/// SoundTouch WWW: http://www.surina.net/soundtouch
///
////////////////////////////////////////////////////////////////////////////////

#include <string>
#include <iostream>
#include <fstream>
#include "../SoundTouchDLL.h"
#include "../../SoundStretch/WavFile.h"

using namespace std;

// DllTest main
int main(int argc, char *argv[])
{
    // Check program arguments
    if (argc < 4)
    {
        cout << "Too few arguments. Usage: DllTest [infile.wav] [outfile.wav] [sampletype]" << endl;
        return -1;
    }

    const char *inFileName = argv[1];
    const char *outFileName = argv[2];
    string str_sampleType = argv[3];

    bool floatSample;
    if (str_sampleType.compare("float") == 0)
    {
        floatSample = true;
    }
    else if (str_sampleType.compare("short") == 0)
    {
        floatSample = false;
    }
    else
    { 
        cerr << "Missing or invalid sampletype '" << str_sampleType << "'. Expected either short or float" << endl;
        return -1;
    }

    try
    {
        // Open input & output WAV files
        WavInFile inFile(inFileName);
        int numChannels = inFile.getNumChannels();
        int sampleRate = inFile.getSampleRate();
        WavOutFile outFile(outFileName, sampleRate, inFile.getNumBits(), numChannels);

        // Create SoundTouch DLL instance
        HANDLE st = soundtouch_createInstance();
        soundtouch_setChannels(st, numChannels);
        soundtouch_setSampleRate(st, sampleRate);
        soundtouch_setPitchSemiTones(st, 2);

        cout << "processing with soundtouch.dll routines";

        if (floatSample)
        {
            // Process file with SoundTouch.DLL float sample (default) API
            float fbuffer[2048];
            int nmax = 2048 / numChannels;

            cout << " using float api ..." << endl;
            while (inFile.eof() == false)
            {
                int n = inFile.read(fbuffer, nmax * numChannels) / numChannels;
                soundtouch_putSamples(st, fbuffer, n);
                do
                {
                    n = soundtouch_receiveSamples(st, fbuffer, nmax);
                    outFile.write(fbuffer, n * numChannels);
                } while (n > 0);
            }
        }
        else
        {
            // Process file with SoundTouch.DLL int16 (short) sample API.
            // Notice that SoundTouch.dll does internally processing using floating
            // point routines so the int16 API is not any faster, but provided for 
            // convenience.
            short i16buffer[2048];
            int nmax = 2048 / numChannels;

            cout << " using i16 api ..." << endl;
            while (inFile.eof() == false)
            {
                int n = inFile.read(i16buffer, nmax * numChannels) / numChannels;
                soundtouch_putSamples_i16(st, i16buffer, n);
                do
                {
                    n = soundtouch_receiveSamples_i16(st, i16buffer, nmax);
                    outFile.write(i16buffer, n * numChannels);
                } while (n > 0);
            }
        }

        soundtouch_destroyInstance(st);
        cout << "done." << endl;
    }
    catch (const runtime_error &e)
    {
        cerr << e.what() << endl;
    }

    return 0;
}