0
# Audio Processing
1
2
Audio resampling, channel layout conversion, and sample format transformations using FFmpeg's libswresample for comprehensive audio processing.
3
4
## Capabilities
5
6
### Resampling Context Management
7
8
#### Context Operations
9
10
```java { .api }
11
/**
12
* Allocate SwrContext
13
* @return Allocated context or null on failure
14
*/
15
SwrContext swr_alloc();
16
17
/**
18
* Initialize context after setting parameters
19
* @param s Resampling context
20
* @return >= 0 on success
21
*/
22
int swr_init(SwrContext s);
23
24
/**
25
* Free resampling context
26
* @param s Pointer to context to free
27
*/
28
void swr_free(SwrContext s);
29
30
/**
31
* Convert audio samples
32
* @param s Resampling context
33
* @param out Output buffer pointers
34
* @param out_count Maximum output samples per channel
35
* @param in Input buffer pointers
36
* @param in_count Input samples per channel
37
* @return Number of output samples per channel, negative on error
38
*/
39
int swr_convert(SwrContext s, PointerPointer out, int out_count,
40
PointerPointer in, int in_count);
41
```
42
43
**Usage Example:**
44
45
```java
46
import org.bytedeco.ffmpeg.swresample.*;
47
import static org.bytedeco.ffmpeg.global.swresample.*;
48
import static org.bytedeco.ffmpeg.global.avutil.*;
49
50
// Create resampling context
51
SwrContext swrContext = swr_alloc();
52
53
// Set input parameters
54
av_opt_set_int(swrContext, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
55
av_opt_set_int(swrContext, "in_sample_rate", 44100, 0);
56
av_opt_set_sample_fmt(swrContext, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
57
58
// Set output parameters
59
av_opt_set_int(swrContext, "out_channel_layout", AV_CH_LAYOUT_MONO, 0);
60
av_opt_set_int(swrContext, "out_sample_rate", 22050, 0);
61
av_opt_set_sample_fmt(swrContext, "out_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
62
63
// Initialize
64
int result = swr_init(swrContext);
65
if (result < 0) {
66
throw new RuntimeException("Cannot initialize resampler");
67
}
68
69
// Convert audio
70
PointerPointer inputData = /* ... input audio data ... */;
71
PointerPointer outputData = /* ... output buffer ... */;
72
int outputSamples = swr_convert(swrContext, outputData, 1024, inputData, 2048);
73
74
System.out.println("Converted " + outputSamples + " samples");
75
76
// Cleanup
77
swr_free(swrContext);
78
```
79
80
### Channel Layout Operations
81
82
#### Channel Management
83
84
```java { .api }
85
/**
86
* Get default channel layout for given number of channels
87
* @param nb_channels Number of channels
88
* @return Channel layout
89
*/
90
long av_get_default_channel_layout(int nb_channels);
91
92
/**
93
* Get number of channels in layout
94
* @param channel_layout Channel layout
95
* @return Number of channels
96
*/
97
int av_get_channel_layout_nb_channels(long channel_layout);
98
99
/**
100
* Get channel layout string
101
* @param buf Buffer for result
102
* @param buf_size Buffer size
103
* @param nb_channels Number of channels
104
* @param channel_layout Channel layout
105
* @return 0 on success
106
*/
107
int av_get_channel_layout_string(BytePointer buf, int buf_size, int nb_channels, long channel_layout);
108
```
109
110
## Constants
111
112
### Channel Layouts
113
114
```java { .api }
115
// Standard channel layouts
116
long AV_CH_LAYOUT_MONO = 0x4; // Center
117
long AV_CH_LAYOUT_STEREO = 0x3; // Left + Right
118
long AV_CH_LAYOUT_2POINT1 = 0x103; // Stereo + LFE
119
long AV_CH_LAYOUT_SURROUND = 0x7; // Stereo + Center
120
long AV_CH_LAYOUT_4POINT0 = 0x107; // Surround + Back Center
121
long AV_CH_LAYOUT_5POINT0 = 0x607; // 4.0 + Side Left/Right
122
long AV_CH_LAYOUT_5POINT1 = 0x70F; // 5.0 + LFE
123
long AV_CH_LAYOUT_7POINT1 = 0x1C07; // 5.1 + Back Left/Right
124
```
125
126
### Sample Formats
127
128
```java { .api }
129
// Audio sample formats
130
int AV_SAMPLE_FMT_U8 = 0; // Unsigned 8-bit
131
int AV_SAMPLE_FMT_S16 = 1; // Signed 16-bit
132
int AV_SAMPLE_FMT_S32 = 2; // Signed 32-bit
133
int AV_SAMPLE_FMT_FLT = 3; // Float
134
int AV_SAMPLE_FMT_DBL = 4; // Double
135
int AV_SAMPLE_FMT_U8P = 5; // Unsigned 8-bit planar
136
int AV_SAMPLE_FMT_S16P = 6; // Signed 16-bit planar
137
int AV_SAMPLE_FMT_S32P = 7; // Signed 32-bit planar
138
int AV_SAMPLE_FMT_FLTP = 8; // Float planar
139
int AV_SAMPLE_FMT_DBLP = 9; // Double planar
140
```