Skip to content

Commit 91b5383

Browse files
author
Rafael Vega
committed
Preliminary examples. Using the portaudio API to create a wave (output only) and creating a delay (input and output).
1 parent 4688521 commit 91b5383

File tree

4 files changed

+199
-0
lines changed

4 files changed

+199
-0
lines changed

sandbox/portaudio_tutorial/Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CFLAGS =
2+
INCL =
3+
LIBS = -lportaudio
4+
5+
all: echo sawtooth
6+
7+
echo: echo.c
8+
gcc echo.c $(CFLAGS) $(INCL) -o build/echo $(LIBS)
9+
10+
sawtooth:
11+
g++ sawtooth.cpp $(CFLAGS) $(INCL) -o build/sawtooth $(LIBS)
12+
13+
clean:
14+
-rm build/*
15+
16+
.PHONY: clean
6 KB
Binary file not shown.

sandbox/portaudio_tutorial/echo.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <stdio.h>
2+
#include <time.h>
3+
#include <string.h>
4+
#include <stdlib.h>
5+
#include <portaudio.h>
6+
7+
#define SR 44100.
8+
#define BUF 512
9+
#define END 60
10+
11+
typedef struct _mydata{
12+
float delay[(int)SR/2];
13+
int rp;
14+
} mydata;
15+
16+
17+
int audio_callback(const void *input, void *output,
18+
unsigned long frameCount,
19+
const PaStreamCallbackTimeInfo *timeInfo,
20+
PaStreamCallbackFlags statusFlags,
21+
void *userData){
22+
23+
24+
mydata *p = (mydata *)userData;
25+
int i, rp = p->rp;
26+
float out, *delay = p->delay;
27+
float *inp = (float *) input, *outp = (float *) output;
28+
for(i=0; i < frameCount; i++){
29+
out = delay[rp];
30+
delay[rp++] = inp[i] + out*0.1;
31+
if(rp >= SR/2) rp = 0;
32+
outp[i] = out;
33+
}
34+
p->rp = rp;
35+
36+
return paContinue;
37+
38+
}
39+
40+
int main(int agrc, char** argv){
41+
42+
PaError err;
43+
PaDeviceIndex devin,devout, ndev;
44+
const PaDeviceInfo *info;
45+
PaStreamParameters inparam, outparam;
46+
PaStream *handle;
47+
int i;
48+
mydata *data = (mydata *) calloc(sizeof(mydata),1);
49+
50+
err = Pa_Initialize();
51+
if( err == paNoError){
52+
ndev = Pa_GetDeviceCount();
53+
for(i=0; i<ndev; i++){
54+
info = Pa_GetDeviceInfo((PaDeviceIndex) i);
55+
if(info->maxOutputChannels > 0) printf("output device: ");
56+
if (info->maxInputChannels > 0) printf("input device: ");
57+
printf("%d: %s\n", i, info->name);
58+
}
59+
60+
printf("choose device for input: ");
61+
scanf("%d", &devin);
62+
printf("choose device for output: ");
63+
scanf("%d", &devout);
64+
65+
memset(&inparam, 0, sizeof(PaStreamParameters));
66+
inparam.device = devin;
67+
inparam.channelCount = 1;
68+
inparam.sampleFormat = paFloat32;
69+
memset(&outparam, 0, sizeof(PaStreamParameters));
70+
outparam.device = (PaDeviceIndex) devout;
71+
outparam.channelCount = 1;
72+
outparam.sampleFormat = paFloat32;
73+
74+
err = Pa_OpenStream(&handle,&inparam,&outparam,SR,BUF,paNoFlag,
75+
audio_callback, data);
76+
77+
if(err == paNoError){
78+
err = Pa_StartStream(handle);
79+
if(err == paNoError){
80+
while(Pa_GetStreamTime(handle) < END);
81+
Pa_StopStream(handle);
82+
} else printf("%s \n", Pa_GetErrorText(err));
83+
Pa_CloseStream(handle);
84+
} else printf("%s \n", Pa_GetErrorText(err));
85+
Pa_Terminate();
86+
} else printf("%s \n", Pa_GetErrorText(err));
87+
88+
free(data);
89+
return 0;
90+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include "portaudio.h"
2+
#include <iostream>
3+
#include <string>
4+
using namespace std;
5+
6+
/*
7+
* TYPE DEFINITIONS
8+
*/
9+
typedef struct {
10+
float left_phase;
11+
float right_phase;
12+
}
13+
paTestData;
14+
15+
/*
16+
* CONSTANT DEFINITIONS
17+
*/
18+
#define SAMPLE_RATE (44100)
19+
20+
/*
21+
* GLOBAL VARIABLES
22+
*/
23+
static paTestData data;
24+
25+
/*
26+
* PORT AUDIO CALLBACK
27+
*/
28+
static int paCallback( const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData ){
29+
/* Cast data passed through stream to our structure. */
30+
paTestData *data = (paTestData*)userData;
31+
float *out = (float*)outputBuffer;
32+
unsigned int i;
33+
(void) inputBuffer; /* Prevent unused variable warning. */
34+
35+
for( i=0; i<framesPerBuffer; i++ ){
36+
*out++ = data->left_phase; /* left */
37+
*out++ = data->right_phase; /* right */
38+
/* Generate simple sawtooth phaser that ranges between -1.0 and 1.0. */
39+
data->left_phase += 0.01f;
40+
/* When signal reaches top, drop back down. */
41+
if( data->left_phase >= 1.0f ) data->left_phase -= 2.0f;
42+
/* higher pitch so we can distinguish left and right. */
43+
data->right_phase += 0.03f;
44+
if( data->right_phase >= 1.0f ) data->right_phase -= 2.0f;
45+
}
46+
return 0;
47+
}
48+
49+
/*
50+
* ENTRY POINT
51+
*/
52+
int main (int argc, char const* argv[]){
53+
PaError err;
54+
55+
err = Pa_Initialize();
56+
if(err!= paNoError){
57+
cout << "PortAudio error:" << Pa_GetErrorText( err );
58+
return 1;
59+
}
60+
61+
/* Open an audio I/O stream. */
62+
PaStream *stream;
63+
err = Pa_OpenDefaultStream(&stream,
64+
0, /* no input channels */
65+
2, /* stereo output */
66+
paFloat32, /* 32 bit floating point output */
67+
SAMPLE_RATE,
68+
256, /* frames per buffer, i.e. the number of sample frames that PortAudio will request from the callback. Many apps may want to use paFramesPerBufferUnspecified, which tells PortAudio to pick the best, possibly changing, buffer size.*/
69+
paCallback, /* this is your callback function */
70+
&data /*This is a pointer that will be passed to your callback*/
71+
);
72+
if(err!= paNoError){
73+
cout << "PortAudio error:" << Pa_GetErrorText( err );
74+
return 1;
75+
}
76+
77+
/* Start the stream */
78+
err = Pa_StartStream( stream );
79+
if(err!= paNoError){
80+
cout << "PortAudio error:" << Pa_GetErrorText( err );
81+
return 1;
82+
}
83+
84+
Pa_Sleep(60000);
85+
86+
err = Pa_StopStream( stream );
87+
if(err!= paNoError){
88+
cout << "PortAudio error:" << Pa_GetErrorText( err );
89+
return 1;
90+
}
91+
92+
return 0;
93+
}

0 commit comments

Comments
 (0)