xiRetimer

fit recorded audio to a tempo
git clone https://git.ce9e.org/xiRetimer.git

commit
8645579caaa9281e4dfd5fb7f19c59dac34c4fc9
parent
41e0a3f00ccd6bf07de012b4d70a73eef309895a
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2019-12-26 09:46
Gardening

Diffstat

M src/playback.cpp 47 +++++++++++++++++++++++------------------------

1 files changed, 23 insertions, 24 deletions


diff --git a/src/playback.cpp b/src/playback.cpp

@@ -12,10 +12,10 @@ struct sample {
   12    12 	bool _play;
   13    13 
   14    14 Playback::Playback(Sample* s) {
   15    -1 	sample=s;
   16    -1 	_play=false;
   17    -1 	seeker=0;
   18    -1 	oseeker=0;
   -1    15 	sample = s;
   -1    16 	_play = false;
   -1    17 	seeker = 0;
   -1    18 	oseeker = 0;
   19    19 
   20    20 	extern void callback(void *unused, Uint8 *stream, int len);
   21    21 	SDL_AudioSpec fmt;
@@ -29,7 +29,7 @@ Playback::Playback(Sample* s) {
   29    29 	fmt.userdata = NULL;
   30    30 
   31    31 	/* Open the audio device and start playing sound! */
   32    -1 	if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
   -1    32 	if (SDL_OpenAudio(&fmt, NULL) < 0) {
   33    33 		fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
   34    34 		exit(1);
   35    35 	}
@@ -47,8 +47,8 @@ int Playback::play() {
   47    47 		return 0;
   48    48 	}
   49    49 	else {
   50    -1 		int err=start();
   51    -1 		if (err==0) _play=true;
   -1    50 		int err = start();
   -1    51 		if (err == 0) _play=true;
   52    52 		return err;
   53    53 	}
   54    54 }
@@ -56,15 +56,15 @@ int Playback::play() {
   56    56 int Playback::start() {
   57    57 	SDL_LockAudio();
   58    58 
   59    -1 	int length=sample->getLength();
   -1    59 	int length = sample->getLength();
   60    60 	Uint8 idata[length];
   61    -1 	for (int i=0; i<length; ++i) {
   62    -1 		idata[i]=int(sample->get(i/(float)length)*128);
   -1    61 	for (int i = 0; i < length; ++i) {
   -1    62 		idata[i] = int(sample->get(i / (float)length) * 128);
   63    63 	}
   64    64 	/* Put the sound data in the slot (it starts playing immediately) */
   65    65 	sounds.dlen = length;
   66    66 	sounds.data = idata;
   67    -1 	sounds.dpos = int(seeker*length);
   -1    67 	sounds.dpos = int(seeker * length);
   68    68 
   69    69 	SDL_UnlockAudio();
   70    70 
@@ -73,38 +73,38 @@ int Playback::start() {
   73    73 
   74    74 void callback(void *udata, Uint8 *stream, int len) {
   75    75 	if (_play) {
   76    -1 		if (sounds.dpos==sounds.dlen) {
   77    -1 			_play=false;
   78    -1 			seeker=float(oseeker);
   -1    76 		if (sounds.dpos == sounds.dlen) {
   -1    77 			_play = false;
   -1    78 			seeker = float(oseeker);
   79    79 			return;
   80    80 		}
   81    81 		Uint32 amount;
   82    82 
   83    -1 		amount = (sounds.dlen-sounds.dpos);
   84    -1 		if ( amount > len ) {
   -1    83 		amount = sounds.dlen - sounds.dpos;
   -1    84 		if (amount > len) {
   85    85 			amount = len;
   86    86 		}
   87    87 		SDL_MixAudio(stream, &sounds.data[sounds.dpos], amount, SDL_MIX_MAXVOLUME);
   88    88 		sounds.dpos += amount;
   89    89 
   90    -1 		seeker=sounds.dpos/(float)sounds.dlen;
   -1    90 		seeker = sounds.dpos / (float)sounds.dlen;
   91    91 	}
   92    92 }
   93    93 
   94    94 void Playback::stop() {
   95    -1 	seeker=sounds.dpos/(float)sounds.dlen;
   96    -1 	oseeker=float(seeker);
   -1    95 	seeker = sounds.dpos / (float)sounds.dlen;
   -1    96 	oseeker = float(seeker);
   97    97 	SDL_LockAudio();
   98    -1 	sounds.dpos=sounds.dlen;
   -1    98 	sounds.dpos = sounds.dlen;
   99    99 	SDL_UnlockAudio();
  100   100 }
  101   101 
  102   102 void Playback::setSeeker(float nn) {
  103    -1 	seeker=nn;
  104    -1 	oseeker=float(seeker);
   -1   103 	seeker = nn;
   -1   104 	oseeker = float(seeker);
  105   105 	if (_play) {
  106   106 		SDL_LockAudio();
  107    -1 		sounds.dpos = int(seeker*sounds.dlen);
   -1   107 		sounds.dpos = int(seeker * sounds.dlen);
  108   108 		SDL_UnlockAudio();
  109   109 	}
  110   110 }
@@ -112,4 +112,3 @@ void Playback::setSeeker(float nn) {
  112   112 float Playback::getSeeker() {
  113   113 	return seeker;
  114   114 }
  115    -1