This GitLab instance reached the end of its service life. It won't be possible to create new users or projects.

Please read the deprecation notice for more information concerning the deprecation timeline

Visit migration.git.tu-berlin.de (internal network only) to import your old projects to the new GitLab platform 📥

Commit 5542ab0f by Henrik von Coler

Expanded wav_example to stereo (and mono)!

parent e929d24f
......@@ -15,7 +15,7 @@ endif
# libraries to link
LDFLAGS = -ljack -llo -lsndfile
SRCS = wav_example.cpp oscman.cpp singlesample.cpp
SRCS = main.cpp wav_example.cpp oscman.cpp singlesample.cpp
DEPS = oscman.h gain_example.h
all: wav_example
......@@ -23,8 +23,8 @@ all: wav_example
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
wav_example: wav_example.o oscman.o singlesample.o
$(CC) $(DBFLAGS) -o wav_example wav_example.o oscman.o singlesample.o $(LDFLAGS)
wav_example: main.o wav_example.o oscman.o singlesample.o
$(CC) $(DBFLAGS) -o wav_example main.o wav_example.o oscman.o singlesample.o $(LDFLAGS)
clean:
......
......@@ -13,10 +13,7 @@
#include "oscman.h"
///
/// \brief OscMan::OscMan
/// \param p
///
OscMan::OscMan(int p)
{
......@@ -43,15 +40,6 @@ OscMan::OscMan(int p)
}
///
////// \brief OscMan::gain_callback
////// \param path
////// \param types
////// \param argv
////// \param argc
////// \param data
////// \param user_data
////// \return
int OscMan::speed_callback(const char *path, const char *types, lo_arg ** argv,
int argc, void *data, void *user_data )
{
......@@ -67,10 +55,7 @@ int argc, void *data, void *user_data )
}
///
/// \brief OscMan::get_speed
/// \return
///
double OscMan::get_speed()
{
return speed;
......
......@@ -39,14 +39,31 @@ private:
double speed;
///
////// \brief OscMan::speed_callback
////// \param path
////// \param types
////// \param argv
////// \param argc
////// \param data
////// \param user_data
////// \return
static int speed_callback(const char *path, const char *types, lo_arg ** argv,
int argc, void *data, void *user_data);
public:
///
/// \brief OscMan::OscMan
/// \param p
///
OscMan(int p);
///
/// \brief OscMan::get_speed
/// \return
///
double get_speed();
......
#include "singlesample.h"
SingleSample::SingleSample(std::string filePath)
SingleSample::SingleSample(std::string filePath, int fs)
{
rate = 1;
pos = 0.0;
FS = fs;
read_wavefile(filePath);
}
SingleSample::~SingleSample()
......@@ -15,14 +20,12 @@ SingleSample::~SingleSample()
double SingleSample::get_rate()
{
return rate;
}
void SingleSample::set_rate(double r)
{
rate = r;
}
double SingleSample::get_pos()
......@@ -36,29 +39,7 @@ void SingleSample::set_pos(double p)
}
/// loop play
void SingleSample::get_frame(int n, float* a)
{
for(int i=0; i<n; i++)
{
pos = pos+rate;
// are we within bounds?
if((int)pos<L && (int)pos>=0 )
{
a[i] = get_sample(pos);
}
else
{
if ( rate >= 0)
pos = 0;
else
pos = L-1;
}
}
}
int SingleSample::get_L()
......@@ -66,7 +47,7 @@ int SingleSample::get_L()
return L;
}
double* SingleSample::get_x()
double** SingleSample::get_x()
{
return x;
}
......@@ -74,7 +55,7 @@ double* SingleSample::get_x()
void SingleSample::set_f(int in)
{
f = in;
F = in;
}
void SingleSample::set_sr(int in)
......@@ -93,31 +74,36 @@ void SingleSample::set_nChannels(int in)
nChannels = in;
}
void SingleSample::initialize()
int SingleSample::get_nChannels()
{
x = new double[L*sizeof(double)];
return nChannels;
}
double SingleSample::get_sample(int pos)
void SingleSample::initialize()
{
/// Return sample at integer position only.s
return x[pos];
}
//double SingleSample::get_sample(int chan, int pos)
//{
// return x[chan][pos];
//}
int SingleSample::getFS()
{
return info.samplerate;
}
double SingleSample::get_sample(double pos)
double SingleSample::get_sample(int chan, double pos)
{
/// Use linear interpolation to
/// get the next sample at the floating point
/// position in time!
if(pos>=1 && pos<L)
// Use linear interpolation to
// get the next sample at the floating point
// position index!
if(pos>=0 && pos<=L)
{
double nearRound;
......@@ -126,29 +112,33 @@ double SingleSample::get_sample(double pos)
int lowerBound = (int) nearRound;
int upperBound = lowerBound +1;
double lowerSP = x[lowerBound];
double upperSP = x[upperBound];
double lowerSP = x[chan][lowerBound];
double upperSP = x[chan][upperBound];
double interp = (1-fract)*lowerSP + fract*upperSP;
return interp;
}
else
{
return 0;
cout << "Out of range!: " << endl;
}
}
void SingleSample::step()
{
pos = pos+rate;
double R = rate * GR;
pos = pos+R;
// loop in forward playback
if(pos>=L)
pos = 0;
if(pos>F)
pos = 0 + pos-(double)F;
// loop in reverse playback
if(pos<0)
pos = L;
pos = F;
}
......@@ -156,7 +146,7 @@ void SingleSample::step()
void SingleSample::read_wavefile(std::string filePath)
{
cout << "Trying to read: " << filePath << endl;
cout << "Reading: " << filePath << endl;
/// Open the audio file using sndfile!
info.format = 0;
......@@ -168,17 +158,44 @@ void SingleSample::read_wavefile(std::string filePath)
//exit(-1);
}
set_f( info.frames);
set_sr(info.samplerate);
set_nChannels(info.channels);
//
F = info.frames;
sr = info.samplerate;
nChannels = info.channels;
L = info.frames * info.channels;
set_L(info.frames*info.channels);
//
GR = ((double) sr / (double) FS );
initialize();
cout << "GR: " << GR << endl;
cout << "Fs:\t\t" << sr << endl;
cout << "Num Channels:\t" << nChannels << endl;
cout << "Num Samples:\t" << F << endl;
int num = sf_read_double (sf,x,info.frames*info.channels);
// read interleaved data
double* tmp = new double[L];
sf_count_t num = sf_read_double(sf,tmp,L);
cout << "Read Frames:\t" << num << " of " << L << endl;
sf_close(sf);
// allocate memory for deinterleaved channels
x = new double*[nChannels];
for(int i=0; i< nChannels; i++)
{
x[i] = new double[F];
}
// deinterleave into 'separate' arrays for L and R
int i=0;
for(int sampCNT=0; sampCNT<F; sampCNT++)
{
for(int chanCNT=0; chanCNT<nChannels; chanCNT++)
{
x[chanCNT][sampCNT] = tmp[i];
i++;
}
}
}
/**
* \file singlesample.h
* \class SingleSample
*
*
......@@ -12,11 +13,9 @@
*
* \author Henrik von Coler
*
* \version $Revision: 0.5 $
* \version $Revision: 0.52$
*
* \date $Date: 2016/11/22 14:16:20 $
*
* Contact: voncoler@tu-berlin.de
* \date 2016-11-22
*
*
*/
......@@ -27,7 +26,7 @@
#include<iostream>
#include <sndfile.h>
#include <sndfile.hh>
#include <string>
#include <cmath>
......@@ -39,22 +38,28 @@ using std::endl;
class SingleSample
{
public:
SingleSample(std::string filePath);
SingleSample(std::string filePath, int fs);
virtual ~SingleSample();
void read_wavefile(std::string filePath);
int get_L();
double* get_x();
double **get_x();
void set_f(int in);
void set_sr(int in);
void set_nChannels(int in);
int get_nChannels();
void set_L(int in);
void initialize();
double get_sample(double pos);
double get_sample(int pos);
double get_sample(int chan, double pos);
//double get_sample(int chan, int pos);
double get_rate();
void set_rate(double r);
......@@ -64,7 +69,6 @@ public:
void step();
void get_frame(int n, float *a);
//void setResampleRate(double resRate);
int getFS();
......@@ -72,15 +76,20 @@ public:
private:
SNDFILE *sf;
SF_INFO info;
int FS;
double GR;
int num_channels;
int f,sr,nChannels;
int F,sr,nChannels;
int num, L;
double* x;
double** x;
double pos;
double rate;
......
/**
* \file GainExample.cpp
* \class GainExample
*
* \brief Simple example, passing the input to the output
* with gain modification through OSC.
* \file WavExample.cpp
* \class WavExample
*
* \brief Read a wave file and play it back in a loop.
*
* \author Henrik von Coler
*
......@@ -13,40 +13,33 @@
#include"wav_example.h"
using std::cout;
using std::endl;
WavExample::WavExample(std::string filename){
// creating an OSC manager instance
oscman = new OscMan(5111);
this->client = jack_client_open("WAV_Example", JackNullOption, &status, NULL);
// hard coded for now
sample = new SingleSample(filename);
fs = jack_get_sample_rate(client);
cout << "Starting Jack Client!" << endl;
this->client = jack_client_open("WAV_Example", JackNullOption, &status, NULL);
// hard coded for now
sample = new SingleSample(filename, fs);
jack_set_process_callback(this->client, this->callback_process, this);
nChannels = sample->get_nChannels();
// allocate array
input_port = new jack_port_t*[nChannels];
for (int i=0; i<nChannels; i++)
{
std::string tmp_str = "input_" + std::to_string(i+1);
input_port[i] = jack_port_register (client, tmp_str.c_str(),
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsInput, 0);
}
cout << "Starting Jack Client with " << nChannels << " outputs!" << endl;
jack_set_process_callback(this->client, this->callback_process, this);
output_port = new jack_port_t*[nChannels];
for (int i=0; i<nChannels; i++)
for (int i=0; i<nOutputs; i++)
{
std::string tmp_str = "output_" + std::to_string(i+1);
output_port[i] = jack_port_register (client, tmp_str.c_str(),
......@@ -55,29 +48,13 @@ WavExample::WavExample(std::string filename){
}
out = new jack_default_audio_sample_t*;
in = new jack_default_audio_sample_t*;
jack_activate(this->client);
jack_connect(client, jack_port_name(output_port[0]), "system:playback_1");
jack_connect(client, jack_port_name(output_port[1]), "system:playback_2");
jack_connect (client, "system:capture_1", jack_port_name(input_port[0]));
jack_connect (client, "system:capture_2", jack_port_name(input_port[1]));
jack_connect (client, jack_port_name(output_port[0]), "system:playback_1");
jack_connect (client, jack_port_name(output_port[1]), "system:playback_2");
sleep (-1);
sleep(-1);
}
......@@ -85,33 +62,30 @@ WavExample::WavExample(std::string filename){
int WavExample::process (jack_nframes_t nframes)
{
sample->set_rate(oscman->get_speed());
// get buffers
// allocate output buffer array
jack_default_audio_sample_t **out = new jack_default_audio_sample_t*[nChannels];
for ( int i=0 ; i<nChannels; i++)
{
// buffer size can change
out[i] = (jack_default_audio_sample_t *) jack_port_get_buffer(this->output_port[i], jack_get_buffer_size(client));
in[i] = (jack_default_audio_sample_t *) jack_port_get_buffer(this->input_port[i], jack_get_buffer_size(client));
}
sample->set_rate(oscman->get_speed());
// loop over all samples of output buffer
for(int sampCNT=0; sampCNT<nframes; sampCNT++)
{
double tmpPos = sample->get_pos();
for(int chanCNT=0; chanCNT<nChannels; chanCNT++)
{
out[chanCNT][sampCNT] = sample->get_sample(sample->get_pos());
out[chanCNT][sampCNT] = sample->get_sample(chanCNT, tmpPos);
}
sample->step();
}
return 0;
}
......
......@@ -33,19 +33,19 @@ private:
SingleSample *sample;
int nChannels = 2;
int fs = 0;
int nOutputs = 2;
int nChannels;
const char **ports;
jack_client_t *client;
jack_status_t status;
jack_port_t **input_port;
jack_port_t **output_port;
jack_default_audio_sample_t **in, **out;
/// The OSC manager object
OscMan *oscman;
......@@ -54,7 +54,7 @@ private:
/// \param nframes
/// \return
///
int process (jack_nframes_t nframes);
int process(jack_nframes_t nframes);
///
/// \brief callback_process
......
......@@ -2,4 +2,4 @@
#
# called by synth-user.service
#
/home/student/examples/gain_example/gain_example -c /home/student/examples/gain_example/config.yml
/home/student/examples/wav_example/wav_example -f /home/student/wav/ALF_stereo.wav
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment