lua add sound_current, consolidate sound_ functions

add defines for sound functions
 SOUND_VOLUME, SOUND_BALANCE, SOUND_CHANNELS, SOUND_STEREO_WIDTH ...
 defines depend on target
 require "sound_defines" to add them to rb.sound_settings[]

consolidates:
 sound_set, sound_current, sound_default, sound_min, sound_max,
 sound_unit, sound_pitch, sound_val2phys to a single function

 rb.sound("name", setting, [value])

 require "sound.lua" for old functionality

Change-Id: Ice695218aa433f4fcbb48fbd6b8a9bf29c994110
diff --git a/apps/plugins/lua/include_lua/sound.lua b/apps/plugins/lua/include_lua/sound.lua
new file mode 100644
index 0000000..072df0b
--- /dev/null
+++ b/apps/plugins/lua/include_lua/sound.lua
@@ -0,0 +1,36 @@
+--[[ Lua RB sound Operations
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id$
+ *
+ * Copyright (C) 2019 William Wilgus
+ *
+ * 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.
+ *
+ ****************************************************************************/
+]]
+
+-- [[ conversion to old style sound_ functions ]]
+if not rb.sound then rb.splash(rb.HZ, "No Support!") return nil end
+
+require "sound_defines"
+
+rb.sound_set = function(s, v) return rb.sound("set", s, v) end
+rb.sound_current = function(s) return rb.sound("current", s) end
+rb.sound_default = function(s) return rb.sound("default", s) end
+rb.sound_min = function(s) return rb.sound("min", s) end
+rb.sound_max = function(s) return rb.sound("max", s) end
+rb.sound_unit = function(s) return rb.sound("unit", s) end
+rb.sound_pitch = function(s) return rb.sound("pitch", s) end
+rb.sound_val2phys = function(s, v) return rb.sound("val2phys", s, v) end
diff --git a/apps/plugins/lua/lua.make b/apps/plugins/lua/lua.make
index f54d35b..16e25c3 100644
--- a/apps/plugins/lua/lua.make
+++ b/apps/plugins/lua/lua.make
@@ -17,7 +17,9 @@
 
 LUA_INCLUDEDIR := $(LUA_SRCDIR)/include_lua
 LUA_INCLUDELIST := $(addprefix $(LUA_BUILDDIR)/,audio.lua blit.lua color.lua draw.lua \
-	image.lua lcd.lua math_ex.lua print.lua timer.lua playlist.lua pcm.lua rbcompat.lua)
+						image.lua lcd.lua math_ex.lua print.lua \
+						timer.lua playlist.lua pcm.lua sound.lua \
+						rbcompat.lua )
 
 
 ifndef APP_TYPE
@@ -35,16 +37,13 @@
     ROCKS += $(LUA_BUILDDIR)/lua.rock
 endif
 
-$(LUA_BUILDDIR)/lua.rock: $(LUA_OBJ) $(TLSFLIB) $(LUA_BUILDDIR)/actions.lua $(LUA_BUILDDIR)/buttons.lua $(LUA_BUILDDIR)/settings.lua $(LUA_BUILDDIR)/rocklib_aux.o $(LUA_BUILDDIR)/rb_defines.lua $(LUA_INCLUDELIST)
+$(LUA_BUILDDIR)/lua.rock: $(LUA_OBJ) $(TLSFLIB) $(LUA_BUILDDIR)/actions.lua $(LUA_BUILDDIR)/buttons.lua $(LUA_BUILDDIR)/settings.lua \
+                                                $(LUA_BUILDDIR)/rocklib_aux.o $(LUA_BUILDDIR)/rb_defines.lua $(LUA_BUILDDIR)/sound_defines.lua \
+                                                $(LUA_INCLUDELIST)
 
 $(LUA_BUILDDIR)/actions.lua: $(LUA_OBJ) $(LUA_SRCDIR)/action_helper.pl
 	$(call PRINTS,GEN $(@F))$(CC) $(PLUGINFLAGS) $(INCLUDES) -E -P $(APPSDIR)/plugins/lib/pluginlib_actions.h | $(LUA_SRCDIR)/action_helper.pl > $(LUA_BUILDDIR)/actions.lua
 
