[ create a new paste ] login | about

Link: http://codepad.org/a7XL8wNW    [ raw code | fork ]

D, pasted on Jul 13:
/+
 + How many sine waves can we calculate and play in less than 80% CPU Load.
 +
 + Author: Ross Bencina <rossb@audiomulch.com>
 + Author: Phil Burk <philburk@softsynth.com>
 +
 + Ported to D2 by Andrej Mitrovic, 2011.
 +
 + Use -version=PA_USE_ASIO to compile with ASIO support.
 +/

/*
 * PortAudio Portable Real-Time Audio Library
 * Latest version at: http://www.portaudio.com
 * <platform> Implementation
 * Copyright (c) 1999-2000 <author(s)>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal _in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * The text above constitutes the entire PortAudio license; however,
 * the PortAudio community also makes the following non-binding requests:
 *
 * Any person wishing to distribute modifications to the Software is
 * requested to send the modifications to the original developer so that
 * they can be incorporated into the canonical version. It is also
 * requested that these non-binding requests be included along with the
 * license above.
 */

module patest_maxsines;

import core.stdc.config;
import core.thread;
import std.conv;
import std.exception;
import std.math;
import std.range;
import std.stdio;
import std.string;

import portaudio.portaudio;
import portaudio.exception;
import portaudio.pa_asio;

import cmdln.interact;

enum MAX_SINES =    500;
enum MAX_USAGE =    0.8;

auto FREQ_TO_PHASE_INC(F) (F freq)
{
    return (freq / cast(float)SAMPLE_RATE);
}

auto MIN_PHASE_INC()
{
    return FREQ_TO_PHASE_INC(200.0f);
}

auto MAX_PHASE_INC()
{
    return (MIN_PHASE_INC * (1 << 5));
}

enum SAMPLE_RATE       = 44100;
enum TABLE_SIZE        = 512;
enum FRAMES_PER_BUFFER = 512;

struct paTestData
{
    int numSines;
    float[TABLE_SIZE + 1] sine; /* add one for guard point for interpolation */
    float[MAX_SINES] phases;
}

/* Convert phase between and 1.0 to sine value
 * using linear interpolation.
 */
float LookupSine(paTestData* data, float phase)
{
    float fIndex = phase * TABLE_SIZE;
    int index    = cast(int)fIndex;
    float fract  = fIndex - index;
    float lo     = data.sine[index];
    float hi     = data.sine[index + 1];
    float val    = lo + fract * (hi - lo);
    return val;
}

/*
   This routine will be called by the PortAudio stream when audio is needed.
   It may be called at interrupt level on some machines so don't do anything
   that could mess up the system like calling malloc() or free().
 */
extern (C) int patestCallback(const void* inputBuffer,
                              void* outputBuffer,
                              c_ulong framesPerBuffer,
                              const PaStreamCallbackTimeInfo* timeInfo,
                              PaStreamCallbackFlags statusFlags,
                              void* userData)
{
    version (Debug)
    {
        import std.math;
        FloatingPointControl fpc;
        fpc.enableExceptions(FloatingPointControl.severeExceptions);
    }

    paTestData* data = cast(paTestData*)userData;
    float* _out = cast(float*)outputBuffer;
    float outSample = 0.0;
    float scaler = 0.0;
    int numForScale;
    c_ulong i;
    int j;
    int finished = 0;

    /* Determine amplitude scaling factor */
    numForScale = data.numSines;

    if (numForScale < 8)
        numForScale = 8;  /* prevent pops at beginning */

    scaler = 1.0f / numForScale;

    for (i = 0; i < framesPerBuffer; i++)
    {
        float output   = 0.0;
        float phaseInc = MIN_PHASE_INC;
        float phase = 0.0;

        for (j = 0; j < data.numSines; j++)
        {
            /* Advance phase of next oscillator. */
            phase  = data.phases[j];
            phase += phaseInc;

            if (phase >= 1.0)
                phase -= 1.0;

            output += LookupSine(data, phase);
            data.phases[j] = phase;

            phaseInc *= 1.02f;

            if (phaseInc > MAX_PHASE_INC)
                phaseInc = MIN_PHASE_INC;
        }

        outSample = cast(float)(output * scaler);
        *_out++   = outSample; /* Left */
        *_out++   = outSample; /* Right */
    }

    return finished;
}

void patest_maxsines()
{
    int i;
    PaStream* stream;
    PaStreamParameters outputParameters;
    PaError error;
    paTestData data;
    double load;

    writefln("PortAudio Test: output sine wave. SR = %s, BufSize = %s", SAMPLE_RATE, FRAMES_PER_BUFFER);

    /* initialise sinusoidal wavetable */
    for (i = 0; i < TABLE_SIZE; i++)
    {
        data.sine[i] = cast(float)sin( (cast(double)i / cast(double)TABLE_SIZE) * PI * 2.);
    }

    data.sine[TABLE_SIZE] = data.sine[0];                  /* set guard point */
    data.phases = 0.0;

    outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */
    enforce(outputParameters.device != paNoDevice, new PortaudioException("Error: No default output device."));

    outputParameters.channelCount = 2;         /* Stereo output. */
    outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output. */
    outputParameters.hostApiSpecificStreamInfo = null;
    outputParameters.suggestedLatency          = Pa_GetDeviceInfo(outputParameters.device)
                                                 .defaultHighOutputLatency;
    error = Pa_OpenStream(&stream,
                          null,                  /* no input */
                          &outputParameters,
                          SAMPLE_RATE,
                          FRAMES_PER_BUFFER,
                          paClipOff,             /* No out of range samples should occur. */
                          &patestCallback,
                          &data);

    enforce(error >= PaErrorCode.paNoError, new PortaudioException(error));
    
    error = Pa_StartStream(stream);
    enforce(error >= PaErrorCode.paNoError, new PortaudioException(error));
    
    /* Play an increasing number of sine waves until we hit MAX_USAGE */
    do
    {
        data.numSines++;
        Pa_Sleep(200);
        load = Pa_GetStreamCpuLoad(stream);
        writefln("numSines = %s, CPU load = %f", data.numSines, load);
    } while ((load < MAX_USAGE) && (data.numSines < MAX_SINES));

    Pa_Sleep(2000);     /* Stay for 2 seconds around 80% CPU. */

    error = Pa_StopStream(stream);
    enforce(error >= PaErrorCode.paNoError, new PortaudioException(error));

    error = Pa_CloseStream(stream);
    enforce(error >= PaErrorCode.paNoError, new PortaudioException(error));

    writeln("Test finished.");
}

int main(string[] args)
{
    int result;

    try
    {
        auto error = Pa_Initialize();
        enforce(error >= PaErrorCode.paNoError, new PortaudioException(error));
    }
    catch (PortaudioException exc)
    {
        result = -1;
        writeln(exc);
        return result;
    }

    try
    {
        patest_maxsines();
    }
    catch (PortaudioException exc)
    {
        result = -1;
        writeln(exc);
    }
    finally
    {
        Pa_Terminate();
    }

    return result;
}


Create a new paste based on this one


Comments: