Audio-C4Stream-Wav

 view release on metacpan or  search on metacpan

lib/Audio/C4Stream/Wav.pm  view on Meta::CPAN

			$blankDetectAmpl = 36767;
		}

		if ($blankDetectAmpl) {
			my $blankSize = WAV_left_trim_size( $wavReader, $blankDetectAmpl );
			if ($blankSize) {
				WAV_set_left_trim_size( $wavReader, $blankSize );
				$this->{_leftTrimSize} = $blankSize;
			}

			$blankSize = WAV_right_trim_size( $wavReader, $blankDetectAmpl );
			if ($blankSize) {
				WAV_set_right_trim_size( $wavReader, $blankSize );
				$this->{_rightTrimSize} = $blankSize;
			}
		}
	}

	if ( $this->{normalize} ) {
		my $highestAmpl   = WAV_highest_ampl($wavReader);
		my $normalizeAmpl = &_dbToAmpl( $this->{normalize} );

		WAV_set_ampl_ratio( $wavReader, $normalizeAmpl / $highestAmpl );
	}
	
	$this->{_wavReader} = $wavReader;

	return $this;
}

sub _log10 {
	return log(shift) / log(10);
}

sub _dbToAmpl {
	my $db = shift;

	return MAX_AMPL * exp( $db / 20 * log(10) );
}

sub _amplToDb {
	my $ampl = shift;

	return 20 * &_log10( $ampl / MAX_AMPL );
}

sub getPngData {
	my $this = shift;

	my $read = $this->{_read};

	my $wavDraw = WAV_init_draw(
		$this->{_wavReader},
		{
			data_size => $read->length,
			data_len  => $read->length_seconds,
			font      => 'fonts/ arialbd . ttf '
		}
	);

	my $png;
	WAV_draw( $wavDraw, 634, 77 * 2 + 2 + 6, $png );

	WAV_final_draw($wavDraw);

	return $png;
}

sub getNextRawData {
	my $this = shift;

	my $data;
	WAV_read_block( $this->{_wavReader}, $data );

	return $data;
}

sub getOrigDataLen {
	return WAV_get_length( shift->{_wavReader} );
}

sub getOrigSec {
	return WAV_get_length_seconds( shift->{_wavReader} );
}

sub getDataLen {
	return WAV_data_size( shift->{_wavReader} );
}

sub getSec {
	my $this = shift;

	return WAV_get_length_seconds( $this->{_wavReader} ) - $this->{leftTrimLen} - $this->{rightTrimLen};
}

sub DESTROY {
	my $this = shift;

	WAV_final_reader( $this->{_wavReader} );
}

1;

__DATA__
=head1 NAME

Audio::C4Stream::Wav - Perl extension for open and stream WAV files.

=head1 SYNOPSIS

  use Audio::C4Stream::Wav;
  
  my $audio = new Audio::C4Stream::Wav(
		filename            => $file,
		leftTrimLen         => 5,5, #in seconds
		rightTrimLen        => 5,5, #seconds
		fadeInLen           => 10, #seconds
		fadeOutLen          => 10, #seconds
		leftBlankDetectLen  => 0, #seconds (default 20)
		rightBlankDetectLen => 0, #seconds (default 20)
		blankDetectDb       => 30, #decibels
		normalize           => 0, #decibels
  );
  

=head1 DESCRIPTION

lib/Audio/C4Stream/Wav.pm  view on Meta::CPAN


struct wav_draw_s *wav_init_draw (struct wav_reader_s *reader) {
	struct wav_draw_s *draw;
	char *ptr;
	SV **svp;
	
	if ((draw = (struct wav_draw_s *) malloc (sizeof (struct wav_draw_s))) == NULL) {
		fprintf(stderr, "Memory allocation error\\n");
		return NULL;
	}
	memset (draw, 0, sizeof (struct wav_draw_s));
	
	draw->reader = reader;

	return draw;
}

unsigned int WAV_init_draw (int reader_ptr, HV *hv) {
	struct wav_reader_s *reader = (struct wav_reader_s *) reader_ptr;
	struct wav_draw_s *draw;
	char *ptr;
	SV **svp;
	
	if ((draw = wav_init_draw(reader)) == NULL) {
		croak("Memory allocation error");
		return 0;
	}
	
	svp = hv_fetch(hv, "font", strlen ("font"), 0);
	if (svp && (ptr = SvPV_nolen(*svp)) != NULL)
		draw->font = ptr;

	if (draw->font == NULL) {
		croak("No font");
	}
	
	return (unsigned int) draw;
}

void wav_final_draw (struct wav_draw_s *draw) {
	free (draw);
}

void WAV_final_draw (int draw_ptr) {
	free ((struct wav_draw_s *) draw_ptr);
}