-$(LUA_BUILDDIR)/rb_defines.lua: $(LUA_OBJ) $(LUA_SRCDIR)/rbdefines_helper.pl
-	$(SILENT)$(CC) $(INCLUDES) -dD -E -P $(TARGET) $(CFLAGS) -include plugin.h - < /dev/null | $(LUA_SRCDIR)/rbdefines_helper.pl | \
-	$(HOSTCC) -fno-builtin $(HOST_INCLUDES) -x c -o $(LUA_BUILDDIR)/rbdefines_helper -
-	$(call PRINTS,GEN $(@F))$(LUA_BUILDDIR)/rbdefines_helper > $(LUA_BUILDDIR)/rb_defines.lua
-
 $(LUA_BUILDDIR)/settings.lua: $(LUA_OBJ) $(LUA_SRCDIR)/settings_helper.pl
 	$(SILENT)$(CC) $(INCLUDES) -E -P $(TARGET) $(CFLAGS) -include plugin.h -include cuesheet.h - < /dev/null | $(LUA_SRCDIR)/settings_helper.pl | \
 		$(CC) $(INCLUDES) $(TARGET) $(CFLAGS) -S -x c -include config.h -include plugin.h -o $(LUA_BUILDDIR)/settings_helper.s -
@@ -55,6 +54,16 @@
 	$(SILENT)$(CC) $(INCLUDES) -dM -E -P -include button-target.h - < /dev/null | $(LUA_SRCDIR)/button_helper.pl | $(HOSTCC) -fno-builtin $(HOST_INCLUDES) -x c -o $(LUA_BUILDDIR)/button_helper -
 	$(call PRINTS,GEN $(@F))$(LUA_BUILDDIR)/button_helper > $(LUA_BUILDDIR)/buttons.lua
 
+$(LUA_BUILDDIR)/rb_defines.lua: $(LUA_OBJ) $(LUA_SRCDIR)/rbdefines_helper.pl
+	$(SILENT)$(CC) $(INCLUDES) -dD -E -P $(TARGET) $(CFLAGS) -include plugin.h - < /dev/null | $(LUA_SRCDIR)/rbdefines_helper.pl "rb_defines" | \
+	$(HOSTCC) -fno-builtin $(HOST_INCLUDES) -x c -o $(LUA_BUILDDIR)/rbdefines_helper -
+	$(call PRINTS,GEN $(@F))$(LUA_BUILDDIR)/rbdefines_helper > $(LUA_BUILDDIR)/rb_defines.lua
+
+$(LUA_BUILDDIR)/sound_defines.lua: $(LUA_OBJ) $(LUA_SRCDIR)/rbdefines_helper.pl
+	$(SILENT)$(CC) $(INCLUDES) -dD -E -P $(TARGET) $(CFLAGS) -include config.h -include audiohw_settings.h - < /dev/null | $(LUA_SRCDIR)/rbdefines_helper.pl "sound_defines" | \
+	$(HOSTCC) -fno-builtin $(HOST_INCLUDES) -x c -o $(LUA_BUILDDIR)/sounddefines_helper -
+	$(call PRINTS,GEN $(@F))$(LUA_BUILDDIR)/sounddefines_helper > $(LUA_BUILDDIR)/sound_defines.lua
+
 $(LUA_BUILDDIR)/rocklib_aux.c: $(APPSDIR)/plugin.h $(LUA_OBJ) $(LUA_SRCDIR)/rocklib_aux.pl
 	$(call PRINTS,GEN $(@F))$(CC) $(PLUGINFLAGS) $(INCLUDES) -E -P -include plugin.h - < /dev/null | $(LUA_SRCDIR)/rocklib_aux.pl $(LUA_SRCDIR) > $(LUA_BUILDDIR)/rocklib_aux.c
 
diff --git a/apps/plugins/lua/rbdefines_helper.pl b/apps/plugins/lua/rbdefines_helper.pl
index ba15346..b556090 100755
--- a/apps/plugins/lua/rbdefines_helper.pl
+++ b/apps/plugins/lua/rbdefines_helper.pl
@@ -20,28 +20,46 @@
 
 #rockbox to lua define generator, add names of constants to the array to include
 
