Audio-C4Stream-Wav

 view release on metacpan or  search on metacpan

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

			ampl_ratio     => 1
		}
	);
	unless ($wavReader) {
		return;
	}

	#nb octets read --> (30 secondes)
	#$this->{_leftTrimSize}  = int( $this->{leftTrimLen} * $this->{_size} / $read->length_seconds );
	#$this->{_rightTrimSize} = int( $this->{rightTrimLen} * $this->{_size} / $read->length_seconds );

	if ( $this->{blankDetectDb} ) {
		my $blankDetectAmpl = int( &_dbToAmpl( $this->{blankDetectDb} ) );
		if ( $blankDetectAmpl > 36767 ) {
			$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

The functions are :

=over 7

=item C<getPngData> 

Get the sine wave graphic of the WAV data

=item C<getNextData> 

Get next data

=item C<getNextRawData> 

Get next source data

=item C<getOrigDataLen> 

Get the original data length

=item C<getOrigSec> 

Get the number of seconds of the original data

=item C<getDataLen> 

Get the length of the mixed data

=item C<getSec> 

Get the seconds of the data

=back

=head1 SEE ALSO

See Audio::C4Stream::Mixer

Depends on IO::Scalar and C library GD L<https://bitbucket.org/pierrejoye/gd-libgd/overview>

=head1 AUTHOR

cloud4pc, L<adeamara@cloud4pc.com>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2012 by cloud4pc

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.1 or,
at your option, any later version of Perl 5 you may have available.

=cut

__C__

#include <gd.h>
#include <gdfontl.h>

#define BLOCKSIZE 4096
#define MAX_AMPL 32767

struct wav_header_s {
	int chunkSize;
	int subChunkSize;

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 2.484 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )