Official website for Linux User & Developer
FOLLOW US ON:
Nov
15

Open source programming for beginners

by Kunal Deo

Sometimes even experienced developers can be a bit overwhelmed by Linux’s extensive development capabilities. Sit back and soak up these tips to become a smarter and more productive Linux developer overnight…

Developing for the Text User Interface
While GUI is almost everywhere, the Text User Interface (TUI) is not dead. GUI interfaces need more system resources and some times may not even work if you have a faulty driver or misconfigured X server. In those cases, Text User Interface can come in handy. Text User Interface is also suitable for embedded devices where resources are very limited.

Note that a Text User Interface is not a command-line interface, but a GUI-like interface written for the Terminal, so that it can be executed without X server or over a remote shell. You can use the ncurses (new curses) library to develop Text User Interface applications. The ncurses runtime is included with almost all Linux distributions. However, you may need to install the developer package for ncurses. On Ubuntu/Debian you can use the following command to install ncurses:

$ sudo apt-get install libncurses5-dev

@code: hello_ncurses.c

#include <ncurses.h> /* ncurses.h includes stdio.h */
#include <string.h> 

int main()
{
 char mesg[]=”Hello Ncurses”;    /* message to be appeared on the screen */
 int row,col; /* to store the number of rows and     the number of colums of the screen */
 initscr(); /* start the curses mode */
 getmaxyx(stdscr,row,col); /* get the number of rows and columns */
 mvprintw(row/2,(col-strlen(mesg))/2,”%s”,mesg); /* print the message at the center of the screen */
 mvprintw(row-2,0,”This screen has %d rows and %d columns\n”,row,col);
 refresh();
 getch();
 endwin();

 return 0;
}

Building multimedia applications
Back in the old days, building multimedia applications for Linux (or other UNIX systems) was very difficult. Even if you could get a multimedia application working, support for popular multimedia formats was missing. With the introduction of FFmpeg, building multimedia applications for Linux (and almost any other OS) has become very easy. FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video. It includes libavcodec – which provides developers with an easy-to-use interface to develop multimedia applications. FFmpeg supports almost all the audio/video codecs and file formats. It is capable of transcoding, playing and demuxing for most of these formats/codecs.

The following example encodes a single-tone MP2 audio file using the FFmpeg library.
@example (FFmpeg API Example)
@code:audiotone.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_AV_CONFIG_H
#undef HAVE_AV_CONFIG_H
#endif
#include “libavcodec/avcodec.h”
#include “libavutil/mathematics.h”
#define INBUF_SIZE 4096
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096
/*
 * Audio encoding example
 */
static void audio_encode_example(const char *filename)
{
 AVCodec *codec;
 AVCodecContext *c= NULL;
 int frame_size, i, j, out_size, outbuf_size;
 FILE *f;
 short *samples;
 float t, tincr;
 uint8_t *outbuf;
 printf(“Encoding Single Tone Audio...\n”);

/* find the MP2 encoder */
 codec = avcodec_find_encoder(CODEC_ID_MP2);
 if (!codec) {
 fprintf(stderr, “codec not found\n”);
 exit(1);
 }
 c= avcodec_alloc_context();

/* put sample parameters */
 c->bit_rate = 64000;
 c->sample_rate = 44100;
 c->channels = 2;

/* open it */
 if (avcodec_open(c, codec) < 0) {
 fprintf(stderr, “could not open codec\n”);
 exit(1);
 }

/* the codec gives us the frame size, in samples */
 frame_size = c->frame_size;
 samples = malloc(frame_size * 2 * c->channels);
 outbuf_size = 10000;
 outbuf = malloc(outbuf_size);
 f = fopen(filename, “wb”);
 if (!f) {
 fprintf(stderr, “could not open %s\n”, filename);
 exit(1);
 }

/* encode a single tone sound */
 t = 0;
 tincr = 2 * M_PI * 440.0 / c->sample_rate;
 for(i=0;i<200;i++) {
 for(j=0;j<frame_size;j++) {
 samples[2*j] = (int)(sin(t) * 10000);
 samples[2*j+1] = samples[2*j];
 t += tincr;
 }

/* encode the samples */
 out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
 fwrite(outbuf, 1, out_size, f);
 }
 fclose(f);
 printf(“%s audio file written\n”,filename);
 free(outbuf);
 free(samples);
 avcodec_close(c);
 av_free(c);
}

int main(int argc, char **argv)
{
 const char *filename;

/* must be called before using avcodec lib */
 avcodec_init();

/* register all the codecs */
 avcodec_register_all();
 if (argc <= 1) {
 audio_encode_example(“tone.mp2”);
 ;
 } else {
 filename = argv[1];
 }
 return 0;
}

To build this code, you will need to have FFmpeg developer libraries installed in your system. Once you have it installed, perform the following command to compile the code.

$ gcc audiotone.c -o audiotone -lavcodec -lavutil -lm -lz
kunal@ubuntu-vm-kdeo:~$ ./api
Encoding Single Tone Audio...
tone.mp2 audio file written

To play the file:

$ ffplay tone.mp2
twitter follow us
Pages: 1 2 3 4 5 6
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.
    • grep

      Personally I’d hesitate telling a beginning programmer to use the system() function call. But if I mentioned it, I’d at least explain that it could have grave system security implications.

      For instance, the command that the system() function call executes may run at the same privileges as the program containing the system() function call. This could be bad news if your program somehow gets “root” privileges (via the Set-User-ID or Set-Group-ID settings, or otherwise).

      Next, the program containing the system() function call may not execute the command that you think it will. For instance, using your example ( system(“ls -l”) ), if a cracker can somehow get you to execute your program with a bogus executable file called “ls” in the current directory, then what’s actually executed will be what the cracker intended, but not what you expected. Probably not the best example, but you get the idea. Using a full path name in the parameter for the system() function call may help mitigate this problem, at the possible expense of cross compatibility, but not necessarily eliminate it.

      Admittedly, there’s a time and place to use any of the hundreds of Linux function calls, but a good programmer will (or should) be aware of the implications of those that they chose.

    • Pingback: LXer: Open Source programming for beginners - oBlurb

    • Ken Jennings

      SQL in a C program is a LOT more simple when using a database that provides an embedded SQL compiler. (PostgreSQL for one.)
      Not the end-all-be-all of perfect examples, but here’s a sample:
      http://www.kenjennings.cc/st/stprgux.html
      http://www.kenjennings.cc/st/prg/ux/test_pgc.tar.gz

    • Pingback: Linux User & Developer issue 93 is out now! | Linux User

    • Pingback: Introduction to Linux / Newbies Guide

    • Pingback: Linux coding tips for newbies « 0ddn1x: tricks with *nix

    • Pingback: Nikon D3100 Dslr Reviews

    • Morgan

      A new book covering the basics: Programming From Scratch by Gary Crandall available here http://www.perfscipress.com/programming-from-scratch/

    • http://www.howtouseachainsaw.com/item-poulan-pro-50cc-gas-22-in-chain-saw-with-case-pp5020av-new_280924888864_US_0.html buy chainsaw

      It really is really a good plus practical little bit of info. I’m fulfilled you provided this handy facts along with us. Remember to keep us current this way. Appreciate your spreading.