-my @rockbox_defines = (
-    '^HZ$',
-    '^LCD_(DEPTH|HEIGHT|WIDTH)$',
-    '^MODEL_NAME$',
-    '^SCREEN_MAIN$',
-    '^LCD_DEFAULT_(FG|BG)$',
-    '^LCD_REMOTE_(DEPTH|HEIGHT|WIDTH)$',
-    '^LCD_.+(BRIGHT|DARK)COLOR',
-    '^SCREEN_REMOTE$',
-    '^FONT_SYSFIXED$',
-    '^FONT_UI$',
-    '^PLAYBACK_EVENT_.*',
-    '^PLAYLIST_(INSERT|PREPEND|REPLACE)',
-    '^TOUCHSCREEN_(POINT|BUTTON)$',
-    '^SYS_CHARGER_(DIS|)CONNECTED$',
-    '^SYS_(TIMEOUT|POWEROFF)$',
-    '^SYS_USB_(DIS|)CONNECTED$',
-    '^HOME_DIR$',
-    '^PLUGIN_DIR$',
-    '^PLUGIN(_APPS_|_GAMES_|_)DATA_DIR$',
-    '^ROCKBOX_DIR$',
-    '^VIEWERS_DATA_DIR$');
+if ($#ARGV + 1 != 1) {
+    warn "no definition type defined";
+    exit;
+}
+
+my $def_type = $ARGV[0];
+#warn "$def_type\n";
+my $lua_table;
+my @rockbox_defines;
+
+if ($def_type eq "rb_defines") {
+    $lua_table = "rb";
+    @rockbox_defines = (
+        '^HZ$',
+        '^LCD_(DEPTH|HEIGHT|WIDTH)$',
+        '^MODEL_NAME$',
+        '^SCREEN_MAIN$',
+        '^LCD_DEFAULT_(FG|BG)$',
+        '^LCD_REMOTE_(DEPTH|HEIGHT|WIDTH)$',
+        '^LCD_.+(BRIGHT|DARK)COLOR',
+        '^SCREEN_REMOTE$',
+        '^FONT_SYSFIXED$',
+        '^FONT_UI$',
+        '^PLAYBACK_EVENT_.*',
+        '^PLAYLIST_(INSERT|PREPEND|REPLACE)',
+        '^TOUCHSCREEN_(POINT|BUTTON)$',
+        '^SYS_CHARGER_(DIS|)CONNECTED$',
+        '^SYS_(TIMEOUT|POWEROFF)$',
+        '^SYS_USB_(DIS|)CONNECTED$',
+        '^HOME_DIR$',
+        '^PLUGIN_DIR$',
+        '^PLUGIN(_APPS_|_GAMES_|_)DATA_DIR$',
+        '^ROCKBOX_DIR$',
+        '^VIEWERS_DATA_DIR$');
+}
+elsif ($def_type eq "sound_defines") {
+    $lua_table = "rb.sound_settings";
+    @rockbox_defines = (
+        '^(?!.*LAST_SETTING)SOUND_');
+}
 
 my @captured_defines;
 my @names_seen;
@@ -115,7 +133,12 @@
             else { next; }
             do_enum($line)
         }
-
+        elsif($line =~ /^enum.*{[^;]+};.*/) #enum {
+        {
+            next if($line =~ /enum\s*__.*/); #don't add reserved
+            next if(do_enum($line));
+            
+        }
 }
 #warn "total defines: ".scalar @all_defines;
 #warn "captured defines: ".scalar @captured_defines;
@@ -123,17 +146,18 @@
 my @sorted_defines = sort { @$a{'name'} cmp @$b{'name'} } @captured_defines;
 
 printf "int main(void)\n{\n";
-printf "\tprintf(\"--[[Autogenerated rockbox constants]]\\n\\n\");";
+printf "\tprintf(\"--[[Autogenerated rockbox constants]]\\n\\n\");\n\n";
+printf "\tprintf(\"%s = %s or {}\\n\");\n", $lua_table, $lua_table;
 # Print the C array
 foreach my $define (@sorted_defines)
 {
     if(@$define{'value'} =~ /^0[xX][0-9a-fA-F]+$/) #hex number
     {
-        printf "\tprintf(\"rb[\\\"%%s\\\"] = 0x%%x\\n\", stringify(%s), %s);\n", @$define{'name'}, @$define{'name'};
+        printf "\tprintf(\"%s[\\\"%%s\\\"] = 0x%%x\\n\", stringify(%s), %s);\n", $lua_table, @$define{'name'}, @$define{'name'};
     }
     elsif(@$define{'value'} =~ /^[0-9]+$/) #number
     {
-        printf "\tprintf(\"rb[\\\"%%s\\\"] = %%d\\n\", stringify(%s), %s);\n", @$define{'name'}, @$define{'name'};
+        printf "\tprintf(\"%s[\\\"%%s\\\"] = %%d\\n\", stringify(%s), %s);\n", $lua_table, @$define{'name'}, @$define{'name'};
     }
     else #might be a string but we don't know since the macro isn't expanded far enough
     {
@@ -167,11 +191,11 @@
         if ($var =~$quot_regex) #has a quote it is a string
         {
             #guard with empty literals "" so gcc throws an error if it isn't a string
-            printf "\tprintf(\"rb[\\\"%%s\\\"] = \\\"%%s\\\"\\n\", stringify(%s), \"\" %s \"\");\n", @$define{'name'}, @$define{'name'};
+            printf "\tprintf(\"%s[\\\"%%s\\\"] = \\\"%%s\\\"\\n\", stringify(%s), \"\" %s \"\");\n", $lua_table, @$define{'name'}, @$define{'name'};
         }
         elsif ($var =~$num_regex) #it must be a number
         {
-            printf "\tprintf(\"rb[\\\"%%s\\\"] = %%d\\n\", stringify(%s), %s);\n", @$define{'name'}, @$define{'name'};
+            printf "\tprintf(\"%s[\\\"%%s\\\"] = %%d\\n\", stringify(%s), %s);\n", $lua_table, @$define{'name'}, @$define{'name'};
         }
         else { warn "Skipping ".@$define{'name'}." indeterminate macro type\n"; }
     }
@@ -186,6 +210,23 @@
 
 sub do_enum {
     my ($line) = @_;
+    if($line =~ /.*enum.*{(.*)};.*/) #single line enums
+    {
+        print $line;
+        $value = "0"; #enums are always integers
+        my $enum = $1;
+        $enum =~ s/\s+//g;;
+        my @values = split(',', $enum);
+
+        foreach my $name(@values) {
+            if(grep($name =~ $_, @rockbox_defines))
+            {
+                push(@names_seen, $name);
+                push(@captured_defines, {'name' => $name, 'value' => $value});
+            }
+        }
+        return 1;
+    }
 
     while($line = <STDIN>)
     {
@@ -209,4 +250,5 @@
         }
         
     }
+    return 0;
 }
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c
index 9518fe9..b37f524 100644
--- a/apps/plugins/lua/rocklib.c
+++ b/apps/plugins/lua/rocklib.c
@@ -378,6 +378,65 @@
     return 1;
 }
 
