xiRetimer

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

commit
3ecc0c1fd734e114b42400575607eeffe55027d1
parent
3ca452b1f9504b458a44b2e637a80262c1cd5b9f
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2019-12-26 08:22
Gardening

Diffstat

M src/gui/RetimerPrefsDialog.h 39 +++++++++++++++++++--------------------
M src/playback.cpp 164 ++++++++++++++++++++++++++++++------------------------------

2 files changed, 101 insertions, 102 deletions


diff --git a/src/gui/RetimerPrefsDialog.h b/src/gui/RetimerPrefsDialog.h

@@ -14,28 +14,27 @@ Subclass of PrefsDialog, which is generated by wxFormBuilder.
   14    14 */
   15    15 
   16    16 /** Implementing PrefsDialog */
   17    -1 class RetimerPrefsDialog : public PrefsDialog
   18    -1 {
   -1    17 class RetimerPrefsDialog : public PrefsDialog {
   19    18 protected:
   20    19 	// Handlers for PrefsDialog events.
   21    -1 	void OnIntModeChange( wxCommandEvent& event );
   22    -1 	void OnCheckShowInt( wxCommandEvent& event );
   23    -1 	void OnStretchModeChange( wxCommandEvent& event );
   24    -1 	void OnTempoEnter( wxCommandEvent& event );
   25    -1 	void OnBeatResEnter( wxCommandEvent& event );
   26    -1 	void OnOKClick( wxCommandEvent& event );
   27    -1 	void OnCancelClick( wxCommandEvent& event );
   28    -1 
   29    -1   int oldInterpolationMode;
   30    -1   bool oldShowIntLine;
   31    -1   int oldStretchMode;
   32    -1   int oldTempo;
   33    -1   float oldBeatResolution;
   34    -1 
   35    -1   Marker* marker;
   36    -1   Sample* sample;
   37    -1   Curve* curve;
   38    -1 	
   -1    20 	void OnIntModeChange(wxCommandEvent& event);
   -1    21 	void OnCheckShowInt(wxCommandEvent& event);
   -1    22 	void OnStretchModeChange(wxCommandEvent& event);
   -1    23 	void OnTempoEnter(wxCommandEvent& event);
   -1    24 	void OnBeatResEnter(wxCommandEvent& event);
   -1    25 	void OnOKClick(wxCommandEvent& event);
   -1    26 	void OnCancelClick(wxCommandEvent& event);
   -1    27 
   -1    28 	int oldInterpolationMode;
   -1    29 	bool oldShowIntLine;
   -1    30 	int oldStretchMode;
   -1    31 	int oldTempo;
   -1    32 	float oldBeatResolution;
   -1    33 
   -1    34 	Marker* marker;
   -1    35 	Sample* sample;
   -1    36 	Curve* curve;
   -1    37 
   39    38 public:
   40    39 	/** Constructor */
   41    40 	RetimerPrefsDialog(Marker* m, Sample* s, Curve* c);

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

@@ -1,115 +1,115 @@
    1     1 #include "playback.h"
    2     2 
    3     3 struct sample {
    4    -1     Uint8 *data;
    5    -1     Uint32 dpos;
    6    -1     Uint32 dlen;
   -1     4 	Uint8 *data;
   -1     5 	Uint32 dpos;
   -1     6 	Uint32 dlen;
    7     7 } sounds;
    8     8 
    9    -1   Sample* sample;
   10    -1   float seeker;
   11    -1   float oseeker;
   12    -1   bool _play;
   -1     9 	Sample* sample;
   -1    10 	float seeker;
   -1    11 	float oseeker;
   -1    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;
   19    -1 
   20    -1   extern void callback(void *unused, Uint8 *stream, int len);
   21    -1   SDL_AudioSpec fmt;
   22    -1 
   23    -1   /* Set 16-bit stereo audio at 22Khz */
   24    -1   fmt.freq = 44100/2; // TODO dont know why it takes the double sample-.frequenzy here // TODO link it to sample->sfinfo.samplerate
   25    -1   fmt.format = AUDIO_S16;
   26    -1   fmt.channels = 1;
   27    -1   fmt.samples = 1024;
   28    -1   fmt.callback = callback;
   29    -1   fmt.userdata = NULL;
   30    -1 
   31    -1   /* Open the audio device and start playing sound! */
   32    -1   if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
   33    -1     fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
   34    -1     exit(1);
   35    -1   }
   36    -1   SDL_PauseAudio(0);
   -1    15 	sample=s;
   -1    16 	_play=false;
   -1    17 	seeker=0;
   -1    18 	oseeker=0;
   -1    19 
   -1    20 	extern void callback(void *unused, Uint8 *stream, int len);
   -1    21 	SDL_AudioSpec fmt;
   -1    22 
   -1    23 	/* Set 16-bit stereo audio at 22Khz */
   -1    24 	fmt.freq = 44100/2; // TODO dont know why it takes the double sample-.frequenzy here // TODO link it to sample->sfinfo.samplerate
   -1    25 	fmt.format = AUDIO_S16;
   -1    26 	fmt.channels = 1;
   -1    27 	fmt.samples = 1024;
   -1    28 	fmt.callback = callback;
   -1    29 	fmt.userdata = NULL;
   -1    30 
   -1    31 	/* Open the audio device and start playing sound! */
   -1    32 	if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
   -1    33 		fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
   -1    34 		exit(1);
   -1    35 	}
   -1    36 	SDL_PauseAudio(0);
   37    37 }
   38    38 
   39    39 Playback::~Playback() {
   40    -1   SDL_CloseAudio();
   -1    40 	SDL_CloseAudio();
   41    41 }
   42    42 
   43    43 int Playback::play() {
   44    -1   if (_play) {
   45    -1     stop();
   46    -1     _play=false;
   47    -1     return 0;
   48    -1   }
   49    -1   else {
   50    -1     int err=start();
   51    -1     if (err==0) _play=true;
   52    -1     return err;
   53    -1   }
   -1    44 	if (_play) {
   -1    45 		stop();
   -1    46 		_play=false;
   -1    47 		return 0;
   -1    48 	}
   -1    49 	else {
   -1    50 		int err=start();
   -1    51 		if (err==0) _play=true;
   -1    52 		return err;
   -1    53 	}
   54    54 }
   55    55 
   56    56 int Playback::start() {
   57    -1   SDL_LockAudio();
   -1    57 	SDL_LockAudio();
   58    58 
   59    -1     int length=sample->getLength();
   60    -1     Uint8 idata[length];
   61    -1     for (int i=0; i<length; ++i) {
   62    -1       idata[i]=int(sample->get(i/(float)length)*128);
   63    -1     }
   64    -1     /* Put the sound data in the slot (it starts playing immediately) */
   65    -1     sounds.dlen = length;
   66    -1     sounds.data = idata;
   67    -1     sounds.dpos = int(seeker*length);
   -1    59 	int length=sample->getLength();
   -1    60 	Uint8 idata[length];
   -1    61 	for (int i=0; i<length; ++i) {
   -1    62 		idata[i]=int(sample->get(i/(float)length)*128);
   -1    63 	}
   -1    64 	/* Put the sound data in the slot (it starts playing immediately) */
   -1    65 	sounds.dlen = length;
   -1    66 	sounds.data = idata;
   -1    67 	sounds.dpos = int(seeker*length);
   68    68 
   69    -1   SDL_UnlockAudio();
   -1    69 	SDL_UnlockAudio();
   70    70 
   71    -1     return 0;
   -1    71 	return 0;
   72    72 }
   73    73 
   74    74 void callback(void *udata, Uint8 *stream, int len) {
   75    -1   if (_play) {
   76    -1     if (sounds.dpos==sounds.dlen) {
   77    -1       _play=false;
   78    -1       seeker=float(oseeker);
   79    -1       return;
   80    -1     }
   81    -1     Uint32 amount;
   82    -1 
   83    -1     amount = (sounds.dlen-sounds.dpos);
   84    -1     if ( amount > len ) {
   85    -1       amount = len;
   86    -1     }
   87    -1     SDL_MixAudio(stream, &sounds.data[sounds.dpos], amount, SDL_MIX_MAXVOLUME);
   88    -1     sounds.dpos += amount;
   89    -1 
   90    -1     seeker=sounds.dpos/(float)sounds.dlen;
   91    -1   }
   -1    75 	if (_play) {
   -1    76 		if (sounds.dpos==sounds.dlen) {
   -1    77 			_play=false;
   -1    78 			seeker=float(oseeker);
   -1    79 			return;
   -1    80 		}
   -1    81 		Uint32 amount;
   -1    82 
   -1    83 		amount = (sounds.dlen-sounds.dpos);
   -1    84 		if ( amount > len ) {
   -1    85 			amount = len;
   -1    86 		}
   -1    87 		SDL_MixAudio(stream, &sounds.data[sounds.dpos], amount, SDL_MIX_MAXVOLUME);
   -1    88 		sounds.dpos += amount;
   -1    89 
   -1    90 		seeker=sounds.dpos/(float)sounds.dlen;
   -1    91 	}
   92    92 }
   93    93 
   94    94 void Playback::stop() {
   95    -1   seeker=sounds.dpos/(float)sounds.dlen;
   96    -1   oseeker=float(seeker);
   97    -1   SDL_LockAudio();
   98    -1   sounds.dpos=sounds.dlen;
   99    -1   SDL_UnlockAudio();
   -1    95 	seeker=sounds.dpos/(float)sounds.dlen;
   -1    96 	oseeker=float(seeker);
   -1    97 	SDL_LockAudio();
   -1    98 	sounds.dpos=sounds.dlen;
   -1    99 	SDL_UnlockAudio();
  100   100 }
  101   101 
  102   102 void Playback::setSeeker(float nn) {
  103    -1   seeker=nn;
  104    -1   oseeker=float(seeker);
  105    -1   if (_play) {
  106    -1     SDL_LockAudio();
  107    -1     sounds.dpos = int(seeker*sounds.dlen);
  108    -1     SDL_UnlockAudio();
  109    -1   }
   -1   103 	seeker=nn;
   -1   104 	oseeker=float(seeker);
   -1   105 	if (_play) {
   -1   106 		SDL_LockAudio();
   -1   107 		sounds.dpos = int(seeker*sounds.dlen);
   -1   108 		SDL_UnlockAudio();
   -1   109 	}
  110   110 }
  111   111 
  112   112 float Playback::getSeeker() {
  113    -1   return seeker;
   -1   113 	return seeker;
  114   114 }
  115   115