/* Pitch_to_Sound.c */
/* Paul Boersma, March 8, 2002 */

#include "Pitch_to_PointProcess.h"
#include "PointProcess_and_Sound.h"
#include "Pitch_to_Sound.h"

Sound Pitch_to_Sound (I, double tmin, double tmax, int hum) {
	iam (Pitch);
	static float formant [1 + 6] =
		{ 0, 600, 1400, 2400, 3400, 4500, 5500 };
	static float bandwidth [1 + 6] =
		{ 0, 50, 100, 200, 300, 400, 500 };
	PointProcess point = NULL;
	Sound sound = NULL;
	if (! (point = Pitch_to_PointProcess (me))) goto error;
	if (! (sound = PointProcess_to_Sound (point, 22050, 0.7, 0.05, 30))) goto error;
	if (hum && ! Sound_filterWithFormants (sound, tmin, tmax, 6, formant, bandwidth)) goto error;
	forget (point);
	return sound;
error:
	forget (point);
	forget (sound);
	return Melder_errorp ("Pitch_to_Sound: not performed.");
}

int Pitch_play (I, double tmin, double tmax) {
	iam (Pitch);
	Sound sound = Pitch_to_Sound (me, tmin, tmax, FALSE);
	if (! sound) return Melder_error ("Pitch_play: not played.");
	if (tmax <= tmin) { tmin = my xmin; tmax = my xmax; }   /* Autowindowing. */
	Sound_playPart (sound, tmin, tmax, NULL, NULL);
	forget (sound);
	return 1;
}

int Pitch_hum (I, double tmin, double tmax) {
	iam (Pitch);
	Sound sound = Pitch_to_Sound (me, tmin, tmax, TRUE);
	if (! sound) return Melder_error ("Pitch_hum: not played.");
	if (tmax <= tmin) { tmin = my xmin; tmax = my xmax; }   /* Autowindowing. */
	Sound_playPart (sound, tmin, tmax, NULL, NULL);
	forget (sound);
	return 1;
}

/* End of file Pitch_to_Sound.c */