+RB_WRAP(sound)
+{
+    enum e_snd {SOUND_SET = 0, SOUND_CURRENT, SOUND_DEFAULT,
+                SOUND_MIN, SOUND_MAX, SOUND_UNIT, SOUND_SET_PITCH,
+                SOUND_VAL2PHYS, SOUND_ECOUNT};
+
+    const char *snd_option[] = {"set", "current", "default",
+                                "min", "max", "unit", "pitch",
+                                "val2phys", NULL};
+
+    lua_pushnil(L); /*push nil so options w/o return have something to return */
+
+    int option = luaL_checkoption (L, 1, NULL, snd_option);
+    int setting = luaL_checkint(L, 2);
+    int value, result;
+    switch(option)
+    {
+        case SOUND_SET:
+            value = luaL_checkint(L, 3);
+            rb->sound_set(setting, value);
+            return 1; /*nil*/
+            break;
+        case SOUND_CURRENT:
+            result = rb->sound_current(setting);
+            break;
+        case SOUND_DEFAULT:
+            result = rb->sound_default(setting);
+            break;
+        case SOUND_MIN:
+            result = rb->sound_min(setting);
+            break;
+        case SOUND_MAX:
+            result = rb->sound_max(setting);
+            break;
+        case SOUND_UNIT:
+            lua_pushstring (L, rb->sound_unit(setting));
+            return 1;
+            break;
+#if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
+    (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHCONTROL)
+        case SOUND_SET_PITCH:
+            rb->sound_set_pitch(setting);
+            return 1;/*nil*/
+            break;
+#endif
+        case SOUND_VAL2PHYS:
+            value = luaL_checkint(L, 3);
+            result = rb->sound_val2phys(setting, value);      
+            break;
+
+        default:
+            return 1;
+            break;
+    }
+
+    lua_pushinteger(L, result);
+    return 1;
+}
+
 #if CONFIG_CODEC == SWCODEC
 RB_WRAP(pcm)
 {
@@ -723,9 +782,10 @@
     RB_FUNC(gui_syncyesno_run),
     RB_FUNC(do_menu),
 
-    /* DEVICE AUDIO / PLAYLIST CONTROL */
+    /* DEVICE AUDIO / SOUND / PLAYLIST CONTROL */
     RB_FUNC(audio),
     RB_FUNC(playlist),
+    RB_FUNC(sound),
 #if CONFIG_CODEC == SWCODEC
     RB_FUNC(pcm),
     RB_FUNC(mixer_frequency),
diff --git a/apps/plugins/lua/rocklib_aux.pl b/apps/plugins/lua/rocklib_aux.pl
index 5d77288..c059801 100755
--- a/apps/plugins/lua/rocklib_aux.pl
+++ b/apps/plugins/lua/rocklib_aux.pl
@@ -99,6 +99,7 @@
                            '^pcm_play_(stop|pause|lock|unlock)$',
                            '^pcm_(apply_settings|get_bytes_waiting)$',
                            '^pcm_(set_frequency|calculate_peaks)$',
+                           '^sound_(set|current|default|min|max|unit|pitch|val2phys)$',
                            '^mixer_(set|get)_frequency$',
                            '^(trigger|cancel)_cpu_boost$',
                            '^round_value_to_list32$');