'/* A simple frontend for libxmp */
'/* This file is in public domain */

#include "crt.bi"
#Include "xmp.bi"
#include "sound.bi"

Sub display_data(mi As xmp_module_info ptr, fi As xmp_frame_info Ptr)
	printf(!"%3d/%3d %3d/%3d\r",_
	       fi->pos, mi->mod_->len, fi->row, fi->num_rows)

	fflush(stdout)	
End Sub





	Dim As xmp_context ctx
	Dim as xmp_module_info mi
	Dim As xmp_frame_info fi
	Dim As integer row, i
Dim argv As ZString Ptr Ptr = __FB_ARGV__

	if sound_init(44100, 2) < 0 Then
		fprintf(stderr, !"%s: can't initialize sound\n", Command(0))
		End		
	EndIf


	ctx = xmp_create_context()

	for i = 1 to __FB_ARGC__ - 1
		if xmp_load_module(ctx, *argv[i]) < 0 Then
			fprintf(stderr, !"%s: error loading %s\n",Command(0) ,*argv[i])
			continue For
		EndIf

		if xmp_start_player(ctx, 44100, 0) = 0 Then

			'/* Show module data */

			xmp_get_module_info(ctx, @mi)
			printf(!"%s (%s)\n", mi.mod_->name, mi.mod_->type)

			'/* Play module */

			row = -1
			while xmp_play_frame(ctx) = 0
				xmp_get_frame_info(ctx, @fi)
				if fi.loop_count > 0 Then Exit while
					

				sound_play(fi.buffer, fi.buffer_size)
			
				if fi.row <> row Then
					display_data(@mi, @fi)
					row = fi.row
				EndIf
			Wend
			xmp_end_player(ctx)
		EndIf

		xmp_release_module(ctx)
		printf(!"\n")
	Next

	xmp_free_context(ctx)

	sound_deinit()
