]> git.sesse.net Git - mlt/commitdiff
add waveform method to frame
authorddennedy <ddennedy@d19143bc-622f-0410-bfdd-b5b2a6649095>
Sun, 29 Aug 2004 21:46:19 +0000 (21:46 +0000)
committerddennedy <ddennedy@d19143bc-622f-0410-bfdd-b5b2a6649095>
Sun, 29 Aug 2004 21:46:19 +0000 (21:46 +0000)
git-svn-id: https://mlt.svn.sourceforge.net/svnroot/mlt/trunk/mlt@397 d19143bc-622f-0410-bfdd-b5b2a6649095

src/framework/mlt_frame.c
src/framework/mlt_frame.h

index f0092ed90e564287a2e17f7f0f3dec4019610ebd..204eaae94a2ffd8c046d6763f6c2afaa953d20a7 100644 (file)
@@ -339,6 +339,54 @@ int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *for
        return 0;
 }
 
+unsigned char *mlt_frame_get_waveform( mlt_frame this, double fps, int w, int h )
+{
+       /* Currently, w is not honored - the user must scale to desired height.
+          Get the integer property waveform_width after calling to see the 
+          produced width (currently equal to # samples). */
+       int16_t *pcm = NULL;
+       mlt_properties properties = mlt_frame_properties( this );
+       mlt_audio_format format = mlt_audio_pcm;
+       int frequency = 32000; /* lower frequency available? */
+       int channels = 2;
+       int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
+       
+       /* get the pcm data */
+       mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
+       mlt_properties_set_int( properties, "waveform_width", samples );
+       
+       /* make an 8-bit buffer large enough to hold rendering */
+       int size = samples * h;
+       unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
+       if ( bitmap != NULL )
+               memset( bitmap, 0, size );
+       mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
+       
+       /* render - pcm data has channels interleaved */
+       int vertical_resolution = h / channels;
+       int i, j, k;
+       
+       for ( i = 0; i < samples; i++ )
+       {
+               for ( j = 0; j < channels; j++ )
+               {
+                       /* the height of a "bar" is the ratio of the sample multiplied by the vertical resolution */
+                       int pcm_scaled = ( int )( ( double )( *pcm ) / 32767 * vertical_resolution );
+                       int height = pcm_scaled < 0 ? -pcm_scaled : pcm_scaled;
+                       int offset = j * samples * vertical_resolution;
+                       int displacement = pcm_scaled < 0 ? ( vertical_resolution / 2 ) : ( vertical_resolution / 2 - pcm_scaled );
+                       unsigned char *p = &bitmap[ offset + i + displacement * samples ];
+                       
+                       for ( k = 0; k < height; k++ )
+                               p[ samples * k ] = 0xFF;
+                       
+                       pcm++;
+               }
+       }
+       return bitmap;
+}
+
+
 void mlt_frame_close( mlt_frame this )
 {
        if ( this != NULL && mlt_properties_dec_ref( mlt_frame_properties( this ) ) <= 0 )
index 7b23aed0ffdc62e9d77df201f3788a56eef83798..11da681bde77f1dd4da2326a33600d54a527be88 100644 (file)
@@ -51,6 +51,7 @@ extern int mlt_frame_set_position( mlt_frame self, mlt_position value );
 extern int mlt_frame_get_image( mlt_frame self, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable );
 extern uint8_t *mlt_frame_get_alpha_mask( mlt_frame self );
 extern int mlt_frame_get_audio( mlt_frame self, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples );
+extern unsigned char *mlt_frame_get_waveform( mlt_frame self, double fps, int w, int h );
 extern int mlt_frame_push_get_image( mlt_frame self, mlt_get_image get_image );
 extern mlt_get_image mlt_frame_pop_get_image( mlt_frame self );
 extern int mlt_frame_push_frame( mlt_frame self, mlt_frame that );
@@ -96,4 +97,3 @@ extern int mlt_sample_calculator( float fps, int frequency, int64_t position );
   v = v > 240 ? 240 : v
 
 #endif
-