char *wav_draw (struct wav_draw_s *draw, int width, int height, int *size) {
	struct wav_reader_s *reader;
	struct wav_channels_s *chptr;
	gdImagePtr im;
	int styleDotted[4];
  	int white, blue, orange, color, hblue, lblue, green, second, thrid;
  	int i, r, offset, data_size;
  	int xval, yval1, yval2, ymin1, ymin2, ymax1, ymax2;
  	int xtmp = 0, ytmp = 0, lx, ly1 = 0, ly2 = 0;
  	int draw1, draw2, pos;
  	int limit, mlimit1, mlimit2;
  	double step, curr;
  	char *font = "fonts/db.ttf";
  	char *png_ptr, *err;
  	int brect[8];
	
	if ((im = gdImageCreate(width, height)) == NULL) {
		fprintf(stderr, "gdImageCreate failed");
		return NULL;
	}
	
	white = gdImageColorAllocate(im, 255, 255, 255 );
	gdImageRectangle(im, 0, 0, width, height, white);
	
	hblue = gdImageColorAllocate(im, 90, 117, 112 );	//#5A7570
	green = gdImageColorAllocate(im, 47, 61, 58 );  	//#2f3d3a
	lblue = gdImageColorAllocate(im, 156, 176, 173 );	//#9CB0AD
	orange = gdImageColorAllocate(im, 223, 223, 223 ); 	//#DFDFDF
	second = gdImageColorAllocate(im, 86, 108, 105 ); 	//#DFDFDF
	thrid = gdImageColorAllocate(im, 110, 140, 135 ); 	//#DFDFDF
	
	blue = gdImageColorAllocate(im, 99, 126, 122 );		//008CEF
	
	gdImageFilledRectangle(im, 0, 0, width - 1, height - 1, orange);
	
	gdImageFilledRectangle(im, 0, 0, width - 1, height / 2 - 4, lblue);
	gdImageRectangle(im, 0, 0, width - 1, height / 2 - 4, hblue);
	
	gdImageFilledRectangle(im, 0, height / 2 + 3, width - 1, height - 1, lblue);
	gdImageRectangle(im, 0, height / 2 + 3, width - 1, height - 1, hblue);	
	
	styleDotted[0] = second;
	styleDotted[1] = gdTransparent;
	styleDotted[2] = gdTransparent;
	styleDotted[3] = gdTransparent;
	
	/*step = (double) ( 250 - 220 ) / height;
	for ( curr = 250, i = 0 ; i != height ; i++ ) {
		color = gdImageColorAllocate(im, (int) 156, (int) 176, (int) 173 );    //#9CB0AD
		gdImageLine(im, 0, i, width, i, color);
		
		curr -= step;
	}*/
	
	xval = -1;
	
	yval1 = -1;
	ymin1 = 0;
	ymax1 = 0;
	
	yval2 = -1;
	ymin2 = 0;
	ymax2 = 0;
	
	limit = height / 2 - 2;
	mlimit1 = height / 4;
	mlimit2 = height - height / 4;
		
	pos = lseek (draw->reader->fd, 0, 1);
	
	reader = draw->reader;
	
	draw->offset = 0;
	data_size = wav_data_size (reader);

lib/Audio/C4Stream/Wav.pm  view on Meta::CPAN

				ymin1 = ymax1 = ymin2 = ymax2 = 0;
			}
			
			if (ytmp < ymin1) {
				ymin1 = ytmp;
				draw1 = 1;
			}
			else if (ytmp > ymax1) {
				ymax1 = ytmp;
				draw1 = 1;
			}
			
			if (draw1) {
				gdImageLine(im, lx, ly1 + mlimit1 - 1, xval, ytmp + mlimit1 - 1, hblue);
				lx = xval;
				ly1 = ytmp;
				draw1 = 0;
			}
			
			ytmp = 0 - ((double) chptr->right / 65536) * limit;
			
			if (ytmp < ymin2) {
				ymin2 = ytmp;
				draw2 = 1;
			}
			else if (ytmp > ymax2) {
				ymax2 = ytmp;
				draw2 = 1;
			}
			
			if ( draw2 ) {
				gdImageLine(im, lx, ly2 + mlimit2 + 1, xval, ytmp + mlimit2 + 1, hblue);
				lx = xval;
				ly2 = ytmp;
				draw2 = 0;
			}

			i += sizeof (struct wav_channels_s);
			draw->offset += sizeof (struct wav_channels_s);
			chptr++;
		}
	}
	
	gdImageStringFT(im, brect, white, font, 8, 0, 5, mlimit1 + height / 8 + 13, "-6,0 db");	
	gdImageStringFT(im, brect, white, font, 8, 0, 5, mlimit2 - height / 8 - 4, "-6,0 db");
	gdImageStringFT(im, brect, white, font, 8, 0, 5, mlimit2 + height / 8 + 14, "-6,0 db");
	gdImageStringFT(im, brect, white, font, 8, 0, 5, height / 8 - 6, "-6,0 db");	
	
	gdImageLine(im, 0, mlimit1 - 1, width, mlimit1 - 1, second);
	gdImageLine(im, 0, mlimit1, width, mlimit1, blue);
	gdImageLine(im, 0, mlimit2, width, mlimit2, second);
	gdImageLine(im, 0, mlimit2 + 1, width, mlimit2 + 1, blue);
	
	gdImageSetStyle(im, styleDotted, 4);
	gdImageLine(im, 1, height / 8 - 1, width - 2, height / 8 - 1, gdStyled);
	gdImageLine(im, 1, mlimit2 + height / 8 + 1, width - 2, mlimit2 + height / 8 + 1, gdStyled);
	gdImageLine(im, 1, mlimit2 - height / 8 + 1, width - 2, mlimit2 - height / 8 + 1, gdStyled);
	gdImageLine(im, 1, mlimit1 + height / 8 - 1, width - 2, mlimit1 + height / 8 - 1, gdStyled);
	
	
	png_ptr = gdImagePngPtr(im, size);

	lseek (draw->reader->fd, pos, 0);
	
	return png_ptr;

}

int WAV_draw (int draw_ptr, int width, int height, SV *png) {
	char *png_ptr;
	int size;
	
	if ((png_ptr = wav_draw ((struct wav_draw_s *) draw_ptr, width, height, &size)) == NULL) {
		return 0;
	}
	
	sv_setpvn(png, png_ptr, size);
	
	return size;
}



( run in 0.880 second using v1.01-cache-2.11-cpan-df04353d9ac )