Exemple #1
0
static void playSound (ExperimentMFC me, Sound sound, Sound carrierBefore, Sound carrierAfter,
	double initialSilenceDuration, double finalSilenceDuration)
{
	long numberOfSamplesWritten = 0;

	long initialSilenceSamples = lround (initialSilenceDuration / my samplePeriod);
	for (long channel = 1; channel <= my numberOfChannels; channel ++) {
		for (long i = 1; i <= initialSilenceSamples; i ++) {
			my playBuffer -> z [channel] [i] = 0.0;
		}
	}
	numberOfSamplesWritten += initialSilenceSamples;

	if (carrierBefore) {
		for (long channel = 1; channel <= my numberOfChannels; channel ++) {
			NUMvector_copyElements <double> (carrierBefore -> z [channel],
				my playBuffer -> z [channel] + numberOfSamplesWritten, 1, carrierBefore -> nx);
		}
		numberOfSamplesWritten += carrierBefore -> nx;
	}

	if (sound) {
		for (long channel = 1; channel <= my numberOfChannels; channel ++) {
			NUMvector_copyElements <double> (sound -> z [channel],
				my playBuffer -> z [channel] + numberOfSamplesWritten, 1, sound -> nx);
		}
		numberOfSamplesWritten += sound -> nx;
	}

	if (carrierAfter) {
		for (long channel = 1; channel <= my numberOfChannels; channel ++) {
			NUMvector_copyElements <double> (carrierAfter -> z [channel],
				my playBuffer -> z [channel] + numberOfSamplesWritten, 1, carrierAfter -> nx);
		}
		numberOfSamplesWritten += carrierAfter -> nx;
	}

	long finalSilenceSamples = lround (finalSilenceDuration / my samplePeriod);
	for (long channel = 1; channel <= my numberOfChannels; channel ++) {
		for (long i = 1; i <= finalSilenceSamples; i ++) {
			my playBuffer -> z [channel] [i + numberOfSamplesWritten] = 0.0;
		}
	}
	numberOfSamplesWritten += finalSilenceSamples;

	if (! my blankWhilePlaying)
		my startingTime = Melder_clock ();
	Sound_playPart (my playBuffer.get(), 0.0, numberOfSamplesWritten * my samplePeriod, 0, nullptr);
	if (my blankWhilePlaying)
		my startingTime = Melder_clock ();
}
void Sound_playPart (Sound me, double tmin, double tmax,
	int (*callback) (void *closure, int phase, double tmin, double tmax, double t), void *closure)
{
	try {
		long ifsamp = floor (1.0 / my dx + 0.5), bestSampleRate = MelderAudio_getOutputBestSampleRate (ifsamp);
		if (ifsamp == bestSampleRate) {
			struct SoundPlay *thee = (struct SoundPlay *) & thePlayingSound;
			double *fromLeft = my z [1], *fromRight = my ny > 1 ? my z [2] : NULL;
			MelderAudio_stopPlaying (MelderAudio_IMPLICIT);
			long i1, i2;
			if ((thy numberOfSamples = Matrix_getWindowSamplesX (me, tmin, tmax, & i1, & i2)) < 1) return;
			thy tmin = tmin;
			thy tmax = tmax;
			thy dt = my dx;
			thy t1 = my x1;
			thy callback = callback;
			thy closure = closure;
			thy silenceBefore = (long) (ifsamp * MelderAudio_getOutputSilenceBefore ());
			thy silenceAfter = (long) (ifsamp * MelderAudio_getOutputSilenceAfter ());
			int numberOfChannels = my ny > 1 ? 2 : 1;
			NUMvector_free (thy buffer, 1);   // just in case
			thy buffer = NUMvector <short> (1, (i2 - i1 + 1 + thy silenceBefore + thy silenceAfter) * numberOfChannels);
			thy i1 = i1;
			thy i2 = i2;
			short *to = thy buffer + thy silenceBefore * numberOfChannels;
			if (numberOfChannels == 2) {
				for (long i = i1; i <= i2; i ++) {
					long valueLeft = (long) floor (fromLeft [i] * 32768.0 + 0.5);
					* ++ to = valueLeft < -32768 ? -32768 : valueLeft > 32767 ? 32767 : valueLeft;
					long valueRight = (long) floor (fromRight [i] * 32768.0 + 0.5);
					* ++ to = valueRight < -32768 ? -32768 : valueRight > 32767 ? 32767 : valueRight;
				}
			} else {
				for (long i = i1; i <= i2; i ++) {
					long value = (long) floor (fromLeft [i] * 32768.0 + 0.5);
					* ++ to = value < -32768 ? -32768 : value > 32767 ? 32767 : value;
				}
			}
			if (thy callback) thy callback (thy closure, 1, tmin, tmax, tmin);
			MelderAudio_play16 (thy buffer + 1, ifsamp,
				thy silenceBefore + thy numberOfSamples + thy silenceAfter, numberOfChannels, melderPlayCallback, thee);
		} else {
			autoSound resampled = Sound_resample (me, bestSampleRate, 1);
			Sound_playPart (resampled.peek(), tmin, tmax, callback, closure);   // recursively
		}
	} catch (MelderError) {
		Melder_throw (me, ": not played.");
	}
}
void FormantGrid_playPart (FormantGrid me, double tmin, double tmax, double samplingFrequency,
	double tStart, double f0Start, double tMid, double f0Mid, double tEnd, double f0End,
	double adaptFactor, double maximumPeriod, double openPhase, double collisionPhase, double power1, double power2,
	int (*playCallback) (void *playClosure, int phase, double tmin, double tmax, double t), void *playClosure)
{
	try {
		autoSound sound = FormantGrid_to_Sound (me, samplingFrequency,
			tStart, f0Start, tMid, f0Mid, tEnd, f0End,
			adaptFactor, maximumPeriod, openPhase, collisionPhase, power1, power2);
		Vector_scale (sound.peek(), 0.99);
		Sound_playPart (sound.peek(), tmin, tmax, playCallback, playClosure);
	} catch (MelderError) {
		Melder_throw (me, ": not played.");
	}
}
Exemple #4
0
int FormantGrid_playPart (FormantGrid me, double tmin, double tmax, double samplingFrequency,
	double tStart, double f0Start, double tMid, double f0Mid, double tEnd, double f0End,
	double adaptFactor, double maximumPeriod, double openPhase, double collisionPhase, double power1, double power2,
	int (*playCallback) (void *playClosure, int phase, double tmin, double tmax, double t), void *playClosure)
{
	Sound sound = FormantGrid_to_Sound (me, samplingFrequency,
		tStart, f0Start, tMid, f0Mid, tEnd, f0End,
		adaptFactor, maximumPeriod, openPhase, collisionPhase, power1, power2); cherror
	Vector_scale (sound, 0.99);
	Sound_playPart (sound, tmin, tmax, playCallback, playClosure);
end:
	forget (sound);
	iferror return 0;
	return 1;
}
Exemple #5
0
void structSoundEditor :: v_play (double a_tmin, double a_tmax) {
	if (d_longSound.data)
		LongSound_playPart ((LongSound) data, a_tmin, a_tmax, theFunctionEditor_playCallback, this);
	else
		Sound_playPart ((Sound) data, a_tmin, a_tmax, theFunctionEditor_playCallback, this);
}
Exemple #6
0
void Movie_play (Movie me, Graphics /* g */, double tmin, double tmax, Sound_PlayCallback callback, Thing boss)
{
	Sound_playPart (my d_sound, tmin, tmax, callback, boss);
}
void SpeechSynthesizer_playText (SpeechSynthesizer me, const char32 *text) {
	autoSound thee = SpeechSynthesizer_to_Sound (me, text, nullptr, nullptr);
	Sound_playPart (thee.get(), thy xmin, thy xmax, nullptr, nullptr);
}
Exemple #8
0
void SoundEditor::play (double tmin, double tmax) {
	if (_longSound.data)
		LongSound_playPart ((LongSound) _data, tmin, tmax, playCallback, this);
	else
		Sound_playPart ((Sound) _data, tmin, tmax, playCallback, this);
}
void SpeechSynthesizer_playText (SpeechSynthesizer me, const char32 *text) {
	autoSound thee= SpeechSynthesizer_to_Sound (me, text, 0, 0);
	Sound_playPart (thee.peek(), thy xmin, thy xmax, 0, 0);
}
Exemple #10
0
void Sound_play (Sound me,
	int (*playCallback) (void *playClosure, int phase, double tmin, double tmax, double t), void *playClosure)
{
	Sound_playPart (me, my xmin, my xmax, playCallback, playClosure);
}
Exemple #11
0
void Movie_play (Movie me, Graphics g, double tmin, double tmax, int (*callback) (void *closure, int phase, double tmin, double tmax, double t), void *closure)
{
	(void) g;
	Sound_playPart (my d_sound, tmin, tmax, callback, closure);
}