Ejemplo n.º 1
0
// Jack | Process Callback
int process(jack_nframes_t nframes, void *arg) {
	sample_t * in = (sample_t *) jack_port_get_buffer(input_port, nframes);
	sample_t * out = (sample_t *) jack_port_get_buffer(output_port, nframes);
	if (aubio_fvec == NULL || aubio_fvec->data == NULL) {
		init_aubio(sample_rate);
	}
	// 
	jack_nframes_t i;
	for (i = 0; i < nframes; i++) {
		// ???: What is the significance of `framecount == hopsize`? 
		if (framecount == hopsize) {
			image_tones_index++;
			if (image_tones_index >= image_tones_size) {
				image_tones_index = 0;
			}
			// Build waveform for the next pixel of our original image
			// TODO: Consider a "feedback" mode.
			build_tone(image_tones[image_tones_index], 1 - image_tones_amp[image_tones_index], waveform_type);
			// Write_to_image() according to analyzed samples 
			// TODO: How often are the vars below redeclared?
			float H, S, L, R, G, B;
			Sound2Hsl(&H, &S, &L, aubio_pitchdetection(aubio, aubio_fvec), max_amp, pitch_scale, lower_bounds);
			Hsl2Rgb(&R, &G, &B, H, S, 1 - L);
			write_to_image(dest_image, R, G, B);
			framecount = 0;
			max_amp = 0;
		}
		out[i] = cycle[offset];
		// !!!:
		aubio_fvec->data[0][framecount] = (smpl_t) in[i];
		if (fabs(in[i]) > max_amp) {
			max_amp = fabs(in[i]);
		}
		offset++;
		if (offset == samples_per_cycle) {
			offset = 0;
		}
		framecount++;
	}
	return 0;      
}
Ejemplo n.º 2
0
// Main
int main(int argc, char * argv[]) {
	// User supplied correct vals? Otherwise show usage...
	// TODO: Allow a minimum of <image path> to be provided and default
	// 	 the rest.
	if (argc < 8) {
		fprintf(stderr, "usage: sonify <client name> <image path> <freq scale> <lowest freq> <sin | sq | tri | saw> <window scale>\ni.e. sonify sfy img.png 10000 1000 1 sin\n");
		exit(1);
	}
	jack_client_t * client;
	const char ** ports;
	char file_name[100];
	int window_scale;
	init_vars(argv, file_name, &window_scale);

	// Init Jack Client
	if ((client = jack_client_open(argv[1], JackNullOption, NULL)) == 0) {
		fprintf (stderr, "Jack server not running?\n");
		return 1;
	}
	jack_set_process_callback(client, process, 0);
	jack_set_sample_rate_callback(client, srate, 0);
	input_port = jack_port_register(client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
	output_port = jack_port_register(client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);

	// Get Sample Rate & Init Aubio
	sample_rate = jack_get_sample_rate(client);
	init_aubio(sample_rate);
	
	// Init SDL Surfaces
	source_image = IMG_Load(file_name);
	if (source_image == NULL) {
		fprintf(stderr, "Load failes: %s\n", IMG_GetError());
		exit(1);
	}
	//   Generate Tones From Pixels
	generate_tone_array(source_image);
	dest_image = SDL_CreateRGBSurface (SDL_SWSURFACE, source_image->w, source_image->h, 32, 0, 0, 0, 0);
	if(dest_image == NULL) {
		fprintf(stderr, "CreateRGBSurface failed: %s\n", SDL_GetError());
		exit(1);
    	}
	SDL_FreeSurface(source_image);

	// Build Tone
	build_tone(image_tones[image_tones_index], image_tones_amp[image_tones_index], waveform_type);

	// Activate Jack Client
	if (jack_activate(client)) {
		fprintf(stderr, "cannot activate client\n");
		return 1;
	}

	// Init SDL Window
	if (SDL_Init(SDL_INIT_VIDEO) != 0) {
		fprintf(stderr, "Init failed: %s\n", SDL_GetError());
		exit(1);
	}
	SDL_Surface * display;
	display = SDL_SetVideoMode(dest_image->w * window_scale, dest_image->h * window_scale, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
	if (display == NULL) { 
		fprintf(stderr, "SetVideoMode failed: %s\n", SDL_GetError()); 
		exit(1);
	}
	SDL_WM_SetCaption("Sonify", "Sonify");
	SDL_Event event;

	// GUI Loop
	// TODO: Implement a fullscreen mode that can be toggled with a keypress.
	// TODO: The larger the window_scale, the slower SDL_ResizeFactor runs.
	//       Consider writing a function that only redraws a particular portion
	//       of dub_image instead. This will improve performance in fullscreen
	//       mode down the line. 
	// TODO: Can we introduce some simple menu items for changing the transcoding
	//       algorithm on the fly?
	while(1) {
		if (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT) {
				break;
			}
		}
		// TODO: Optimize this...
		dub_image = SDL_DisplayFormat(dest_image);
		if (SDL_BlitSurface(SDL_ResizeFactor(dub_image, window_scale, 1), NULL, display, NULL) != 0) {
			fprintf(stderr, "SDL_BlitSurface() Failed.");
			exit(1);
		}
		SDL_Flip(display);
	}

	// Cleanup
	jack_client_close(client);
	del_aubio_pitchdetection(aubio);
	SDL_FreeSurface(display);
	SDL_FreeSurface(dub_image);
	SDL_FreeSurface(dest_image);
	SDL_Quit();
	free(cycle);
	free(image_tones);
	free(image_tones_amp);
	exit(0);
}
Ejemplo n.º 3
0
int tone_zone_register_zone(int fd, struct tone_zone *z)
{
	char buf[MAX_SIZE];
	int res;
	int count = 0;
	int x;
	size_t space = MAX_SIZE;
	void *ptr = buf;
	int iopenedit = 1;
	struct dahdi_tone_def_header *h;

	memset(buf, 0, sizeof(buf));

	h = ptr;
	ptr += sizeof(*h);
	space -= sizeof(*h);
	h->zone = z->zone;

	dahdi_copy_string(h->name, z->description, sizeof(h->name));

	for (x = 0; x < DAHDI_MAX_CADENCE; x++) 
		h->ringcadence[x] = z->ringcadence[x];

	for (x = 0; x < DAHDI_TONE_MAX; x++) {
		if (!strlen(z->tones[x].data))
			continue;

		PRINT_DEBUG("Tone: %d, string: %s\n", z->tones[x].toneid, z->tones[x].data);

		if ((res = build_tone(ptr, space, &z->tones[x], &count)) < 0) {
			fprintf(stderr, "Tone %d not built.\n", x);
			return -1;
		}
		ptr += res;
		space -= res;
	}

	if ((res = build_mf_tones(ptr, space, &count, dtmf_tones, z->dtmf_low_level, z->dtmf_high_level)) < 0) {
		fprintf(stderr, "Could not build DTMF tones.\n");
		return -1;
	}
	ptr += res;
	space -= res;

	if ((res = build_mf_tones(ptr, space, &count, mfr1_tones, z->mfr1_level, z->mfr1_level)) < 0) {
		fprintf(stderr, "Could not build MFR1 tones.\n");
		return -1;
	}
	ptr += res;
	space -= res;

	if ((res = build_mf_tones(ptr, space, &count, mfr2_fwd_tones, z->mfr2_level, z->mfr2_level)) < 0) {
		fprintf(stderr, "Could not build MFR2 FWD tones.\n");
		return -1;
	}
	ptr += res;
	space -= res;

	if ((res = build_mf_tones(ptr, space, &count, mfr2_rev_tones, z->mfr2_level, z->mfr2_level)) < 0) {
		fprintf(stderr, "Could not build MFR2 REV tones.\n");
		return -1;
	}
	ptr += res;
	space -= res;

	h->count = count;

	if (fd < 0) {
		if ((fd = open(DEFAULT_DAHDI_DEV, O_RDWR)) < 0) {
			fprintf(stderr, "Unable to open %s and fd not provided\n", DEFAULT_DAHDI_DEV);
			return -1;
		}
		iopenedit = 1;
	}

	x = z->zone;
	if ((res = ioctl(fd, DAHDI_FREEZONE, &x))) {
		if (errno != EBUSY)
			fprintf(stderr, "ioctl(DAHDI_FREEZONE) failed: %s\n", strerror(errno));
		return res;
	}

#if defined(TONEZONE_DRIVER)
	dump_tone_zone(h, MAX_SIZE - space);
#endif

#if defined(__FreeBSD__)
	if ((res = ioctl(fd, DAHDI_LOADZONE, &h))) {
#else
	if ((res = ioctl(fd, DAHDI_LOADZONE, h))) {
#endif
		fprintf(stderr, "ioctl(DAHDI_LOADZONE) failed: %s\n", strerror(errno));
		return res;
	}

	if (iopenedit)
		close(fd);

	return res;
}

int tone_zone_register(int fd, char *country)
{
	struct tone_zone *z;
	z = tone_zone_find(country);
	if (z) {
		return tone_zone_register_zone(-1, z);
	} else {
		return -1;
	}
}

int tone_zone_set_zone(int fd, char *country)
{
	int res=-1;
	struct tone_zone *z;
	if (fd > -1) {
		z = tone_zone_find(country);
		if (z)
			res = ioctl(fd, DAHDI_SETTONEZONE, &z->zone);
		if ((res < 0) && (errno == ENODATA)) {
			tone_zone_register_zone(fd, z);
			res = ioctl(fd, DAHDI_SETTONEZONE, &z->zone);
		}
	}
	return res;
}