Speex codec is implemented on Android

Source: Internet
Author: User

Previously, we used speex encoding and decoding in applications. Recently, we have summarized the implementation of speex on Android. Speex is an open-source, free, and patent-free audio compression format. The speex project strives to reduce the input threshold of speech applications by providing an alternative high-performance speech codec. In addition, compared with other codec, speex is also suitable for network applications and has its own unique advantages in network applications. In addition, speex is part of the GNU project and has been well supported in the revised BSD Protocol. Speex is based on CELP and is specially designed for speech compression at a bit rate of 2-kbps. Speex source code is implemented based on C speech (Java implementation is also available, and the efficiency is relatively low ).

1. Go to the official speex website to download the latest speex source code.

2. Create a new Android project and a JNI folder.

3. Copy all the libspeex and include directories and Their subdirectories under the speex source code directory to the $ project/JNI directory.

4. Add the Android. mk file in the JNI directory and edit the file as follows:

[Plain]
View plaincopyprint?
  1. Local_path: = $ (call my-DIR)
  2. Include $ (clear_vars)
  3. Local_module: = libspeex
  4. Local_cflags =-dfixed_point-duse_kiss_fft-dexport = ""-uhave_config_h
  5. Local_c_includes: = $ (local_path)/include
  6. Local_src_files: = \
  7. Libspeex/bits. c \
  8. Libspeex/buffer. c \
  9. Libspeex/cb_search.c \
  10. Libspeex/exc_10_16_table.c \
  11. Libspeex/exc_10_32_table.c \
  12. Libspeex/exc_20_32_table.c \
  13. Libspeex/exc_5_256_table.c \
  14. Libspeex/exc_5_64_table.c \
  15. Libspeex/exc_8_128_table.c \
  16. Libspeex/fftwrap. c \
  17. Libspeex/filterbank. c \
  18. Libspeex/filters. c \
  19. Libspeex/gain_table.c \
  20. Libspeex/gain_table_lbr.c \
  21. Libspeex/hexc_10_32_table.c \
  22. Libspeex/hexc_table.c \
  23. Libspeex/high_lsp_tables.c \
  24. Libspeex/jitter. c \
  25. Libspeex/kiss_fft.c \
  26. Libspeex/kiss_fftr.c \
  27. Libspeex/LPC. c \
  28. Libspeex/LSP. c \
  29. Libspeex/lsp_tables_nb.c \
  30. Libspeex/LTP. c \
  31. Libspeex/MDF. c \
  32. Libspeex/modes. c \
  33. Libspeex/modes_wb.c \
  34. Libspeex/nb_celp.c \
  35. Libspeex/preprocess. c \
  36. Libspeex/quant_lsp.c \
  37. Libspeex/resample. c \
  38. Libspeex/sb_celp.c \
  39. Libspeex/Scal. c \
  40. Libspeex/smallft. c \
  41. Libspeex/speex. c \
  42. Libspeex/speex_callbacks.c \
  43. Libspeex/speex_header.c \
  44. Libspeex/stereo. c \
  45. Libspeex/VBR. c \
  46. Libspeex/VQ. c \
  47. Libspeex/window. c \
  48. Speex_jni.cpp \
  49. Include $ (build_shared_library)

5. Add the application. mk file in the JNI directory. The edit content is as follows:

[Plain]
View plaincopyprint?
  1. App_abi: = armeabi armeabi-v7a

6. Add the speex_config_types.h file in the $ Project/JNI/include/speex/directory. The edited content is as follows:

[HTML]
View plaincopyprint?
  1. 01. # ifndef _ speex_types_h __
  2. 02. # DEFINE _ speex_types_h __
  3. 03. typedef short spx_int16_t;
  4. 04. typedef unsigned short spx_uint16_t;
  5. 05. typedef int spx_int32_t;
  6. 06. typedef unsigned int spx_uint32_t;
  7. 07. # endif

7. Create the JNI packaging class speex_jni.cpp to call the C code function in speex. The editing content is as follows:

