Speex for Android

Source: Internet
Author: User

http://blog.csdn.net/chenfeng0104/article/details/7088138
In Android development, recording is required and sent to the other device. At this point, the mobile phone is often GPRS, 3G and other ways to surf the internet, so it is very important to save traffic, using Speex to compress audio files, you can press the audio file for a small number of times.

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

2. Create a new app (I created the app named audio) and create a JNI directory ($project/jni).

3. Copy the Libspeex and include directories and their subdirectory files under the Speex source directory to the $project/jni directory ($project/jni/libspeex and $project/jni/include).

4. Add the Android.mk file to the JNI directory and edit the content as follows

[Plain]View Plaincopy
  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. ./speex_jni.cpp \
  8. ./LIBSPEEX/BITS.C \
  9. ./libspeex/buffer.c \
  10. ./libspeex/cb_search.c \
  11. ./libspeex/exc_10_16_table.c \
  12. ./libspeex/exc_10_32_table.c \
  13. ./libspeex/exc_20_32_table.c \
  14. ./libspeex/exc_5_256_table.c \
  15. ./libspeex/exc_5_64_table.c \
  16. ./libspeex/exc_8_128_table.c \
  17. ./libspeex/fftwrap.c \
  18. ./libspeex/filterbank.c \
  19. ./LIBSPEEX/FILTERS.C \
  20. ./libspeex/gain_table.c \
  21. ./libspeex/gain_table_lbr.c \
  22. ./libspeex/hexc_10_32_table.c \
  23. ./libspeex/hexc_table.c \
  24. ./LIBSPEEX/HIGH_LSP_TABLES.C \
  25. ./libspeex/jitter.c \
  26. ./libspeex/kiss_fft.c \
  27. ./libspeex/kiss_fftr.c \
  28. ./LIBSPEEX/LPC.C \
  29. ./libspeex/lsp.c \
  30. ./LIBSPEEX/LSP_TABLES_NB.C \
  31. ./libspeex/ltp.c \
  32. ./LIBSPEEX/MDF.C \
  33. ./LIBSPEEX/MODES.C \
  34. ./LIBSPEEX/MODES_WB.C \
  35. ./libspeex/nb_celp.c \
  36. ./LIBSPEEX/PREPROCESS.C \
  37. ./libspeex/quant_lsp.c \
  38. ./libspeex/resample.c \
  39. ./libspeex/sb_celp.c \
  40. ./LIBSPEEX/SCAL.C \
  41. ./libspeex/smallft.c \
  42. ./libspeex/speex.c \
  43. ./LIBSPEEX/SPEEX_CALLBACKS.C \
  44. ./libspeex/speex_header.c \
  45. ./libspeex/stereo.c \
  46. ./libspeex/vbr.c \
  47. ./libspeex/vq.c \
  48. ./libspeex/window.c
  49. Include $ (build_shared_library)


5. Add the Application.mk file to the JNI directory and edit the content as follows

[Plain]View Plaincopy
    1. App_abi: = Armeabi armeabi-v7a


6. Add the Speex_config_types.h file in the $project/jni/include/speex/directory and edit the contents as follows

[CPP]View Plaincopy
    1. #ifndef __speex_types_h__
    2. #define __speex_types_h__
    3. typedef short spx_int16_t;
    4. typedef unsigned short spx_uint16_t;
    5. typedef INT spx_int32_t;
    6. typedef unsigned int spx_uint32_t;
    7. #endif

7. Create the Jni wrapper class Speex_jni.cpp, which is used to call the C code function in Speex, and edit the contents as follows

[CPP]View Plaincopy
  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_audio_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_audio_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_audio_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_audio_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_audio_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 in the Java layer with the following content

[Java]View Plaincopy
  1. Package Com.audio;
  2. Class Speex {
  3. / * Quality
  4. * 1:4KBPS (very noticeable artifacts, usually intelligible)
  5. * 2:6KBPS (very noticeable artifacts, good intelligibility)
  6. * 4:8KBPS (noticeable artifacts sometimes)
  7. * 6:11KPBS (artifacts usually only noticeable with headphones)
  8. * 8:15KBPS (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 (shortlin[], int offset, byte encoded[], int size);
  28. public native void Close ();
  29. }

9. Open the Cygwin tool, switch to the project directory (my project is in F:\workspace\Audio), enter $ndk/ndk-build

Cygwin tool installation and configuration, you can read this article-using the NDK and the environment to build

The Libs directory and the libspeex.so file are generated in the project, which is System.loadlibrary ("Speex") in the Speex class, and the system is referenced by the "Speex" according to the operating system Find the corresponding dynamic library libspeex.so,windows is the. dll file under Linux, which is the. so file.

Currently, my project structure is as follows

Speex for Android

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.