/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id$ * * Copyright (C) 2008 Dan Everton (safetydan) * Copyright (C) 2009 Maurus Cuelenaere * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/ #define lrocklib_c #define LUA_LIB #include "lua.h" #include "lauxlib.h" #include "rocklib.h" #include "lib/helper.h" #include "lib/pluginlib_actions.h" /* * http://www.lua.org/manual/5.1/manual.html#lua_CFunction * * In order to communicate properly with Lua, a C function must use the following protocol, * which defines the way parameters and results are passed: a C function receives its arguments * from Lua in its stack in direct order (the first argument is pushed first). To return values to Lua, * a C function just pushes them onto the stack, in direct order (the first result is pushed first), * and returns the number of results. Any other value in the stack below the results will be properly * discarded by Lua. Like a Lua function, a C function called by Lua can also return many results. * * When porting new functions, don't forget to check rocklib_aux.pl whether it automatically creates * wrappers for the function and if so, add the function names to @forbidden_functions. This is to * prevent namespace collisions and adding duplicate wrappers. */ /* * ----------------------------- * * Rockbox Lua image wrapper * * ----------------------------- */ #define ROCKLUA_IMAGE "rb.image" struct rocklua_image { int width; int height; fb_data *data; fb_data dummy[1][1]; }; static void rli_wrap(lua_State *L, fb_data *src, int width, int height) { struct rocklua_image *a = (struct rocklua_image *)lua_newuserdata(L, sizeof(struct rocklua_image)); luaL_getmetatable(L, ROCKLUA_IMAGE); lua_setmetatable(L, -2); a->width = width; a->height = height; a->data = src; } static fb_data* rli_alloc(lua_State *L, int width, int height) { size_t nbytes = sizeof(struct rocklua_image) + ((width*height) - 1) * sizeof(fb_data); struct rocklua_image *a = (struct rocklua_image *)lua_newuserdata(L, nbytes); luaL_getmetatable(L, ROCKLUA_IMAGE); lua_setmetatable(L, -2); a->width = width; a->height = height; a->data = &a->dummy[0][0]; return a->data; } static int rli_new(lua_State *L) { int width = luaL_checkint(L, 1); int height = luaL_checkint(L, 2); rli_alloc(L, width, height); return 1; } static struct rocklua_image* rli_checktype(lua_State *L, int arg) { void *ud = luaL_checkudata(L, arg, ROCKLUA_IMAGE); luaL_argcheck(L, ud != NULL, arg, "'" ROCKLUA_IMAGE "' expected"); return (struct rocklua_image*) ud; } static int rli_width(lua_State *L) { struct rocklua_image *a = rli_checktype(L, 1); lua_pushnumber(L, a->width); return 1; } static int rli_height(lua_State *L) { struct rocklua_image *a = rli_checktype(L, 1); lua_pushnumber(L, a->height); return 1; } static fb_data* rli_element(lua_State *L) { struct rocklua_image *a = rli_checktype(L, 1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); luaL_argcheck(L, 1 <= x && x <= a->width, 2, "index out of range"); luaL_argcheck(L, 1 <= y && y <= a->height, 3, "index out of range"); /* return element address */ return &a->data[a->width * (y - 1) + (x - 1)]; } static int rli_set(lua_State *L) { fb_data newvalue = FB_SCALARPACK((unsigned)luaL_checknumber(L, 4)); *rli_element(L) = newvalue; return 0; } static int rli_get(lua_State *L) { lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(*rli_element(L))); return 1; } static int rli_tostring(lua_State *L) { struct rocklua_image *a = rli_checktype(L, 1); lua_pushfstring(L, ROCKLUA_IMAGE ": %dx%d", a->width, a->height); return 1; } static const struct luaL_reg rli_lib [] = { {"__tostring", rli_tostring}, {"set", rli_set}, {"get", rli_get}, {"width", rli_width}, {"height", rli_height}, {NULL, NULL} }; static inline void rli_init(lua_State *L) { luaL_newmetatable(L, ROCKLUA_IMAGE); lua_pushstring(L, "__index"); lua_pushvalue(L, -2); /* pushes the metatable */ lua_settable(L, -3); /* metatable.__index = metatable */ luaL_register(L, NULL, rli_lib); } /* * ----------------------------- * * Rockbox wrappers start here! * * ----------------------------- */ #define RB_WRAP(M) static int rock_##M(lua_State UNUSED_ATTR *L) #define SIMPLE_VOID_WRAPPER(func) RB_WRAP(func) { (void)L; func(); return 0; } /* Helper function for opt_viewport */ static void check_tablevalue(lua_State *L, const char* key, int tablepos, void* res, bool unsigned_val) { lua_getfield(L, tablepos, key); /* Find table[key] */ if(!lua_isnoneornil(L, -1)) { if(unsigned_val) *(unsigned*)res = luaL_checkint(L, -1); else *(int*)res = luaL_checkint(L, -1); } lua_pop(L, 1); /* Pop the value off the stack */ } static struct viewport* opt_viewport(lua_State *L, int narg, struct viewport* alt) { if(lua_isnoneornil(L, narg)) return alt; int tablepos = lua_gettop(L); struct viewport *vp; lua_getfield(L, tablepos, "vp"); /* get table['vp'] */ if(lua_isnoneornil(L, -1)) { lua_pop(L, 1); /* Pop nil off stack */ vp = (struct viewport*) lua_newuserdata(L, sizeof(struct viewport)); /* Allocate memory and push it as udata on the stack */ memset(vp, 0, sizeof(struct viewport)); /* Init viewport values to 0 */ lua_setfield(L, tablepos, "vp"); /* table['vp'] = vp (pops value off the stack) */ } else { vp = (struct viewport*) lua_touserdata(L, -1); /* Reuse viewport struct */ lua_pop(L, 1); /* We don't need the value on stack */ } luaL_checktype(L, narg, LUA_TTABLE); check_tablevalue(L, "x", tablepos, &vp->x, false); check_tablevalue(L, "y", tablepos, &vp->y, false); check_tablevalue(L, "width", tablepos, &vp->width, false); check_tablevalue(L, "height", tablepos, &vp->height, false); #ifdef HAVE_LCD_BITMAP check_tablevalue(L, "font", tablepos, &vp->font, false); check_tablevalue(L, "drawmode", tablepos, &vp->drawmode, false); #endif #if LCD_DEPTH > 1 check_tablevalue(L, "fg_pattern", tablepos, &vp->fg_pattern, true); check_tablevalue(L, "bg_pattern", tablepos, &vp->bg_pattern, true); #endif return vp; } RB_WRAP(set_viewport) { struct viewport *vp = opt_viewport(L, 1, NULL); int screen = luaL_optint(L, 2, SCREEN_MAIN); rb->screens[screen]->set_viewport(vp); return 0; } RB_WRAP(clear_viewport) { int screen = luaL_optint(L, 1, SCREEN_MAIN); rb->screens[screen]->clear_viewport(); return 0; } #ifdef HAVE_LCD_BITMAP RB_WRAP(lcd_framebuffer) { rli_wrap(L, rb->lcd_framebuffer, LCD_WIDTH, LCD_HEIGHT); return 1; } RB_WRAP(lcd_mono_bitmap_part) { struct rocklua_image *src = rli_checktype(L, 1); int src_x = luaL_checkint(L, 2); int src_y = luaL_checkint(L, 3); int stride = luaL_checkint(L, 4); int x = luaL_checkint(L, 5); int y = luaL_checkint(L, 6); int width = luaL_checkint(L, 7); int height = luaL_checkint(L, 8); int screen = luaL_optint(L, 9, SCREEN_MAIN); rb->screens[screen]->mono_bitmap_part((const unsigned char *)src->data, src_x, src_y, stride, x, y, width, height); return 0; } RB_WRAP(lcd_mono_bitmap) { struct rocklua_image *src = rli_checktype(L, 1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int width = luaL_checkint(L, 4); int height = luaL_checkint(L, 5); int screen = luaL_optint(L, 6, SCREEN_MAIN); rb->screens[screen]->mono_bitmap((const unsigned char *)src->data, x, y, width, height); return 0; } #if LCD_DEPTH > 1 RB_WRAP(lcd_bitmap_part) { struct rocklua_image *src = rli_checktype(L, 1); int src_x = luaL_checkint(L, 2); int src_y = luaL_checkint(L, 3); int stride = luaL_checkint(L, 4); int x = luaL_checkint(L, 5); int y = luaL_checkint(L, 6); int width = luaL_checkint(L, 7); int height = luaL_checkint(L, 8); int screen = luaL_optint(L, 9, SCREEN_MAIN); rb->screens[screen]->bitmap_part(src->data, src_x, src_y, stride, x, y, width, height); return 0; } RB_WRAP(lcd_bitmap) { struct rocklua_image *src = rli_checktype(L, 1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int width = luaL_checkint(L, 4); int height = luaL_checkint(L, 5); int screen = luaL_optint(L, 6, SCREEN_MAIN); rb->screens[screen]->bitmap(src->data, x, y, width, height); return 0; } RB_WRAP(lcd_get_backdrop) { fb_data* backdrop = rb->lcd_get_backdrop(); if(backdrop == NULL) lua_pushnil(L); else rli_wrap(L, backdrop, LCD_WIDTH, LCD_HEIGHT); return 1; } #endif /* LCD_DEPTH > 1 */ #if LCD_DEPTH == 16 RB_WRAP(lcd_bitmap_transparent_part) { struct rocklua_image *src = rli_checktype(L, 1); int src_x = luaL_checkint(L, 2); int src_y = luaL_checkint(L, 3); int stride = luaL_checkint(L, 4); int x = luaL_checkint(L, 5); int y = luaL_checkint(L, 6); int width = luaL_checkint(L, 7); int height = luaL_checkint(L, 8); int screen = luaL_optint(L, 9, SCREEN_MAIN); rb->screens[screen]->transparent_bitmap_part(src->data, src_x, src_y, stride, x, y, width, height); return 0; } RB_WRAP(lcd_bitmap_transparent) { struct rocklua_image *src = rli_checktype(L, 1); int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int width = luaL_checkint(L, 4); int height = luaL_checkint(L, 5); int screen = luaL_optint(L, 6, SCREEN_MAIN); rb->screens[screen]->transparent_bitmap(src->data, x, y, width, height); return 0; } #endif /* LCD_DEPTH == 16 */ #endif /* defined(LCD_BITMAP) */ RB_WRAP(current_tick) { lua_pushinteger(L, *rb->current_tick); return 1; } #ifdef HAVE_TOUCHSCREEN RB_WRAP(action_get_touchscreen_press) { short x, y; int result = rb->action_get_touchscreen_press(&x, &y); lua_pushinteger(L, result); lua_pushinteger(L, x); lua_pushinteger(L, y); return 3; } #endif RB_WRAP(kbd_input) { luaL_Buffer b; luaL_buffinit(L, &b); const char *input = luaL_optstring(L, 1, NULL); char *buffer = luaL_prepbuffer(&b); if(input != NULL) rb->strlcpy(buffer, input, LUAL_BUFFERSIZE); else buffer[0] = '\0'; if(!rb->kbd_input(buffer, LUAL_BUFFERSIZE)) { luaL_addsize(&b, strlen(buffer)); luaL_pushresult(&b); } else lua_pushnil(L); return 1; } #ifdef HAVE_TOUCHSCREEN RB_WRAP(touchscreen_set_mode) { enum touchscreen_mode mode = luaL_checkint(L, 1); rb->touchscreen_set_mode(mode); return 0; } RB_WRAP(touchscreen_get_mode) { lua_pushinteger(L, rb->touchscreen_get_mode()); return 1; } #endif RB_WRAP(font_getstringsize) { const unsigned char* str = luaL_checkstring(L, 1); int fontnumber = luaL_checkint(L, 2); int w, h; if (fontnumber == FONT_UI) fontnumber = rb->global_status->font_id[SCREEN_MAIN]; else fontnumber = FONT_SYSFIXED; int result = rb->font_getstringsize(str, &w, &h, fontnumber); lua_pushinteger(L, result); lua_pushinteger(L, w); lua_pushinteger(L, h); return 3; } #ifdef HAVE_LCD_COLOR RB_WRAP(lcd_rgbpack) { int r = luaL_checkint(L, 1); int g = luaL_checkint(L, 2); int b = luaL_checkint(L, 3); int result = LCD_RGBPACK(r, g, b); lua_pushinteger(L, result); return 1; } RB_WRAP(lcd_rgbunpack) { int rgb = luaL_checkint(L, 1); lua_pushinteger(L, RGB_UNPACK_RED(rgb)); lua_pushinteger(L, RGB_UNPACK_GREEN(rgb)); lua_pushinteger(L, RGB_UNPACK_BLUE(rgb)); return 3; } #endif RB_WRAP(read_bmp_file) { struct bitmap bm; const char* filename = luaL_checkstring(L, 1); bool dither = luaL_optboolean(L, 2, true); bool transparent = luaL_optboolean(L, 3, false); int format = FORMAT_NATIVE; if(dither) format |= FORMAT_DITHER; if(transparent) format |= FORMAT_TRANSPARENT; int result = rb->read_bmp_file(filename, &bm, 0, format | FORMAT_RETURN_SIZE, NULL); if(result > 0) { bm.data = (unsigned char*) rli_alloc(L, bm.width, bm.height); if(rb->read_bmp_file(filename, &bm, result, format, NULL) < 0) { /* Error occured, drop newly allocated image from stack */ lua_pop(L, 1); lua_pushnil(L); } } else lua_pushnil(L); return 1; } RB_WRAP(current_path) { const char *current_path = get_current_path(L, 1); if(current_path != NULL) lua_pushstring(L, current_path); else lua_pushnil(L); return 1; } static void fill_text_message(lua_State *L, struct text_message * message, int pos) { int i; luaL_checktype(L, pos, LUA_TTABLE); int n = luaL_getn(L, pos); const char **lines = (const char**) tlsf_malloc(n * sizeof(const char*)); if(lines == NULL) luaL_error(L, "Can't allocate %d bytes!", n * sizeof(const char*)); for(i=1; i<=n; i++) { lua_rawgeti(L, pos, i); lines[i-1] = luaL_checkstring(L, -1); lua_pop(L, 1); } message->message_lines = lines; message->nb_lines = n; } RB_WRAP(gui_syncyesno_run) { struct text_message main_message, yes_message, no_message; struct text_message *yes = NULL, *no = NULL; fill_text_message(L, &main_message, 1); if(!lua_isnoneornil(L, 2)) fill_text_message(L, (yes = &yes_message), 2); if(!lua_isnoneornil(L, 3)) fill_text_message(L, (no = &no_message), 3); enum yesno_res result = rb->gui_syncyesno_run(&main_message, yes, no); tlsf_free(main_message.message_lines); if(yes) tlsf_free(yes_message.message_lines); if(no) tlsf_free(no_message.message_lines); lua_pushinteger(L, result); return 1; } RB_WRAP(do_menu) { struct menu_callback_with_desc menu_desc = {NULL, NULL, Icon_NOICON}; struct menu_item_ex menu = {MT_RETURN_ID | MENU_HAS_DESC, {.strings = NULL}, {.callback_and_desc = &menu_desc}}; int i, n, start_selected; const char **items, *title; title = luaL_checkstring(L, 1); luaL_checktype(L, 2, LUA_TTABLE); start_selected = luaL_optint(L, 3, 0); n = luaL_getn(L, 2); items = (const char**) tlsf_malloc(n * sizeof(const char*)); if(items == NULL) luaL_error(L, "Can't allocate %d bytes!", n * sizeof(const char*)); for(i=1; i<=n; i++) { lua_rawgeti(L, 2, i); /* Push item on the stack */ items[i-1] = luaL_checkstring(L, -1); lua_pop(L, 1); /* Pop it */ } menu.strings = items; menu.flags |= MENU_ITEM_COUNT(n); menu_desc.desc = (unsigned char*) title; int result = rb->do_menu(&menu, &start_selected, NULL, false); tlsf_free(items); lua_pushinteger(L, result); return 1; } RB_WRAP(playlist_sync) { /* just pass NULL to work with the current playlist */ rb->playlist_sync(NULL); return 1; } RB_WRAP(playlist_remove_all_tracks) { /* just pass NULL to work with the current playlist */ int result = rb->playlist_remove_all_tracks(NULL); lua_pushinteger(L, result); return 1; } RB_WRAP(playlist_insert_track) { const char *filename; int position, queue, sync; /* for now don't take a playlist_info pointer, but just pass NULL to use the current playlist. If this changes later, all the other parameters can be shifted back. */ filename = luaL_checkstring(L, 1); /* only required parameter */ position = luaL_optint(L, 2, PLAYLIST_INSERT); queue = lua_toboolean(L, 3); /* default to false */ sync = lua_toboolean(L, 4); /* default to false */ int result = rb->playlist_insert_track(NULL, filename, position, queue, sync); lua_pushinteger(L, result); return 1; } RB_WRAP(playlist_insert_directory) { const char* dirname; int position, queue, recurse; /* just like in playlist_insert_track, only works with current playlist. */ dirname = luaL_checkstring(L, 1); /* only required parameter */ position = luaL_optint(L, 2, PLAYLIST_INSERT); queue = lua_toboolean(L, 3); /* default to false */ recurse = lua_toboolean(L, 4); /* default to false */ int result = rb->playlist_insert_directory(NULL, dirname, position, queue, recurse); lua_pushinteger(L, result); return 1; } SIMPLE_VOID_WRAPPER(backlight_force_on); SIMPLE_VOID_WRAPPER(backlight_use_settings); #ifdef HAVE_REMOTE_LCD SIMPLE_VOID_WRAPPER(remote_backlight_force_on); SIMPLE_VOID_WRAPPER(remote_backlight_use_settings); #endif #ifdef HAVE_BUTTON_LIGHT SIMPLE_VOID_WRAPPER(buttonlight_force_on); SIMPLE_VOID_WRAPPER(buttonlight_use_settings); #endif #ifdef HAVE_BACKLIGHT_BRIGHTNESS RB_WRAP(backlight_brightness_set) { int brightness = luaL_checkint(L, 1); backlight_brightness_set(brightness); return 0; } SIMPLE_VOID_WRAPPER(backlight_brightness_use_setting); #endif RB_WRAP(get_plugin_action) { static const struct button_mapping *m1[] = { pla_main_ctx }; int timeout = luaL_checkint(L, 1); int btn; #ifdef HAVE_REMOTE_LCD static const struct button_mapping *m2[] = { pla_main_ctx, pla_remote_ctx }; bool with_remote = luaL_optint(L, 2, 0); if (with_remote) btn = pluginlib_getaction(timeout, m2, 2); else #endif btn = pluginlib_getaction(timeout, m1, 1); lua_pushinteger(L, btn); return 1; } #define R(NAME) {#NAME, rock_##NAME} static const luaL_Reg rocklib[] = { /* Graphics */ #ifdef HAVE_LCD_BITMAP R(lcd_framebuffer), R(lcd_mono_bitmap_part), R(lcd_mono_bitmap), #if LCD_DEPTH > 1 R(lcd_get_backdrop), R(lcd_bitmap_part), R(lcd_bitmap), #endif #if LCD_DEPTH == 16 R(lcd_bitmap_transparent_part), R(lcd_bitmap_transparent), #endif #endif #ifdef HAVE_LCD_COLOR R(lcd_rgbpack), R(lcd_rgbunpack), #endif /* Kernel */ R(current_tick), /* Buttons */ #ifdef HAVE_TOUCHSCREEN R(action_get_touchscreen_press), R(touchscreen_set_mode), R(touchscreen_get_mode), #endif R(kbd_input), R(font_getstringsize), R(read_bmp_file), R(set_viewport), R(clear_viewport), R(current_path), R(gui_syncyesno_run), R(playlist_sync), R(playlist_remove_all_tracks), R(playlist_insert_track), R(playlist_insert_directory), R(do_menu), /* Backlight helper */ R(backlight_force_on), R(backlight_use_settings), #ifdef HAVE_REMOTE_LCD R(remote_backlight_force_on), R(remote_backlight_use_settings), #endif #ifdef HAVE_BUTTON_LIGHT R(buttonlight_force_on), R(buttonlight_use_settings), #endif #ifdef HAVE_BACKLIGHT_BRIGHTNESS R(backlight_brightness_set), R(backlight_brightness_use_setting), #endif R(get_plugin_action), {"new_image", rli_new}, {NULL, NULL} }; #undef R extern const luaL_Reg rocklib_aux[]; #define RB_CONSTANT(x) lua_pushinteger(L, x); lua_setfield(L, -2, #x); #define RB_STRING_CONSTANT(x) lua_pushstring(L, x); lua_setfield(L, -2, #x); /* ** Open Rockbox library */ LUALIB_API int luaopen_rock(lua_State *L) { luaL_register(L, LUA_ROCKLIBNAME, rocklib); luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux); RB_CONSTANT(HZ); RB_CONSTANT(LCD_WIDTH); RB_CONSTANT(LCD_HEIGHT); RB_CONSTANT(LCD_DEPTH); RB_CONSTANT(FONT_SYSFIXED); RB_CONSTANT(FONT_UI); RB_CONSTANT(PLAYLIST_PREPEND); RB_CONSTANT(PLAYLIST_INSERT); RB_CONSTANT(PLAYLIST_INSERT_LAST); RB_CONSTANT(PLAYLIST_INSERT_FIRST); RB_CONSTANT(PLAYLIST_INSERT_SHUFFLED); RB_CONSTANT(PLAYLIST_REPLACE); RB_CONSTANT(PLAYLIST_INSERT_LAST_SHUFFLED); #ifdef HAVE_TOUCHSCREEN RB_CONSTANT(TOUCHSCREEN_POINT); RB_CONSTANT(TOUCHSCREEN_BUTTON); #endif RB_CONSTANT(SCREEN_MAIN); #ifdef HAVE_REMOTE_LCD RB_CONSTANT(SCREEN_REMOTE); #endif /* some useful paths constants */ RB_STRING_CONSTANT(ROCKBOX_DIR); RB_STRING_CONSTANT(HOME_DIR); RB_STRING_CONSTANT(PLUGIN_DIR); RB_STRING_CONSTANT(PLUGIN_APPS_DATA_DIR); RB_STRING_CONSTANT(PLUGIN_GAMES_DATA_DIR); RB_STRING_CONSTANT(PLUGIN_DATA_DIR); RB_STRING_CONSTANT(VIEWERS_DATA_DIR); rli_init(L); return 1; } '#n541'>541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
/* MikMod sound library
(c) 1998, 1999, 2000, 2001, 2002 Miodrag Vallat and others - see file
AUTHORS for complete list.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
/*==============================================================================
$Id: virtch.c,v 1.4 2005/05/18 13:42:23 raphassenat Exp $
Sample mixing routines, using a 32 bits mixing buffer.
==============================================================================*/
/*
Optional features include:
(a) 4-step reverb (for 16 bit output only)
(b) Interpolation of sample data during mixing
(c) Dolby Surround Sound
*/
#if 0
#include <assert.h>
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stddef.h>
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <string.h>
#include "mikmod_internals.h"
#include "mikmod.h"
/*
Constant definitions
====================
BITSHIFT
Controls the maximum volume of the sound output. All data is shifted
right by BITSHIFT after being mixed. Higher values result in quieter
sound and less chance of distortion.
REVERBERATION
Controls the duration of the reverb. Larger values represent a shorter
reverb loop. Smaller values extend the reverb but can result in more of
an echo-ish sound.
*/
#define BITSHIFT 9
#define REVERBERATION 110000L
#define FRACBITS 11
#define FRACMASK ((1L<<FRACBITS)-1L)
#define TICKLSIZE 8192
#define TICKWSIZE (TICKLSIZE<<1)
#define TICKBSIZE (TICKWSIZE<<1)
#define CLICK_SHIFT 6
#define CLICK_BUFFER (1L<<CLICK_SHIFT)
#ifndef MIN
#define MIN(a,b) (((a)<(b)) ? (a) : (b))
#endif
typedef struct VINFO {
UBYTE kick; /* =1 -> sample has to be restarted */
UBYTE active; /* =1 -> sample is playing */
UWORD flags; /* 16/8 bits looping/one-shot */
SWORD handle; /* identifies the sample */
ULONG start; /* start index */
ULONG size; /* samplesize */
ULONG reppos; /* loop start */
ULONG repend; /* loop end */
ULONG frq; /* current frequency */
int vol; /* current volume */
int pan; /* current panning position */
int rampvol;
int lvolsel,rvolsel; /* Volume factor in range 0-255 */
int oldlvol,oldrvol;
SLONGLONG current; /* current index in the sample */
SLONGLONG increment; /* increment value */
} VINFO;
static SWORD **Samples;
static VINFO *vinf=NULL,*vnf;
static long tickleft,samplesthatfit,vc_memory=0;
static int vc_softchn;
static SLONGLONG idxsize,idxlpos,idxlend;
static SLONG *vc_tickbuf=NULL;
static UWORD vc_mode;
/* Reverb control variables */
static int RVc1, RVc2, RVc3, RVc4, RVc5, RVc6, RVc7, RVc8;
static ULONG RVRindex;
/* For Mono or Left Channel */
static SLONG *RVbufL1=NULL,*RVbufL2=NULL,*RVbufL3=NULL,*RVbufL4=NULL,
*RVbufL5=NULL,*RVbufL6=NULL,*RVbufL7=NULL,*RVbufL8=NULL;
/* For Stereo only (Right Channel) */
static SLONG *RVbufR1=NULL,*RVbufR2=NULL,*RVbufR3=NULL,*RVbufR4=NULL,
*RVbufR5=NULL,*RVbufR6=NULL,*RVbufR7=NULL,*RVbufR8=NULL;
#ifdef NATIVE_64BIT_INT
#define NATIVE SLONGLONG
#else
#define NATIVE SLONG
#endif
#if defined HAVE_SSE2 || defined HAVE_ALTIVEC
static size_t MixSIMDMonoNormal(const SWORD* srce,SLONG* dest,size_t index, size_t increment,size_t todo)
{
// TODO:
SWORD sample;
SLONG lvolsel = vnf->lvolsel;
while(todo--) {
sample = srce[index >> FRACBITS];
index += increment;
*dest++ += lvolsel * sample;
}
return index;
}
static size_t MixSIMDStereoNormal(const SWORD* srce, SLONG* dest, size_t index, size_t increment,size_t todo)
{
SWORD vol[8] = {vnf->lvolsel, vnf->rvolsel};
SWORD sample;
SLONG remain = todo;
// Dest can be misaligned ...
while(!IS_ALIGNED_16(dest)) {
sample=srce[(index += increment) >> FRACBITS];
*dest++ += vol[0] * sample;
*dest++ += vol[1] * sample;
todo--;
}
// Srce is always aligned ...
#if defined HAVE_SSE2
remain = todo&3;
{
__m128i v0 = _mm_set_epi16(0, vol[1],
0, vol[0],
0, vol[1],
0, vol[0]);
for(todo>>=2;todo; todo--)
{
SWORD s0 = srce[(index += increment) >> FRACBITS];
SWORD s1 = srce[(index += increment) >> FRACBITS];
SWORD s2 = srce[(index += increment) >> FRACBITS];
SWORD s3 = srce[(index += increment) >> FRACBITS];
__m128i v1 = _mm_set_epi16(0, s1, 0, s1, 0, s0, 0, s0);
__m128i v2 = _mm_set_epi16(0, s3, 0, s3, 0, s2, 0, s2);
__m128i v3 = _mm_load_si128((__m128i*)(dest+0));
__m128i v4 = _mm_load_si128((__m128i*)(dest+4));
_mm_store_si128((__m128i*)(dest+0), _mm_add_epi32(v3, _mm_madd_epi16(v0, v1)));
_mm_store_si128((__m128i*)(dest+4), _mm_add_epi32(v4, _mm_madd_epi16(v0, v2)));
dest+=8;
}
}
#elif defined HAVE_ALTIVEC
remain = todo&3;
{
vector signed short r0 = vec_ld(0, vol);
vector signed short v0 = vec_perm(r0, r0, (vector unsigned char)(0, 1, // l
0, 1, // l
2, 3, // r
2, 1, // r
0, 1, // l
0, 1, // l
2, 3, // r
2, 3 // r
));
SWORD s[8];
for(todo>>=2;todo; todo--)
{
// Load constants
s[0] = srce[(index += increment) >> FRACBITS];
s[1] = srce[(index += increment) >> FRACBITS];
s[2] = srce[(index += increment) >> FRACBITS];
s[3] = srce[(index += increment) >> FRACBITS];
s[4] = 0;
vector short int r1 = vec_ld(0, s);
vector signed short v1 = vec_perm(r1, r1, (vector unsigned char)(0*2, 0*2+1, // s0
4*2, 4*2+1, // 0
0*2, 0*2+1, // s0
4*2, 4*2+1, // 0
1*2, 1*2+1, // s1
4*2, 4*2+1, // 0
1*2, 1*2+1, // s1
4*2, 4*2+1 // 0
));
vector signed short v2 = vec_perm(r1, r1, (vector unsigned char)(2*2, 2*2+1, // s2
4*2, 4*2+1, // 0
2*2, 2*2+1, // s2
4*2, 4*2+1, // 0
3*2, 3*2+1, // s3
4*2, 4*2+1, // 0
3*2, 3*2+1, // s3
4*2, 4*2+1 // 0
));
vector signed int v3 = vec_ld(0, dest);
vector signed int v4 = vec_ld(0, dest + 4);
vector signed int v5 = vec_mule(v0, v1);
vector signed int v6 = vec_mule(v0, v2);
vec_st(vec_add(v3, v5), 0, dest);
vec_st(vec_add(v4, v6), 0x10, dest);
dest+=8;
}
}
#endif // HAVE_ALTIVEC
// Remaining bits ...
while(remain--) {
sample=srce[(index += increment) >> FRACBITS];
*dest++ += vol[0] * sample;
*dest++ += vol[1] * sample;
}
return index;
}
#endif
/*========== 32 bit sample mixers - only for 32 bit platforms */
#ifndef NATIVE_64BIT_INT
static SLONG Mix32MonoNormal(const SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)
{
#if defined HAVE_ALTIVEC || defined HAVE_SSE2
if (md_mode & DMODE_SIMDMIXER)
{
return MixSIMDMonoNormal(srce, dest, index, increment, todo);
}
else
#endif
{
SWORD sample;
SLONG lvolsel = vnf->lvolsel;
while(todo--) {
sample = srce[index >> FRACBITS];
index += increment;
*dest++ += lvolsel * sample;
}
}
return index;
}
// FIXME: This mixer should works also on 64-bit platform
// Hint : changes SLONG / SLONGLONG mess with size_t
static SLONG Mix32StereoNormal(const SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)
{
#if defined HAVE_ALTIVEC || defined HAVE_SSE2
if (md_mode & DMODE_SIMDMIXER)
{