Need simple sound

Sebastiaan Koppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 5 03:19:54 PDT 2017


On Wednesday, 5 July 2017 at 09:43:05 UTC, Martin Tschierschke 
wrote:
> On Wednesday, 5 July 2017 at 07:21:45 UTC, Sebastiaan Koppe 
> wrote:
>> On Wednesday, 5 July 2017 at 05:34:37 UTC, FoxyBrown wrote:
>>> On Tuesday, 4 July 2017 at 20:37:44 UTC, Sebastiaan Koppe 
>>> wrote:
>>>> Portaudio is simple as well. And nice cross platform.
>>>
>>> are there any bindings?
>>
>> Sure, see http://code.dlang.org/packages/portaudio
>
> Sorry, for that question but now, as I have several different 
> options -
> all not 'overdocumented' -  so what would be the shortest 
> possible D program waiting for input and playing a sound from a 
> file in parallel?
>
> Something short like the single-file dub example...?
>
> #!/usr/bin/env dub
> /+ dub.sdl:
> 	name "hello"
> +/
> void main() {
> 	
>  writef("Do you like the music?");
>           char[] answer;
>           answer = readln();
>           writef("This was your Answer: ", answer);
> 	
> }

This one plays a wav file. Couldn't get dub to do single-file on 
it. It kept giving linker errors. (Took example from 
https://github.com/v--/portaudio/blob/master/examples/pa_test.d)

**dub.sdl
name "audio"
description "plays audio file"
authors "Sebastiaan Koppe"
dependency "portaudio" version="==1.0.0"
dependency "wave-d" version="~>1.0.6"

configuration "executable" {
   mainSourceFile "audio.d"
   targetType "executable"
}

**audio.d
import std.stdio;
import waved;
import deimos.portaudio;
import std.conv, std.stdio;
import std.algorithm : min;

struct Audio {
     float* samples;
     size_t frames;
     size_t position;
}

extern(C) int callback(const(void)* inputBuffer, void* 
outputBuffer,
                              size_t framesPerBuffer,
                              const(PaStreamCallbackTimeInfo)* 
timeInfo,
                              PaStreamCallbackFlags statusFlags,
                              void *userData)
{
     auto pout = cast(float*)outputBuffer;
     Audio* audio = cast(Audio*)userData;

     enum vol = 0.2f;
     size_t frames = min(audio.frames - audio.position, 
framesPerBuffer);

     foreach(i; 0 .. frames)
         *pout++ = vol * audio.samples[i + audio.position];
     foreach(i; frames .. framesPerBuffer)
         *pout++ = 0;

     audio.position += framesPerBuffer;
     return frames == framesPerBuffer ? paContinue : paComplete;
}

int main()
{
     PaStream* stream;
     PaError err;

     Sound input = decodeWAV("my_wav_file.wav");
     string answer;

     auto audio = Audio(input.samples.ptr, input.samples.length);

     if ((err = Pa_Initialize()) != paNoError) goto Lerror;

     if ((err = Pa_OpenDefaultStream(&stream,
                                     0,
                                     input.channels,
                                     paFloat32,
                                     cast(double)input.sampleRate,
                                     paFramesPerBufferUnspecified,
                                     &callback,
                                     &audio))
         != paNoError) goto Lerror;

     if ((err = Pa_StartStream(stream)) != paNoError) goto Lerror;

     writef("Do you like the music?");
     answer = readln();
     writefln("This was your Answer: %s", answer);

     if ((err = Pa_StopStream(stream)) != paNoError) goto Lerror;
     if ((err = Pa_CloseStream(stream)) != paNoError) goto Lerror;
     if ((err = Pa_Terminate()) != paNoError) goto Lerror;

     return 0;
  Lerror:
     stderr.writefln("error %s", to!string(Pa_GetErrorText(err)));
     return 1;
}


More information about the Digitalmars-d-learn mailing list