[CPP]
View plaincopyprint?
  1. # Include <JNI. h>
  2. # Include <string. h>
  3. # Include <unistd. h>
  4. # Include <speex/speex. h>
  5. Static int codec_open = 0;
  6. Static int dec_frame_size;
  7. Static int enc_frame_size;
  8. Static speexbits ebits, dBits;
  9. Void * enc_state;
  10. Void * dec_state;
  11. Static JavaVM * gjavavm;
  12. Extern "C"
  13. Jniexport jint jnicall java_com_trunkbow_speextest_speex_open
  14. (Jnienv * ENV, jobject OBJ, jint compression ){
  15. Int TMP;
  16. If (codec_open ++! = 0)
  17. Return (jint) 0;
  18. Speex_bits_init (& ebits );
  19. Speex_bits_init (& dBits );
  20. Enc_state = speex_encoder_init (& speex_nb_mode );
  21. Dec_state = speex_decoder_init (& speex_nb_mode );
  22. TMP = compression;
  23. Speex_encoder_ctl (enc_state, speex_set_quality, & TMP );
  24. Speex_encoder_ctl (enc_state, speex_get_frame_size, & enc_frame_size );
  25. Speex_decoder_ctl (dec_state, speex_get_frame_size, & dec_frame_size );
  26. Return (jint) 0;
  27. }
  28. Extern "C"
  29. Jniexport jint java_com_trunkbow_speextest_speex_encode
  30. (Jnienv * ENV, jobject OBJ, jshortarray Lin, jint offset, jbytearray encoded, jint size ){
  31. Jshort buffer [enc_frame_size];
  32. Jbyte output_buffer [enc_frame_size];
  33. Int nsamples = (size-1)/enc_frame_size + 1;
  34. Int I, tot_bytes = 0;
  35. If (! Codec_open)
  36. Return 0;
  37. Speex_bits_reset (& ebits );
  38. For (I = 0; I <nsamples; I ++ ){
  39. Env-> getshortarrayregion (Lin, offset + I * enc_frame_size, enc_frame_size, buffer );
  40. Speex_encode_int (enc_state, buffer, & ebits );
  41. }
  42. // Env-> getshortarrayregion (Lin, offset, enc_frame_size, buffer );
  43. // Speex_encode_int (enc_state, buffer, & ebits );
  44. Tot_bytes = speex_bits_write (& ebits, (char *) output_buffer,
  45. Enc_frame_size );
  46. Env-> setbytearrayregion (encoded, 0, tot_bytes,
  47. Output_buffer );
  48. Return (jint) tot_bytes;
  49. }
  50. Extern "C"
  51. Jniexport jint jnicall java_com_trunkbow_speextest_speex_decode
  52. (Jnienv * ENV, jobject OBJ, jbytearray encoded, jshortarray Lin, jint size ){
  53. Jbyte buffer [dec_frame_size];
  54. Jshort output_buffer [dec_frame_size];
  55. Jsize encoded_length = size;
  56. If (! Codec_open)
  57. Return 0;
  58. Env-> getbytearrayregion (encoded, 0, encoded_length, buffer );
  59. Speex_bits_read_from (& dBits, (char *) buffer, encoded_length );
  60. Speex_decode_int (dec_state, & dBits, output_buffer );
  61. Env-> setshortarrayregion (Lin, 0, dec_frame_size,
  62. Output_buffer );
  63. Return (jint) dec_frame_size;
  64. }
  65. Extern "C"
  66. Jniexport jint jnicall java_com_trunkbow_speextest_speex_getframesize
  67. (Jnienv * ENV, jobject OBJ ){
  68. If (! Codec_open)
  69. Return 0;
  70. Return (jint) enc_frame_size;
  71. }
  72. Extern "C"
  73. Jniexport void jnicall java_com_trunkbow_speextest_speex_close
  74. (Jnienv * ENV, jobject OBJ ){
  75. If (-- codec_open! = 0)
  76. Return;
  77. Speex_bits_destroy (& ebits );
  78. Speex_bits_destroy (& dBits );
  79. Speex_decoder_destroy (dec_state );
  80. Speex_encoder_destroy (enc_state );
  81. }

8. Create the speex tool class on the Java layer. The content is as follows:

[Java]
View plaincopyprint?
  1. Package com. trunkbow. speextest;
  2. Public class speex {
  3. /* Quality
  4. * 1: 4 kbps (very noticeable artifacts, usually intelligible)
  5. * 2: 6 kbps (very noticeable artifacts, good intelligibility)
  6. * 4: 8 kbps (noticeable artifacts sometimes)
  7. * 6: 11 Kpbs (artifacts usually only noticeable with headphones)
  8. * 8: 15 kbps (artifacts not usually noticeable)
  9. */
  10. Private Static final int default_compression = 8;
  11. Speex (){
  12. }
  13. Public void Init (){
  14. Load ();
  15. Open (default_compression );
  16. }
  17. Private void load (){
  18. Try {
  19. System. loadlibrary ("speex ");
  20. } Catch (throwable e ){
  21. E. printstacktrace ();
  22. }
  23. }
  24. Public native int open (INT compression );
  25. Public native int getframesize ();
  26. Public native int decode (byte encoded [], short LiN [], int size );
  27. Public native int encode (short LiN [], int offset, byte encoded [], int size );
  28. Public native void close ();
  29. }

9. Use cygwin to compile and generate the so file.

**************************************** *******************************

* Reproduced in: http://blog.csdn.net/xyz_lmn

* Author: Zhang xingye

* Email: xy-zhang@163.com

**************************************** *******************************

Refer:

Http://code.google.com/p/android-recorder/downloads/list this is an android recorder that uses speex encoding,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.