不,我不知道出了什么问题。至少当我尝试将函数从 main 中取出并将其粘贴到 main 之前时,我收到的错误比以前更多。
我收到错误:
错误:在“{”标记之前不允许使用函数定义 void draw_spectrum(Display *display, Window window, double *magnitudes) {
使用此代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <alsa/asoundlib.h> // For audio capture
#include <X11/Xlib.h> // For X11 graphics
#include "kiss_fft.h" // For FFT calculations
int main(int argc, char*argv[]) {
/// Initialize audio capture and X11 display:
// Open audio device
snd_pcm_t *pcm_handle;
snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_CAPTURE, 0);
// Set audio parameters (sample rate, channels, etc.)
snd_pcm_set_params(pcm_handle, SND_PCM_FORMAT_U8, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 48000, 1, 500000);
// Open X11 display
Display *display = XOpenDisplay(NULL);
Window window = XCreateSimpleWindow(display, window, 100, 100, 800, 600, 1, BlackPixel(display, DefaultScreen(display)), WhitePixel(display, DefaultScreen(display))); //finish later...display, parent, x, y, width, height, border_width, border, background
XMapWindow(display, window);
/// Create buffers for audio samples and FFT output:
#define FFT_SIZE 1024 // Adjust as needed
kiss_fft_cfg cfg = kiss_fft_alloc(FFT_SIZE, 0, 0, 0);
kiss_fft_cpx in[FFT_SIZE];
kiss_fft_cpx out[FFT_SIZE];
// Map magnitudes to visual representation (lines and bars)
void draw_spectrum(Display *display, Window window, double *magnitudes) {
// Get window dimensions
int width, height;
XWindowAttributes wa;
XGetWindowAttributes(display, window, &wa);
width = wa.width;
height = wa.height;
// Calculate bar width and spacing
int bar_width = width / (FFT_SIZE / 2 + 1);
int bar_spacing = 1; // Adjust spacing as needed
GC gc = XCreateGC(display, window, 0, NULL); // Create a graphics context
// Clear the window (optional)
XSetForeground(display, gc, WhitePixel(display, DefaultScreen(display)));
XFillRectangle(display, window, gc, 0, 0, width, height);
// Set drawing color for the bars
XSetForeground(display, gc, BlackPixel(display, DefaultScreen(display)));
// Draw bars for each magnitude value
for (int i = 0; i < FFT_SIZE / 2 + 1; i++) {
int bar_height = (int) (magnitudes[i] * height / 256); // Scale magnitude to screen height
XFillRectangle(display, window, gc,
i * (bar_width + bar_spacing), height - bar_height,
bar_width, bar_height);
}
XFreeGC(display, gc); // Release the graphics context
}
// Handle events and refresh display
XNextEvent(display, ...);
XFlush(display);
}
/// Main loop:
while (1) {
// Capture audio samples
snd_pcm_readi(pcm_handle, in, FFT_SIZE);
// Apply window function (Hann window)
for (int i = 0; i < FFT_SIZE; i++) {
in[i].r *= 0.5 * (1 - cos(2 * M_PI * i / (FFT_SIZE - 1)));
}
// Perform FFT
kiss_fft(cfg, in, out);
// Calculate magnitudes
double magnitudes[FFT_SIZE / 2 + 1];
for (int i = 0; i < FFT_SIZE / 2 + 1; i++) {
magnitudes[i] = sqrt(out[i].r * out[i].r + out[i].i * out[i].i);
}
// Map magnitudes to visual representation (lines and bars)
draw_spectrum(display, window, magnitudes);
/// Clean up:
// Close audio device
snd_pcm_close(pcm_handle);
// Close X11 display
XDestroyWindow(display, window);
XCloseDisplay(display);
return 0;
}
我该如何解决这个问题?
标准 C 不允许将函数定义放在函数定义中(嵌套函数定义)。
在这种情况下,函数
main
没有引用函数 draw_spectrum
的局部变量,因此您可以通过将 draw_spectrum
的定义移到 main
之前来解决此问题。
此外,函数
}
中看起来有一个额外的 draw_spectrum
,而主循环周围缺少 }
。您应该对代码应用适当的缩进,以便更容易找到此类错误。
另一点是,在函数调用中使用
...
(如XNextEvent(display, ...);
)看起来无效。你应该用合适的东西替换它。