Wav TO amr and easy adjustment of the volume, wavamr volume
Let's talk about how to convert wav to amr. Wav is the easiest way to generate recording audio on PC, but the disadvantage is that the generated audio volume is relatively large. Amr is a mainstream audio playing format on mobile phones. Its advantage is that the audio is small in size and easy to transmit.
The conversion method is very simple. There are two types of amr. Here we take nb as an example. First, you need to download opencore-amr and import the static library and files to the project. Enter the following code:
int wav2Amr( const char *infile, const char *outfile) {enum Mode mode = MR122;int ch, dtx = 0;FILE *out;void *wav, *amr;int format, sampleRate, channels, bitsPerSample;int inputSize;uint8_t* inputBuf;int modeRate = 4750;mode = findMode(modeRate);wav = wav_read_open(infile);if (!wav) {fprintf(stderr, "Unable to open wav file %s\n", infile);return 1;}if (!wav_get_header(wav, &format, &channels, &sampleRate, &bitsPerSample, NULL)) {fprintf(stderr, "Bad wav file %s\n", infile);return 1;}if (format != 1) {fprintf(stderr, "Unsupported WAV format %d\n", format);return 1;}if (bitsPerSample != 16) {fprintf(stderr, "Unsupported WAV sample depth %d\n", bitsPerSample);return 1;}if (channels != 1)fprintf(stderr, "Warning, only compressing one audio channel\n");if (sampleRate != 8000)fprintf(stderr, "Warning, AMR-NB uses 8000 Hz sample rate (WAV file has %d Hz)\n", sampleRate);inputSize = channels*2*160;inputBuf = (uint8_t*) malloc(inputSize);amr = Encoder_Interface_init(dtx);out = fopen(outfile, "wb");if (!out) {perror(outfile);return 1;}fwrite("#!AMR\n", 1, 6, out);while (1) {short buf[160];uint8_t outbuf[500];int read, i, n;read = wav_read_data(wav, inputBuf, inputSize);read /= channels;read /= 2;if (read < 160)break;for (i = 0; i < 160; i++) {const uint8_t* in = &inputBuf[2*channels*i];buf[i] = in[0] | (in[1] << 8);}n = Encoder_Interface_Encode(amr, mode, buf, outbuf, 0);fwrite(outbuf, 1, n, out);}free(inputBuf);fclose(out);Encoder_Interface_exit(amr);wav_read_close(wav);return 0;}
The code is used in opencore test cases. It can be used directly.
Next, let's talk about how to adjust the volume. The test found that the main volume, wave, microphone volume, synthesizer volume, and so on are all useless, at least I tested the effect is not obvious. Since it cannot be processed during recording, you can only process audio files after recording. This blog has a detailed description, and the test shows that it can run normally. Note that the audio processed here does not contain the audio header: Click to open the link.
The test shows that if the audio volume is set to the same size, no sound is generated, so the specific adjustment requires a separate algorithm, but the principle is basically what the connection says.